Advertisement
joaopaulofcc

Semana 6 - Parte 4

Sep 30th, 2020
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.88 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'Models/Produto.dart';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class MyApp extends StatelessWidget {
  7.   @override
  8.   Widget build(BuildContext context) {
  9.     return MaterialApp(
  10.       title: "Mercado",
  11.       home: Mercado(),
  12.     );
  13.   }
  14. }
  15.  
  16. class Mercado extends StatefulWidget {
  17.   @override
  18.   _MercadoState createState() => _MercadoState();
  19. }
  20.  
  21. class _MercadoState extends State<Mercado> {
  22.   final TextEditingController _controladorNome = TextEditingController();
  23.   final TextEditingController _controladorQuantidade = TextEditingController();
  24.   final TextEditingController _controladorValor = TextEditingController();
  25.   final TextEditingController _controladorData = TextEditingController();
  26.  
  27.   final FocusNode fnNome = FocusNode();
  28.   final FocusNode fnQtd = FocusNode();
  29.   final FocusNode fnValor = FocusNode();
  30.  
  31.   final List<Produto> produtos = new List();
  32.   bool sort;
  33.  
  34.   @override
  35.   void initState() {
  36.     sort = false;
  37.     super.initState();
  38.   }
  39.  
  40.   onSortColumn(int columnIndex, bool ascending) {
  41.     if (columnIndex == 0) {
  42.       if (ascending) {
  43.         produtos.sort((a, b) => a.nome.compareTo(b.nome));
  44.       } else {
  45.         produtos.sort((a, b) => b.nome.compareTo(a.nome));
  46.       }
  47.     }
  48.   }
  49.  
  50.   void _showToast(BuildContext context, String message) {
  51.     final scaffold = Scaffold.of(context);
  52.     if (message != null) {
  53.       scaffold.showSnackBar(
  54.         SnackBar(
  55.           content: Text(message),
  56.           action: SnackBarAction(
  57.               label: 'FECHAR', onPressed: scaffold.hideCurrentSnackBar),
  58.         ),
  59.       );
  60.     }
  61.   }
  62.  
  63.   Future<void> _selectDate(context) async {
  64.     DateTime selectedDate = await showDatePicker(
  65.         context: context,
  66.         initialDate: DateTime.now(),
  67.         firstDate: DateTime(2017),
  68.         lastDate: DateTime(2021));
  69.     String resultado =
  70.         "${selectedDate.day}/${selectedDate.month}/${selectedDate.year}";
  71.     _controladorData.text = resultado;
  72.   }
  73.  
  74.   @override
  75.   Widget build(BuildContext context) {
  76.     return MaterialApp(
  77.         home: DefaultTabController(
  78.             length: 2,
  79.             child: Scaffold(
  80.                 resizeToAvoidBottomPadding: false,
  81.                 appBar: AppBar(
  82.                     title: Text('Produtos'),
  83.                     bottom: TabBar(tabs: [
  84.                       Tab(icon: Icon(Icons.add)),
  85.                       Tab(icon: Icon(Icons.table_chart))
  86.                     ])),
  87.                 body: TabBarView(
  88.                   children: [
  89.                     Column(
  90.                       children: <Widget>[
  91.                         Padding(
  92.                             padding: const EdgeInsets.all(16.0),
  93.                             child: TextFormField(
  94.                               controller: _controladorNome,
  95.                               decoration: InputDecoration(labelText: 'Nome'),
  96.                               textInputAction: TextInputAction.next,
  97.                               focusNode: fnNome,
  98.                               onFieldSubmitted: (term) {
  99.                                 fnNome.unfocus();
  100.                                 FocusScope.of(context).requestFocus(fnQtd);
  101.                               },
  102.                             )),
  103.                         Padding(
  104.                             padding: const EdgeInsets.all(16.0),
  105.                             child: TextFormField(
  106.                                 controller: _controladorQuantidade,
  107.                                 decoration:
  108.                                     InputDecoration(labelText: 'Quantidade'),
  109.                                 keyboardType: TextInputType.number,
  110.                                 textInputAction: TextInputAction.next,
  111.                                 focusNode: fnQtd,
  112.                                 onFieldSubmitted: (term) {
  113.                                   fnQtd.unfocus();
  114.                                   FocusScope.of(context).requestFocus(fnValor);
  115.                                 })),
  116.                         Padding(
  117.                             padding: const EdgeInsets.all(16.0),
  118.                             child: TextFormField(
  119.                                 controller: _controladorValor,
  120.                                 decoration: InputDecoration(labelText: 'Valor'),
  121.                                 keyboardType: TextInputType.number,
  122.                                 focusNode: fnValor,
  123.                                 onFieldSubmitted: (term) {
  124.                                   fnQtd.unfocus();
  125.                                 })),
  126.                         Padding(
  127.                           padding: const EdgeInsets.all(16.0),
  128.                           child: Row(
  129.                             children: [
  130.                               Expanded(
  131.                                   child: TextField(
  132.                                 controller: _controladorData,
  133.                                 enabled: false,
  134.                                 decoration: InputDecoration(labelText: 'Data'),
  135.                               )),
  136.                               Expanded(
  137.                                 child: RaisedButton(
  138.                                     child: Text('Selecionar Data'),
  139.                                     onPressed: () {
  140.                                       _selectDate(context);
  141.                                     }),
  142.                               )
  143.                             ],
  144.                           ),
  145.                         ),
  146.                         Padding(
  147.                           padding: const EdgeInsets.only(top: 16.0),
  148.                           child: Builder(
  149.                               builder: (context) => RaisedButton(
  150.                                     child: Text('Cadastrar'),
  151.                                     onPressed: () {
  152.                                       final String nome = _controladorNome.text;
  153.                                       final int quantidade = int.tryParse(
  154.                                           _controladorQuantidade.text);
  155.                                       final double valor = double.tryParse(
  156.                                           _controladorValor.text);
  157.                                       final Produto produtoNovo = Produto(
  158.                                           nome,
  159.                                           quantidade,
  160.                                           valor,
  161.                                           _controladorData.text);
  162.  
  163.                                       produtos.add(produtoNovo);
  164.  
  165.                                       _controladorNome.text = "";
  166.                                       _controladorQuantidade.text = "";
  167.                                       _controladorValor.text = "";
  168.                                       _controladorData.text = "";
  169.  
  170.                                       _showToast(context, "Produto adicionado");
  171.  
  172.                                       setState(() {});
  173.                                     },
  174.                                   )),
  175.                         )
  176.                       ],
  177.                     ),
  178.                     SingleChildScrollView(
  179.                         scrollDirection: Axis.vertical,
  180.                         child: Center(
  181.                           child: Padding(
  182.                             padding: const EdgeInsets.only(top: 30.0),
  183.                             child: DataTable(
  184.                               sortAscending: sort,
  185.                               sortColumnIndex: 0,
  186.                               columns: [
  187.                                 DataColumn(
  188.                                     label: Text("Nome",
  189.                                         style: TextStyle(fontSize: 16)),
  190.                                     numeric: false,
  191.                                     onSort: (columnIndex, ascending) {
  192.                                       setState(() {
  193.                                         sort = !sort;
  194.                                       });
  195.                                       onSortColumn(columnIndex, ascending);
  196.                                     }),
  197.                                 DataColumn(
  198.                                   label: Text("Qtd",
  199.                                       style: TextStyle(fontSize: 16)),
  200.                                   numeric: true,
  201.                                 ),
  202.                                 DataColumn(
  203.                                   label: Text("Valor",
  204.                                       style: TextStyle(fontSize: 16)),
  205.                                   numeric: true,
  206.                                 ),
  207.                                 DataColumn(
  208.                                   label: Text("Data",
  209.                                       style: TextStyle(fontSize: 16)),
  210.                                   numeric: false,
  211.                                 ),
  212.                               ],
  213.                               rows: produtos
  214.                                   .map(
  215.                                     (produto) => DataRow(cells: [
  216.                                       DataCell(
  217.                                         Text(produto.nome),
  218.                                       ),
  219.                                       DataCell(
  220.                                         Text(produto.quantidade.toString()),
  221.                                       ),
  222.                                       DataCell(
  223.                                         Text(produto.valor.toString()),
  224.                                       ),
  225.                                       DataCell(
  226.                                         Text(produto.data),
  227.                                       ),
  228.                                     ]),
  229.                                   )
  230.                                   .toList(),
  231.                             ),
  232.                           ),
  233.                         ))
  234.                   ],
  235.                 ))));
  236.   }
  237. }
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement