From b0e1bcb98c32f983378fe9a03d7152e2aad54931 Mon Sep 17 00:00:00 2001 From: wjrcode Date: Sat, 15 Oct 2022 10:18:06 -0300 Subject: [PATCH] lista e itens --- lib/Controller/ItemController.dart | 38 ++++ lib/Repository/ItemRepository.dart | 9 + .../dialogs/listas/listaCadastroDialog.dart | 175 +++++++++--------- .../dialogs/listas/listaDialog.dart | 14 +- lib/listas_page.dart | 2 +- 5 files changed, 151 insertions(+), 87 deletions(-) diff --git a/lib/Controller/ItemController.dart b/lib/Controller/ItemController.dart index 41af76d..1d18a4d 100644 --- a/lib/Controller/ItemController.dart +++ b/lib/Controller/ItemController.dart @@ -61,4 +61,42 @@ class ItemController extends Base { return false; } } + + Future excluirItem() async { + try { + return await repository.excluirItem(model).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/Repository/ItemRepository.dart b/lib/Repository/ItemRepository.dart index 31d78cd..966ea93 100644 --- a/lib/Repository/ItemRepository.dart +++ b/lib/Repository/ItemRepository.dart @@ -20,4 +20,13 @@ class ItemRepository { return ResponseAPIModel.fromJson(jsonDecode(response.body)); } + + Future excluirItem(ItemModel model) async { + final response = await http.delete( + Uri.parse(ApiModel.ApiUrl + '/item/' + model.id.toString()), + headers: ApiModel.headers, + ); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } } diff --git a/lib/components/dialogs/listas/listaCadastroDialog.dart b/lib/components/dialogs/listas/listaCadastroDialog.dart index f53cec0..4df611f 100644 --- a/lib/components/dialogs/listas/listaCadastroDialog.dart +++ b/lib/components/dialogs/listas/listaCadastroDialog.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.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'; @@ -12,6 +14,8 @@ Future criarLista(BuildContext context, {ListaModel? lista = null, Function? fecharDialog = null}) { late ListaController listaController; late List _controllers = [new TextEditingController()]; + late ItemController itemController; + itemController = ItemController(ItemRepository(), context); listaController = ListaController(ListaRepository(), context); @@ -50,97 +54,102 @@ Future criarLista(BuildContext context, type: MaterialType.transparency, child: Form( key: listaController.formKey, - child: Container( - margin: const EdgeInsets.only(top: 24), - height: itens!.length < 3 - ? MediaQuery.of(context).size.height - : null, - child: DialogPersonalizado( - nome: 'Lista', - child: [ - Padding( - padding: const EdgeInsets.only(bottom: 16), - child: input( - onSaved: listaController.listaNome, - textController: listaController.controllerNome, - label: 'nome', + child: Padding( + padding: const EdgeInsets.only(top: 24.0), + child: ConstrainedBox( + constraints: BoxConstraints( + minHeight: MediaQuery.of(context).size.height, + ), + child: DialogPersonalizado( + nome: 'Lista', + child: [ + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: input( + onSaved: listaController.listaNome, + textController: listaController.controllerNome, + label: 'nome', + ), ), - ), - SelectCor( - cor: listaController.listaCor, - corAtual: lista?.cor ?? ''), - ListView.builder( - primary: false, - shrinkWrap: true, - itemCount: itens.length, - itemBuilder: (context, index) { - //var dialog; - - // var controllerNomeItem = TextEditingController(); - - return Padding( - padding: const EdgeInsets.only(bottom: 16), - child: input( - //onSaved: listaController.listaNome, - textController: _controllers[index], - label: 'item', - excluir: true, - funcao: () { - setState(() { - itens!.remove(itens[index]); - _controllers.remove(_controllers[index]); - }); - }, - ), - ); - }), - Padding( - padding: const EdgeInsets.only(bottom: 16), - child: Botao( - texto: 'Novo item', - cor: const Color(0xFF6BC8E4), - clicar: () async { - setState(() { - itens!.add(ItemModel()); - _controllers.add(new TextEditingController()); - }); - }, - ), - ), - Padding( - padding: const EdgeInsets.only(bottom: 16), - child: Botao( - texto: 'Salvar', - cor: const Color(0xFF6385C3), - clicar: () async { - List? listaitens = []; - - _controllers.asMap().entries.map((item) { - listaitens.add( - ItemModel( - nome: (item.value.text), - concluido: false, - id: itens![item.key]!.id), + SelectCor( + cor: listaController.listaCor, + corAtual: lista?.cor ?? ''), + ListView.builder( + primary: false, + shrinkWrap: true, + itemCount: itens!.length, + itemBuilder: (context, index) { + return Padding( + padding: const EdgeInsets.only(bottom: 16), + child: input( + //onSaved: listaController.listaNome, + textController: _controllers[index], + label: 'item', + excluir: true, + funcao: () async { + if (itens![index]!.id != null) { + print('entrei no if'); + print(itens[index]!.id); + itemController.itemId(itens[index]!.id); + await itemController.excluirItem(); + } + setState(() { + itens!.remove(itens[index]); + _controllers + .remove(_controllers[index]); + }); + }, + ), ); - }).toList(); + }), + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: Botao( + texto: 'Novo item', + cor: const Color(0xFF6BC8E4), + clicar: () async { + setState(() { + itens!.add(ItemModel()); + _controllers.add(new TextEditingController()); + }); + }, + ), + ), + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: Botao( + texto: 'Salvar', + cor: const Color(0xFF6385C3), + clicar: () async { + List? listaitens = []; - listaController.listaItens(listaitens); - bool succes = await listaController.saveLista(); + _controllers.asMap().entries.map((item) { + listaitens.add( + ItemModel( + nome: (item.value.text), + concluido: false, + id: itens![item.key]!.id), + ); + }).toList(); - if (succes == true) { - var nav = Navigator.of(context); - nav.pop(); + listaController.listaItens(listaitens); + bool succes = await listaController.saveLista(); - if (lista?.id != null) { - //nav.pop(); + if (succes == true) { + var nav = Navigator.of(context); nav.pop(); + + if (lista?.id != null) { + //nav.pop(); + nav.pop(); + } + fecharDialog!(); } - fecharDialog!(); - } - }, + }, + ), ), - ), - ], + ], + ), ), ), ), diff --git a/lib/components/dialogs/listas/listaDialog.dart b/lib/components/dialogs/listas/listaDialog.dart index e404476..6f8e277 100644 --- a/lib/components/dialogs/listas/listaDialog.dart +++ b/lib/components/dialogs/listas/listaDialog.dart @@ -18,6 +18,10 @@ Future visualizarLista(BuildContext context, listaController.listaId(lista.id); + var _pageSize = MediaQuery.of(context).size.height; + var _notifySize = MediaQuery.of(context).padding.top; + //var _appBarSize = appBar.preferredSize.height; + return showDialog( barrierDismissible: false, barrierColor: Colors.white.withOpacity(0), @@ -33,15 +37,19 @@ Future visualizarLista(BuildContext context, child: Form( key: listaController.formKey, child: Container( + margin: const EdgeInsets.only(top: 24), + height: lista.itens!.length < 3 + ? MediaQuery.of(context).size.height + : null, // height: MediaQuery.of(context).size.height * // lista.itens!.length, - margin: const EdgeInsets.only(top: 24), + child: DialogPersonalizado( nome: lista.nome ?? '', cor: lista.cor ?? '', child: [ ListView.builder( - //primary: false, + primary: false, shrinkWrap: true, itemCount: lista.itens!.length, itemBuilder: (context, index) { @@ -56,7 +64,7 @@ Future visualizarLista(BuildContext context, crossAxisAlignment: CrossAxisAlignment.end, children: [ Padding( - padding: const EdgeInsets.only(bottom: 1.0), + padding: const EdgeInsets.only(bottom: 4.0), child: Checkbox( //tristate: true, activeColor: Color( diff --git a/lib/listas_page.dart b/lib/listas_page.dart index b35fcf6..49ed47b 100644 --- a/lib/listas_page.dart +++ b/lib/listas_page.dart @@ -72,7 +72,7 @@ class _HomePageState extends State { ); } - if (snapshot.data.length == 0) { + if (snapshot.data['listas'].length == 0) { return const Center( child: Text("Você não tem nada pra fazer hoje!"),