From b2fa4e1586821bc4044f6dbd0c2255e34d5b4996 Mon Sep 17 00:00:00 2001 From: Willian <47341070+wjrcode@users.noreply.github.com> Date: Fri, 14 Oct 2022 17:57:28 -0300 Subject: [PATCH] Lista --- lib/Controller/ItemController.dart | 64 ++++++++++ lib/Model/API/APIModel.dart | 4 +- lib/Model/Lista/ListaModel.dart | 15 ++- .../dialogs/listas/listaCadastroDialog.dart | 16 ++- .../dialogs/listas/listaDialog.dart | 110 ++++++++---------- .../navigation/bottom_navigation_bar.dart | 21 ++-- lib/listas_page.dart | 64 +++------- 7 files changed, 168 insertions(+), 126 deletions(-) create mode 100644 lib/Controller/ItemController.dart diff --git a/lib/Controller/ItemController.dart b/lib/Controller/ItemController.dart new file mode 100644 index 0000000..41af76d --- /dev/null +++ b/lib/Controller/ItemController.dart @@ -0,0 +1,64 @@ +import 'dart:ffi'; + +import 'package:flutter/material.dart'; +import 'package:http/http.dart'; +import 'package:organizei/Model/Item/ItemModel.dart'; +import 'package:organizei/Controller/Base/Base.dart'; +import 'package:organizei/Repository/ItemRepository.dart'; +import 'package:organizei/services/persistencia/login.configuracoes.dart'; + +class ItemController extends Base { + ItemController(this.repository, this.context); + + final BuildContext context; + final ItemRepository repository; + final formKey = GlobalKey(); + var model = ItemModel(); + //var loginConfiguracoes = LoginConfiguracoes(); + + itemId(int? value) => model.id = value; + itemNome(String? value) => model.nome = value.toString(); + itemConcluido(bool? value) => model.concluido = value; + + var controllerNome = TextEditingController(); + + Future concluirItem(bool? concluido) async { + try { + return await repository + .concluirItem(model, concluido!) + .then((value) async { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + elevation: 6.0, + behavior: SnackBarBehavior.floating, + shape: RoundedRectangleBorder( + side: const BorderSide(color: Colors.black, width: 3), + borderRadius: BorderRadius.circular(16), + ), + content: Text( + value.msg!, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w500, + ), + ), + backgroundColor: value.valido! + ? const Color(0xFF74C198) + : const Color(0xFFEF7E69), + ), + ); + + await Future.delayed(const Duration(milliseconds: 500)); + + if (value.valido!) { + return value.valido!; + } else { + return false; + } + }); + } catch (e) { + print(e); + return false; + } + } +} diff --git a/lib/Model/API/APIModel.dart b/lib/Model/API/APIModel.dart index 45a7c22..4f9932a 100644 --- a/lib/Model/API/APIModel.dart +++ b/lib/Model/API/APIModel.dart @@ -1,6 +1,6 @@ class ApiModel { - //static const ApiUrl = "http://10.0.0.91:4444"; - static const ApiUrl = "http://192.168.100.74:4444"; + static const ApiUrl = "http://10.0.0.91:4444"; + //static const ApiUrl = "http://192.168.100.74:4444"; // static const ApiUrl = "http://189.10.2.165:4444"; static const String _token = "fd7bc88b7c0149adbc134d5d0a919814"; static const Map headers = { diff --git a/lib/Model/Lista/ListaModel.dart b/lib/Model/Lista/ListaModel.dart index 9bb5262..07554ef 100644 --- a/lib/Model/Lista/ListaModel.dart +++ b/lib/Model/Lista/ListaModel.dart @@ -19,7 +19,20 @@ class ListaModel { id = int.parse(json['id']); nome = json['nome']; cor = json['cor']; - itens = json['itens']; + + List listaItens = []; + json['item'].map((item) { + listaItens.add(ItemModel( + nome: item['nome'], + concluido: item['concluido'], + id: int.parse(item['id']))); + }).toList(); + + itens = listaItens; + + // _controllers.map((item) { + // listaitens.add(ItemModel(nome: (item.text))); + // }).toList(); } Map toJson() { diff --git a/lib/components/dialogs/listas/listaCadastroDialog.dart b/lib/components/dialogs/listas/listaCadastroDialog.dart index 2d44002..d562f6a 100644 --- a/lib/components/dialogs/listas/listaCadastroDialog.dart +++ b/lib/components/dialogs/listas/listaCadastroDialog.dart @@ -22,6 +22,16 @@ Future criarLista(BuildContext context, listaController.listaCor(lista.cor); listaController.listaId(lista.id); listaController.listaItens(lista.itens); + + itens = []; + + itens = lista.itens; + + itens!.map((item) { + var controllerNome = TextEditingController(); + controllerNome.text = (item!.nome ?? ''); + _controllers.add(controllerNome); + }).toList(); } return showDialog( @@ -40,7 +50,7 @@ Future criarLista(BuildContext context, key: listaController.formKey, child: Container( margin: const EdgeInsets.only(top: 24), - height: itens.length < 3 + height: itens!.length < 3 ? MediaQuery.of(context).size.height : null, child: DialogPersonalizado( @@ -75,7 +85,7 @@ Future criarLista(BuildContext context, excluir: true, funcao: () { setState(() { - itens.remove(itens[index]); + itens!.remove(itens[index]); _controllers.remove(_controllers[index]); }); }, @@ -89,7 +99,7 @@ Future criarLista(BuildContext context, cor: const Color(0xFF6BC8E4), clicar: () async { setState(() { - itens.add(ItemModel()); + itens!.add(ItemModel()); _controllers.add(new TextEditingController()); }); diff --git a/lib/components/dialogs/listas/listaDialog.dart b/lib/components/dialogs/listas/listaDialog.dart index d8e7e11..2fb0f7a 100644 --- a/lib/components/dialogs/listas/listaDialog.dart +++ b/lib/components/dialogs/listas/listaDialog.dart @@ -1,17 +1,22 @@ import 'package:flutter/material.dart'; -import 'package:organizei/Model/Tarefa/TarefaModel.dart'; -import 'package:organizei/Repository/TarefaRepository.dart'; +import 'package:organizei/Controller/ItemController.dart'; +import 'package:organizei/Model/Item/ItemModel.dart'; +import 'package:organizei/Model/Lista/ListaModel.dart'; +import 'package:organizei/Repository/ItemRepository.dart'; +import 'package:organizei/Repository/ListaRepository.dart'; import 'package:organizei/components/botao.dart'; import 'package:organizei/components/dialog_personalizado.dart'; -import 'package:organizei/components/dialogs/tarefas/tarefaCadastroDialog.dart'; -import '../../../Controller/TarefaController.dart'; +import 'package:organizei/components/dialogs/listas/listaCadastroDialog.dart'; +import '../../../Controller/ListaController.dart'; -Future visualizarTarefa(BuildContext context, - {required TarefaModel tarefa, Function? fecharDialog = null}) { - late TarefaController tarefaController; - tarefaController = TarefaController(TarefaRepository(), context); +Future visualizarLista(BuildContext context, + {required ListaModel lista, Function? fecharDialog = null}) { + late ListaController listaController; + late ItemController itemController; + listaController = ListaController(ListaRepository(), context); + itemController = ItemController(ItemRepository(), context); - tarefaController.tarefaId(tarefa.id); + listaController.listaId(lista.id); return showDialog( barrierDismissible: false, @@ -26,65 +31,52 @@ Future visualizarTarefa(BuildContext context, child: Material( type: MaterialType.transparency, child: Form( - key: tarefaController.formKey, + key: listaController.formKey, child: Container( height: MediaQuery.of(context).size.height, margin: const EdgeInsets.only(top: 24), child: DialogPersonalizado( - nome: tarefa.nome ?? '', - cor: tarefa.cor ?? '', + nome: lista.nome ?? '', + cor: lista.cor ?? '', child: [ - Text(tarefa.observacao ?? ''), - Row( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - const Text( - 'data: ', - style: TextStyle( - color: Colors.black, - fontWeight: FontWeight.w700, - ), - ), - Text(tarefa.data ?? ''), - ], - ), - Row( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - const Text( - 'prioridade: ', - style: TextStyle( - color: Colors.black, - fontWeight: FontWeight.w700, - ), - ), - Text(tarefa.prioridade ?? ''), - ], - ), - Padding( - padding: const EdgeInsets.only(bottom: 16, top: 16), - child: Botao( - texto: 'Concluir', - cor: const Color(0xFF74C198), - clicar: () async { - bool succes = - await tarefaController.concluirTarefa(true); + ListView.builder( + //primary: false, + shrinkWrap: true, + itemCount: lista.itens!.length, + itemBuilder: (context, index) { + var item = ItemModel( + id: lista.itens![index]!.id, + nome: lista.itens![index]!.nome, + concluido: lista.itens![index]!.concluido, + ); - if (succes == true) { - Navigator.pop(context); - fecharDialog!(); - } - }, - ), - ), + return Row( + children: [ + Checkbox( + value: lista.itens![index]!.concluido, + onChanged: (bool? value) async { + setState(() { + lista.itens![index]!.concluido = value!; + }); + itemController.itemId(item.id); + await itemController.concluirItem( + lista.itens![index]!.concluido); + }, + ), + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: Text( + lista.itens![index]!.nome.toString()), + ), + ], + ); + }), Botao( texto: 'Editar', cor: const Color(0xFF6385C3), clicar: () async { - criarTarefa(context, - tarefa: tarefa, fecharDialog: fecharDialog); + criarLista(context, + lista: lista, fecharDialog: fecharDialog); }, ), Padding( @@ -94,7 +86,7 @@ Future visualizarTarefa(BuildContext context, cor: const Color(0xFFEF7E69), clicar: () async { bool succes = - await tarefaController.excluirTarefa(); + await listaController.excluirLista(); if (succes == true) { Navigator.pop(context); diff --git a/lib/components/navigation/bottom_navigation_bar.dart b/lib/components/navigation/bottom_navigation_bar.dart index 04ad9e6..b9a23fc 100644 --- a/lib/components/navigation/bottom_navigation_bar.dart +++ b/lib/components/navigation/bottom_navigation_bar.dart @@ -6,10 +6,12 @@ import 'package:organizei/listas_page.dart'; import '../box.dart'; class ButtonNavigatorBar extends StatefulWidget { - const ButtonNavigatorBar({Key? key, this.fecharDialog = null}) + ButtonNavigatorBar( + {Key? key, this.fecharDialog = null, this.iconSelected = 'home'}) : super(key: key); final Function? fecharDialog; + late String? iconSelected; @override State createState() => _ButtonNavigatorBarState(); @@ -18,7 +20,6 @@ class ButtonNavigatorBar extends StatefulWidget { class _ButtonNavigatorBarState extends State with SingleTickerProviderStateMixin { bool addIcone = false; - String iconSelected = 'home'; Offset offset = const Offset(0, 2); @@ -70,13 +71,13 @@ class _ButtonNavigatorBarState extends State IconButton( icon: Icon( Icons.home_outlined, - color: iconSelected == 'home' + color: widget.iconSelected == 'home' ? const Color(0xFFF7BC36) : Colors.black, ), onPressed: () { setState(() { - iconSelected = 'home'; + widget.iconSelected = 'home'; }); Navigator.of(context).push( MaterialPageRoute( @@ -88,13 +89,13 @@ class _ButtonNavigatorBarState extends State IconButton( icon: Icon( Icons.bar_chart_outlined, - color: iconSelected == 'dashboard' + color: widget.iconSelected == 'dashboard' ? const Color(0xFFF7BC36) : Colors.black, ), onPressed: () { setState(() { - iconSelected = 'dashboard'; + widget.iconSelected = 'dashboard'; }); }, ), @@ -127,13 +128,13 @@ class _ButtonNavigatorBarState extends State IconButton( icon: Icon( Icons.list_outlined, - color: iconSelected == 'listas' + color: widget.iconSelected == 'listas' ? const Color(0xFFF7BC36) : Colors.black, ), onPressed: () { setState(() { - iconSelected = 'listas'; + widget.iconSelected = 'listas'; }); Navigator.push( context, @@ -150,13 +151,13 @@ class _ButtonNavigatorBarState extends State IconButton( icon: Icon( Icons.folder_outlined, - color: iconSelected == 'projetos' + color: widget.iconSelected == 'projetos' ? const Color(0xFFF7BC36) : Colors.black, ), onPressed: () { setState(() { - iconSelected = 'projetos'; + widget.iconSelected = 'projetos'; }); }, ), diff --git a/lib/listas_page.dart b/lib/listas_page.dart index 208a61e..b35fcf6 100644 --- a/lib/listas_page.dart +++ b/lib/listas_page.dart @@ -1,44 +1,12 @@ import 'package:flutter/material.dart'; -import 'package:http/retry.dart'; -import 'package:intl/date_symbol_data_local.dart'; import 'package:organizei/Controller/ListaController.dart'; -import 'package:organizei/Controller/TarefaController.dart'; -import 'package:organizei/Model/Habito/HabitoModel.dart'; import 'package:organizei/Model/Lista/ListaModel.dart'; -import 'package:organizei/Model/Tarefa/TarefaModel.dart'; import 'package:organizei/Repository/ListaRepository.dart'; -import 'package:organizei/Repository/TarefaRepository.dart'; import 'package:organizei/components/card_item.dart'; -import 'package:intl/intl.dart'; -import 'package:organizei/components/dialogs/habitos/habitoDialog.dart'; -import 'package:organizei/components/dialogs/tarefas/tarefaDialog.dart'; -import 'package:shared_preferences/shared_preferences.dart'; - -import 'components/box.dart'; +import 'package:organizei/components/dialogs/listas/listaDialog.dart'; import 'components/navigation/bottom_navigation_bar.dart'; import 'components/texto_contornado.dart'; -String getSaudacao() { - int horaAtual = new DateTime.now().hour; - - if (horaAtual > 4 && horaAtual < 13) { - return 'Bom dia,'; - } else if (horaAtual > 12 && horaAtual < 19) { - return 'Boa tarde,'; - } else { - return 'Boa Noite,'; - } -} - -String getDia() { - DateTime data = new DateTime.now(); - - Intl.defaultLocale = 'pt_BR'; - initializeDateFormatting('pt_BR'); - - return DateFormat.MMMMd().format(data); -} - class ListasPage extends StatefulWidget { const ListasPage({Key? key}) : super(key: key); @@ -49,20 +17,6 @@ class ListasPage extends StatefulWidget { class _HomePageState extends State { late String? apelido = ''; - @override - initState() { - getApelido(); - super.initState(); - } - - getApelido() async { - SharedPreferences prefs = await SharedPreferences.getInstance(); - - setState(() { - apelido = prefs.getString('UsuarioApelido'); - }); - } - @override Widget build(BuildContext context) { late ListaController listaController; @@ -131,9 +85,8 @@ class _HomePageState extends State { itemCount: snapshot.data['listas'].length, itemBuilder: (context, index) { var item = ListaModel.fromJson( - snapshot.data['listas'][index]); - - //var dialog; + snapshot.data['listas'][index], + ); return cardItem( cor: Color(int.tryParse( @@ -141,7 +94,15 @@ class _HomePageState extends State { 0), nome: item.nome, horario: '', - abrirDialog: () {}); + abrirDialog: () { + return visualizarLista( + context, + lista: item, + fecharDialog: () { + setState(() {}); + }, + ); + }); }); }), ]), @@ -153,6 +114,7 @@ class _HomePageState extends State { ), ), floatingActionButton: ButtonNavigatorBar( + iconSelected: 'listas', fecharDialog: () { setState(() {}); },