Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'Models/Produto.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: "Mercado",
- home: Mercado(),
- );
- }
- }
- class Mercado extends StatefulWidget {
- @override
- _MercadoState createState() => _MercadoState();
- }
- class _MercadoState extends State<Mercado> {
- final TextEditingController _controladorNome = TextEditingController();
- final TextEditingController _controladorQuantidade = TextEditingController();
- final TextEditingController _controladorValor = TextEditingController();
- final TextEditingController _controladorData = TextEditingController();
- final FocusNode fnNome = FocusNode();
- final FocusNode fnQtd = FocusNode();
- final FocusNode fnValor = FocusNode();
- final List<Produto> produtos = new List();
- bool sort;
- @override
- void initState() {
- sort = false;
- super.initState();
- }
- onSortColumn(int columnIndex, bool ascending) {
- if (columnIndex == 0) {
- if (ascending) {
- produtos.sort((a, b) => a.nome.compareTo(b.nome));
- } else {
- produtos.sort((a, b) => b.nome.compareTo(a.nome));
- }
- }
- }
- void _showToast(BuildContext context, String message) {
- final scaffold = Scaffold.of(context);
- if (message != null) {
- scaffold.showSnackBar(
- SnackBar(
- content: Text(message),
- action: SnackBarAction(
- label: 'FECHAR', onPressed: scaffold.hideCurrentSnackBar),
- ),
- );
- }
- }
- Future<void> _selectDate(context) async {
- DateTime selectedDate = await showDatePicker(
- context: context,
- initialDate: DateTime.now(),
- firstDate: DateTime(2017),
- lastDate: DateTime(2021));
- String resultado =
- "${selectedDate.day}/${selectedDate.month}/${selectedDate.year}";
- _controladorData.text = resultado;
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: DefaultTabController(
- length: 2,
- child: Scaffold(
- resizeToAvoidBottomPadding: false,
- appBar: AppBar(
- title: Text('Produtos'),
- bottom: TabBar(tabs: [
- Tab(icon: Icon(Icons.add)),
- Tab(icon: Icon(Icons.table_chart))
- ])),
- body: TabBarView(
- children: [
- Column(
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: TextFormField(
- controller: _controladorNome,
- decoration: InputDecoration(labelText: 'Nome'),
- textInputAction: TextInputAction.next,
- focusNode: fnNome,
- onFieldSubmitted: (term) {
- fnNome.unfocus();
- FocusScope.of(context).requestFocus(fnQtd);
- },
- )),
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: TextFormField(
- controller: _controladorQuantidade,
- decoration:
- InputDecoration(labelText: 'Quantidade'),
- keyboardType: TextInputType.number,
- textInputAction: TextInputAction.next,
- focusNode: fnQtd,
- onFieldSubmitted: (term) {
- fnQtd.unfocus();
- FocusScope.of(context).requestFocus(fnValor);
- })),
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: TextFormField(
- controller: _controladorValor,
- decoration: InputDecoration(labelText: 'Valor'),
- keyboardType: TextInputType.number,
- focusNode: fnValor,
- onFieldSubmitted: (term) {
- fnQtd.unfocus();
- })),
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Row(
- children: [
- Expanded(
- child: TextField(
- controller: _controladorData,
- enabled: false,
- decoration: InputDecoration(labelText: 'Data'),
- )),
- Expanded(
- child: RaisedButton(
- child: Text('Selecionar Data'),
- onPressed: () {
- _selectDate(context);
- }),
- )
- ],
- ),
- ),
- Padding(
- padding: const EdgeInsets.only(top: 16.0),
- child: Builder(
- builder: (context) => RaisedButton(
- child: Text('Cadastrar'),
- onPressed: () {
- final String nome = _controladorNome.text;
- final int quantidade = int.tryParse(
- _controladorQuantidade.text);
- final double valor = double.tryParse(
- _controladorValor.text);
- final Produto produtoNovo = Produto(
- nome,
- quantidade,
- valor,
- _controladorData.text);
- produtos.add(produtoNovo);
- _controladorNome.text = "";
- _controladorQuantidade.text = "";
- _controladorValor.text = "";
- _controladorData.text = "";
- _showToast(context, "Produto adicionado");
- setState(() {});
- },
- )),
- )
- ],
- ),
- SingleChildScrollView(
- scrollDirection: Axis.vertical,
- child: Center(
- child: Padding(
- padding: const EdgeInsets.only(top: 30.0),
- child: DataTable(
- sortAscending: sort,
- sortColumnIndex: 0,
- columns: [
- DataColumn(
- label: Text("Nome",
- style: TextStyle(fontSize: 16)),
- numeric: false,
- onSort: (columnIndex, ascending) {
- setState(() {
- sort = !sort;
- });
- onSortColumn(columnIndex, ascending);
- }),
- DataColumn(
- label: Text("Qtd",
- style: TextStyle(fontSize: 16)),
- numeric: true,
- ),
- DataColumn(
- label: Text("Valor",
- style: TextStyle(fontSize: 16)),
- numeric: true,
- ),
- DataColumn(
- label: Text("Data",
- style: TextStyle(fontSize: 16)),
- numeric: false,
- ),
- ],
- rows: produtos
- .map(
- (produto) => DataRow(cells: [
- DataCell(
- Text(produto.nome),
- ),
- DataCell(
- Text(produto.quantidade.toString()),
- ),
- DataCell(
- Text(produto.valor.toString()),
- ),
- DataCell(
- Text(produto.data),
- ),
- ]),
- )
- .toList(),
- ),
- ),
- ))
- ],
- ))));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement