Advertisement
joaopaulofcc

Semana 6 - Parte 5

Sep 30th, 2020
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 11.02 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.   void _deleteRegister(Produto produto) {
  64.     int index = produtos.indexOf(produto);
  65.     produtos.removeAt(index);
  66.   }
  67.  
  68.   Future<void> _selectDate(context) async {
  69.     DateTime selectedDate = await showDatePicker(
  70.         context: context,
  71.         initialDate: DateTime.now(),
  72.         firstDate: DateTime(2017),
  73.         lastDate: DateTime(2021));
  74.     String resultado =
  75.         "${selectedDate.day}/${selectedDate.month}/${selectedDate.year}";
  76.     _controladorData.text = resultado;
  77.   }
  78.  
  79.   @override
  80.   Widget build(BuildContext context) {
  81.     return MaterialApp(
  82.         home: DefaultTabController(
  83.             length: 2,
  84.             child: Scaffold(
  85.                 resizeToAvoidBottomPadding: false,
  86.                 appBar: AppBar(
  87.                     title: Text('Produtos'),
  88.                     bottom: TabBar(tabs: [
  89.                       Tab(icon: Icon(Icons.add)),
  90.                       Tab(icon: Icon(Icons.table_chart))
  91.                     ])),
  92.                 body: TabBarView(
  93.                   children: [
  94.                     Column(
  95.                       children: <Widget>[
  96.                         Padding(
  97.                             padding: const EdgeInsets.all(16.0),
  98.                             child: TextFormField(
  99.                               controller: _controladorNome,
  100.                               decoration: InputDecoration(labelText: 'Nome'),
  101.                               textInputAction: TextInputAction.next,
  102.                               focusNode: fnNome,
  103.                               onFieldSubmitted: (term) {
  104.                                 fnNome.unfocus();
  105.                                 FocusScope.of(context).requestFocus(fnQtd);
  106.                               },
  107.                             )),
  108.                         Padding(
  109.                             padding: const EdgeInsets.all(16.0),
  110.                             child: TextFormField(
  111.                                 controller: _controladorQuantidade,
  112.                                 decoration:
  113.                                     InputDecoration(labelText: 'Quantidade'),
  114.                                 keyboardType: TextInputType.number,
  115.                                 textInputAction: TextInputAction.next,
  116.                                 focusNode: fnQtd,
  117.                                 onFieldSubmitted: (term) {
  118.                                   fnQtd.unfocus();
  119.                                   FocusScope.of(context).requestFocus(fnValor);
  120.                                 })),
  121.                         Padding(
  122.                             padding: const EdgeInsets.all(16.0),
  123.                             child: TextFormField(
  124.                                 controller: _controladorValor,
  125.                                 decoration: InputDecoration(labelText: 'Valor'),
  126.                                 keyboardType: TextInputType.number,
  127.                                 focusNode: fnValor,
  128.                                 onFieldSubmitted: (term) {
  129.                                   fnQtd.unfocus();
  130.                                 })),
  131.                         Padding(
  132.                           padding: const EdgeInsets.all(16.0),
  133.                           child: Row(
  134.                             children: [
  135.                               Expanded(
  136.                                   child: TextField(
  137.                                 controller: _controladorData,
  138.                                 enabled: false,
  139.                                 decoration: InputDecoration(labelText: 'Data'),
  140.                               )),
  141.                               Expanded(
  142.                                 child: RaisedButton(
  143.                                     child: Text('Selecionar Data'),
  144.                                     onPressed: () {
  145.                                       _selectDate(context);
  146.                                     }),
  147.                               )
  148.                             ],
  149.                           ),
  150.                         ),
  151.                         Padding(
  152.                           padding: const EdgeInsets.only(top: 16.0),
  153.                           child: Builder(
  154.                               builder: (context) => RaisedButton(
  155.                                     child: Text('Cadastrar'),
  156.                                     onPressed: () {
  157.                                       final String nome = _controladorNome.text;
  158.                                       final int quantidade = int.tryParse(
  159.                                           _controladorQuantidade.text);
  160.                                       final double valor = double.tryParse(
  161.                                           _controladorValor.text);
  162.                                       final Produto produtoNovo = Produto(
  163.                                           nome,
  164.                                           quantidade,
  165.                                           valor,
  166.                                           _controladorData.text);
  167.  
  168.                                       produtos.add(produtoNovo);
  169.  
  170.                                       _controladorNome.text = "";
  171.                                       _controladorQuantidade.text = "";
  172.                                       _controladorValor.text = "";
  173.                                       _controladorData.text = "";
  174.  
  175.                                       _showToast(context, "Produto adicionado");
  176.  
  177.                                       setState(() {});
  178.                                     },
  179.                                   )),
  180.                         )
  181.                       ],
  182.                     ),
  183.                     SingleChildScrollView(
  184.                         scrollDirection: Axis.vertical,
  185.                         child: SingleChildScrollView(
  186.                             scrollDirection: Axis.horizontal,
  187.                             child: Center(
  188.                               child: Padding(
  189.                                 padding: const EdgeInsets.only(top: 30.0),
  190.                                 child: DataTable(
  191.                                   sortAscending: sort,
  192.                                   sortColumnIndex: 0,
  193.                                   columns: [
  194.                                     DataColumn(
  195.                                         label: Text("Nome",
  196.                                             style: TextStyle(fontSize: 16)),
  197.                                         numeric: false,
  198.                                         onSort: (columnIndex, ascending) {
  199.                                           setState(() {
  200.                                             sort = !sort;
  201.                                           });
  202.                                           onSortColumn(columnIndex, ascending);
  203.                                         }),
  204.                                     DataColumn(
  205.                                       label: Text("Qtd",
  206.                                           style: TextStyle(fontSize: 16)),
  207.                                       numeric: true,
  208.                                     ),
  209.                                     DataColumn(
  210.                                       label: Text("Valor",
  211.                                           style: TextStyle(fontSize: 16)),
  212.                                       numeric: true,
  213.                                     ),
  214.                                     DataColumn(
  215.                                       label: Text("Data",
  216.                                           style: TextStyle(fontSize: 16)),
  217.                                       numeric: false,
  218.                                     ),
  219.                                     DataColumn(
  220.                                       label: Text("Delete",
  221.                                           style: TextStyle(fontSize: 16)),
  222.                                       numeric: false,
  223.                                     ),
  224.                                   ],
  225.                                   rows: produtos
  226.                                       .map(
  227.                                         (produto) => DataRow(cells: [
  228.                                           DataCell(
  229.                                             Text(produto.nome),
  230.                                           ),
  231.                                           DataCell(
  232.                                             Text(produto.quantidade.toString()),
  233.                                           ),
  234.                                           DataCell(
  235.                                             Text(produto.valor.toString()),
  236.                                           ),
  237.                                           DataCell(
  238.                                             Text(produto.data),
  239.                                           ),
  240.                                           DataCell(IconButton(
  241.                                             icon: Icon(Icons.delete),
  242.                                             onPressed: () {
  243.                                               _deleteRegister(produto);
  244.                                               setState(() {});
  245.                                             },
  246.                                           ))
  247.                                         ]),
  248.                                       )
  249.                                       .toList(),
  250.                                 ),
  251.                               ),
  252.                             )))
  253.                   ],
  254.                 ))));
  255.   }
  256. }
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement