From f95ac22666547f57c106bdd651c2d8bc7a90f4b8 Mon Sep 17 00:00:00 2001 From: Willian <47341070+wjrcode@users.noreply.github.com> Date: Thu, 29 Sep 2022 17:40:06 -0300 Subject: [PATCH] ai mexi em mt coisa viu --- lib/Controller/TarefaController.dart | 78 +++++-- lib/Repository/TarefaRepository.dart | 33 +++ lib/components/card_item.dart | 6 +- lib/components/dialog_personalizado.dart | 2 +- .../dialogs/tarefaCadastroDialog.dart | 95 ++++++++ lib/components/dialogs/tarefaDialog.dart | 56 +++-- .../navigation/bottom_navigation_bar.dart | 10 +- lib/components/navigation/menu.dart | 14 +- lib/components/selectCor.dart | 11 +- lib/components/selectPrioridade.dart | 11 +- lib/home_page.dart | 205 ++++++++++-------- 11 files changed, 371 insertions(+), 150 deletions(-) create mode 100644 lib/components/dialogs/tarefaCadastroDialog.dart diff --git a/lib/Controller/TarefaController.dart b/lib/Controller/TarefaController.dart index 5b2b0b0..540a698 100644 --- a/lib/Controller/TarefaController.dart +++ b/lib/Controller/TarefaController.dart @@ -1,4 +1,7 @@ +import 'dart:ffi'; + import 'package:flutter/material.dart'; +import 'package:http/http.dart'; import 'package:organizei/Model/Tarefa/TarefaModel.dart'; import 'package:organizei/Controller/Base/Base.dart'; import 'package:organizei/Repository/TarefaRepository.dart'; @@ -13,6 +16,7 @@ class TarefaController extends Base { var model = TarefaModel(); //var loginConfiguracoes = LoginConfiguracoes(); + tarefaId(int? value) => model.id = value; tarefaNome(String? value) => model.nome = value.toString(); tarefaDataehora(String? value) => model.data = value.toString(); tarefaObservacao(String? value) => model.observacao = value.toString(); @@ -35,24 +39,28 @@ class TarefaController extends Base { try { if (model.id == null) { return await repository.addTarefa(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!, + 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), - )); + ), + ), + backgroundColor: value.valido! + ? const Color(0xFF74C198) + : const Color(0xFFEF7E69), + ), + ); - await Future.delayed(const Duration(seconds: 1)); + await Future.delayed(const Duration(milliseconds: 500)); if (value.valido!) { return value.valido!; @@ -61,18 +69,44 @@ class TarefaController extends Base { } }); } else { - /*return await repository.updateProduto(model).then((value) { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text(value.msg), - backgroundColor: value.valido ? Colors.green : Colors.red, - )); - return value.valido; - });*/ - return false; + return await repository.updateTarefa(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; } } + + Future?> getTarefas() async { + return await repository.getTarefas(); + } } diff --git a/lib/Repository/TarefaRepository.dart b/lib/Repository/TarefaRepository.dart index 2d40bf3..3fec0f0 100644 --- a/lib/Repository/TarefaRepository.dart +++ b/lib/Repository/TarefaRepository.dart @@ -21,4 +21,37 @@ class TarefaRepository { return ResponseAPIModel.fromJson(jsonDecode(response.body)); } + + Future updateTarefa(TarefaModel model) async { + var json = { + "nome": model.nome, + "data": model.data, + "observacao": model.observacao, + "prioridade": model.prioridade, + "cor": model.cor, + }; + + print(ApiModel.ApiUrl + '/tarefas/' + model.id.toString()); + + final response = await http.put( + Uri.parse(ApiModel.ApiUrl + '/tarefas/' + model.id.toString()), + headers: ApiModel.headers, + body: jsonEncode(json)); + + return ResponseAPIModel.fromJson(jsonDecode(response.body)); + } + + Future> getTarefas() async { + Uri _uriSearchProduto = Uri.parse(ApiModel.ApiUrl + '/tarefas'); + + var _url = Uri.parse(_uriSearchProduto.toString()); + final response = await http.get(_url, headers: ApiModel.headers); + + Map jsonMap = jsonDecode(response.body); + List listaProdutos = (jsonMap['tarefas'] as List) + .map((item) => TarefaModel.fromJson(item)) + .toList(); + + return listaProdutos; + } } diff --git a/lib/components/card_item.dart b/lib/components/card_item.dart index cd76309..6b2fbe4 100644 --- a/lib/components/card_item.dart +++ b/lib/components/card_item.dart @@ -6,8 +6,10 @@ class cardItem extends StatelessWidget { final Color? cor; final String? horario; final String? nome; + final Function? abrirDialog; - const cardItem({Key? key, this.cor, this.horario, this.nome}) + const cardItem( + {Key? key, this.cor, this.horario, this.nome, this.abrirDialog}) : super(key: key); String formatarNome() { @@ -37,7 +39,7 @@ class cardItem extends StatelessWidget { borderRadius: BorderRadius.all(Radius.circular(21)), child: GestureDetector( onTap: () { - debugPrint(nome); + abrirDialog!(); }, child: Row( children: [ diff --git a/lib/components/dialog_personalizado.dart b/lib/components/dialog_personalizado.dart index 44cdaba..0417379 100644 --- a/lib/components/dialog_personalizado.dart +++ b/lib/components/dialog_personalizado.dart @@ -67,7 +67,7 @@ class _DialogPersonalizadoState extends State child: const Icon(Icons.close), onTap: () { _slideDown(); - Future.delayed(Duration(milliseconds: 500)) + Future.delayed(const Duration(milliseconds: 500)) .then((value) => Navigator.pop(context)); }, ), diff --git a/lib/components/dialogs/tarefaCadastroDialog.dart b/lib/components/dialogs/tarefaCadastroDialog.dart new file mode 100644 index 0000000..a3b22a6 --- /dev/null +++ b/lib/components/dialogs/tarefaCadastroDialog.dart @@ -0,0 +1,95 @@ +import 'package:flutter/material.dart'; +import 'package:organizei/Model/Tarefa/TarefaModel.dart'; +import 'package:organizei/Repository/TarefaRepository.dart'; +import 'package:organizei/components/botao.dart'; +import 'package:organizei/components/dialog_personalizado.dart'; +import 'package:organizei/components/input.dart'; +import 'package:organizei/components/selectCor.dart'; +import 'package:organizei/components/selectData.dart'; +import 'package:organizei/components/selectPrioridade.dart'; +import '../../Controller/TarefaController.dart'; + +Future criarTarefa(BuildContext context, + {TarefaModel? tarefa = null, Function? fecharDialog = null}) { + late TarefaController tarefaController; + //setState(() { + tarefaController = TarefaController(TarefaRepository(), context); + + if (tarefa != null) { + tarefaController.controllerNome.text = tarefa.nome ?? ''; + tarefaController.controllerDataehora.text = tarefa.data ?? ''; + tarefaController.controllerObservacao.text = tarefa.observacao ?? ''; + tarefaController.tarefaPrioridade(tarefa.prioridade); + tarefaController.tarefaCor(tarefa.cor); + tarefaController.tarefaId(tarefa.id); + } + + // }); + + return showDialog( + barrierDismissible: false, + barrierColor: Colors.white.withOpacity(0), + context: context, + builder: (context) { + return StatefulBuilder(builder: (context, StateSetter setState) { + return Scaffold( + backgroundColor: Colors.transparent, + body: SingleChildScrollView( + scrollDirection: Axis.vertical, + child: Material( + type: MaterialType.transparency, + child: Form( + key: tarefaController.formKey, + child: Container( + margin: const EdgeInsets.only(top: 24), + child: DialogPersonalizado( + nome: 'Tarefa', + child: [ + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: input( + onSaved: tarefaController.tarefaNome, + textController: tarefaController.controllerNome, + label: 'nome', + ), + ), + SelectData( + controller: tarefaController.controllerDataehora, + onSaved: tarefaController.tarefaDataehora), + Padding( + padding: const EdgeInsets.only(bottom: 16), + child: input( + onSaved: tarefaController.tarefaObservacao, + textController: + tarefaController.controllerObservacao, + label: 'observação', + ), + ), + SelectPrioridade( + prioridade: tarefaController.tarefaPrioridade, + prioridadeAtual: tarefa?.prioridade ?? ''), + SelectCor( + cor: tarefaController.tarefaCor, + corAtual: tarefa?.cor ?? ''), + Botao( + texto: 'Salvar', + cor: const Color(0xFF6385C3), + clicar: () async { + bool succes = await tarefaController.saveTarefa(); + + if (succes == true) { + Navigator.pop(context); + fecharDialog!(); + } + }, + ), + ], + ), + ), + ), + ), + ), + ); + }); + }); +} diff --git a/lib/components/dialogs/tarefaDialog.dart b/lib/components/dialogs/tarefaDialog.dart index 3b32340..8aade9e 100644 --- a/lib/components/dialogs/tarefaDialog.dart +++ b/lib/components/dialogs/tarefaDialog.dart @@ -1,17 +1,21 @@ import 'package:flutter/material.dart'; +import 'package:organizei/Model/Tarefa/TarefaModel.dart'; import 'package:organizei/Repository/TarefaRepository.dart'; import 'package:organizei/components/botao.dart'; import 'package:organizei/components/dialog_personalizado.dart'; +import 'package:organizei/components/dialogs/tarefaCadastroDialog.dart'; import 'package:organizei/components/input.dart'; import 'package:organizei/components/selectCor.dart'; import 'package:organizei/components/selectData.dart'; import 'package:organizei/components/selectPrioridade.dart'; import '../../Controller/TarefaController.dart'; -Future criarTarefa(BuildContext context) { +Future visualizarTarefa(BuildContext context, + {required TarefaModel tarefa, Function? fecharDialog = null}) { late TarefaController tarefaController; //setState(() { tarefaController = TarefaController(TarefaRepository(), context); + // }); return showDialog( @@ -29,42 +33,36 @@ Future criarTarefa(BuildContext context) { child: Form( key: tarefaController.formKey, child: Container( + height: MediaQuery.of(context).size.height, margin: const EdgeInsets.only(top: 24), child: DialogPersonalizado( - nome: 'Tarefa', + nome: tarefa.nome ?? '', child: [ - Padding( - padding: const EdgeInsets.only(bottom: 16), - child: input( - onSaved: tarefaController.tarefaNome, - textController: tarefaController.controllerNome, - label: 'nome', - ), - ), - SelectData( - controller: tarefaController.controllerDataehora, - onSaved: tarefaController.tarefaDataehora), - Padding( - padding: const EdgeInsets.only(bottom: 16), - child: input( - onSaved: tarefaController.tarefaObservacao, - textController: - tarefaController.controllerObservacao, - label: 'observação', - ), + Botao( + texto: 'Concluir', + cor: const Color(0xFF74C198), + clicar: () async { + // bool succes = await tarefaController.saveTarefa(); + + // if (succes == true) { + // Navigator.pop(context); + // fecharDialog!(); + // } + }, ), - SelectPrioridade( - prioridade: tarefaController.tarefaPrioridade), - SelectCor(cor: tarefaController.tarefaCor), Botao( - texto: 'Cadastrar', + texto: 'Editar', cor: const Color(0xFF6385C3), clicar: () async { - bool succes = await tarefaController.saveTarefa(); + Navigator.pop(context); + criarTarefa(context, tarefa: tarefa); + + // bool succes = await tarefaController.saveTarefa(); - if (succes == true) { - Navigator.pop(context); - } + // if (succes == true) { + // Navigator.pop(context); + // fecharDialog!(); + // } }, ), ], diff --git a/lib/components/navigation/bottom_navigation_bar.dart b/lib/components/navigation/bottom_navigation_bar.dart index 9b0588c..0f88124 100644 --- a/lib/components/navigation/bottom_navigation_bar.dart +++ b/lib/components/navigation/bottom_navigation_bar.dart @@ -4,7 +4,10 @@ import 'package:organizei/components/navigation/menu.dart'; import '../box.dart'; class ButtonNavigatorBar extends StatefulWidget { - const ButtonNavigatorBar({Key? key}) : super(key: key); + const ButtonNavigatorBar({Key? key, this.fecharDialog = null}) + : super(key: key); + + final Function? fecharDialog; @override State createState() => _ButtonNavigatorBarState(); @@ -49,7 +52,10 @@ class _ButtonNavigatorBarState extends State offset: offset, duration: const Duration(milliseconds: 250), curve: Curves.easeInOut, - child: Container(child: Menu(fecharMenu: _slideDown)), + child: Container( + child: Menu( + fecharMenu: _slideDown, + fecharDialog: widget.fecharDialog)), ), Box( radius: 30, diff --git a/lib/components/navigation/menu.dart b/lib/components/navigation/menu.dart index c640c51..396f5e2 100644 --- a/lib/components/navigation/menu.dart +++ b/lib/components/navigation/menu.dart @@ -1,11 +1,13 @@ import 'package:flutter/material.dart'; import 'package:organizei/components/dialog_personalizado.dart'; -import 'package:organizei/components/dialogs/tarefaDialog.dart'; +import 'package:organizei/components/dialogs/tarefaCadastroDialog.dart'; class Menu extends StatefulWidget { - const Menu({Key? key, this.fecharMenu}) : super(key: key); + const Menu({Key? key, this.fecharMenu, this.fecharDialog = null}) + : super(key: key); - final fecharMenu; + final Function? fecharMenu; + final Function? fecharDialog; @override State createState() => _MenuState(); @@ -39,7 +41,7 @@ class _MenuState extends State { children: [ GestureDetector( onTap: () { - widget.fecharMenu(); + widget.fecharMenu!(); showDialog( barrierDismissible: false, barrierColor: Colors.white.withOpacity(0), @@ -169,8 +171,8 @@ class _MenuState extends State { ), GestureDetector( onTap: () { - criarTarefa(context); - widget.fecharMenu(); + criarTarefa(context, fecharDialog: widget.fecharDialog); + widget.fecharMenu!(); }, child: AbsorbPointer( child: Row( diff --git a/lib/components/selectCor.dart b/lib/components/selectCor.dart index 4c4fb70..2fcb652 100644 --- a/lib/components/selectCor.dart +++ b/lib/components/selectCor.dart @@ -3,7 +3,9 @@ import 'package:organizei/Controller/TarefaController.dart'; class SelectCor extends StatefulWidget { final dynamic cor; - const SelectCor({Key? key, required this.cor}) : super(key: key); + final String corAtual; + const SelectCor({Key? key, required this.cor, this.corAtual = ''}) + : super(key: key); @override State createState() => _SelectCorState(); @@ -20,6 +22,13 @@ class _SelectCorState extends State { late int corSelected = 0; + @override + void initState() { + super.initState(); + corSelected = + widget.corAtual != '' ? int.tryParse(widget.corAtual) ?? 0 : 0; + } + @override Widget build(BuildContext context) { return Column( diff --git a/lib/components/selectPrioridade.dart b/lib/components/selectPrioridade.dart index 2abc208..73fe921 100644 --- a/lib/components/selectPrioridade.dart +++ b/lib/components/selectPrioridade.dart @@ -6,7 +6,9 @@ const List list = ['baixa', 'média', 'alta']; class SelectPrioridade extends StatefulWidget { final dynamic prioridade; - const SelectPrioridade({Key? key, this.prioridade}) : super(key: key); + final String prioridadeAtual; + const SelectPrioridade({Key? key, this.prioridade, this.prioridadeAtual = ''}) + : super(key: key); @override State createState() => _DropdownButtonExampleState(); @@ -15,6 +17,13 @@ class SelectPrioridade extends StatefulWidget { class _DropdownButtonExampleState extends State { String dropdownValue = list.first; + @override + void initState() { + super.initState(); + dropdownValue = + widget.prioridadeAtual != '' ? widget.prioridadeAtual : list.first; + } + @override Widget build(BuildContext context) { return Column( diff --git a/lib/home_page.dart b/lib/home_page.dart index 044dda9..f35e09f 100644 --- a/lib/home_page.dart +++ b/lib/home_page.dart @@ -1,7 +1,12 @@ import 'package:flutter/material.dart'; import 'package:intl/date_symbol_data_local.dart'; +import 'package:organizei/Controller/TarefaController.dart'; +import 'package:organizei/Model/Tarefa/TarefaModel.dart'; +import 'package:organizei/Repository/TarefaRepository.dart'; import 'package:organizei/components/card_item.dart'; import 'package:intl/intl.dart'; +import 'package:organizei/components/dialogs/tarefaCadastroDialog.dart'; +import 'package:organizei/components/dialogs/tarefaDialog.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'components/box.dart'; @@ -40,45 +45,49 @@ class HomePage extends StatefulWidget { class _HomePageState extends State { @override Widget build(BuildContext context) { + late TarefaController tarefaController; + tarefaController = TarefaController(TarefaRepository(), context); + return Scaffold( resizeToAvoidBottomInset: false, floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, extendBody: true, - backgroundColor: Color(0xFF6BC8E4), - // appBar: AppBar( - // title: Text(widget.title), - // ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Expanded( - child: ListView( + backgroundColor: const Color(0xFF6BC8E4), + body: SingleChildScrollView( + scrollDirection: Axis.vertical, + child: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Column( children: [ Padding( padding: const EdgeInsets.fromLTRB(24.0, 8.0, 24.0, 16.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - getSaudacao(), - style: const TextStyle( - color: Colors.black, - fontSize: 32, - fontWeight: FontWeight.w300, + Padding( + padding: const EdgeInsets.only(top: 32.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + getSaudacao(), + style: const TextStyle( + color: Colors.black, + fontSize: 32, + fontWeight: FontWeight.w300, + ), ), - ), - TextoContornado( - texto: widget.apelido!, - tamanho: 32, - cor: Color(0xFFF7BC36), - ) - ], + TextoContornado( + texto: widget.apelido!, + tamanho: 32, + cor: const Color(0xFFF7BC36), + ) + ], + ), ), Column( mainAxisAlignment: MainAxisAlignment.end, @@ -108,76 +117,100 @@ class _HomePageState extends State { child: Padding( padding: const EdgeInsets.fromLTRB(16.0, 32.0, 16.0, 0.0), - child: Column( - children: [ - Padding( - padding: EdgeInsets.all(8.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - TextoContornado( - texto: 'Hoje ', - tamanho: 32, - cor: Color(0xFF6385C3), - ), - Text( - ' ' + getDia(), - style: TextStyle( - color: Colors.black, - fontSize: 16, - fontWeight: FontWeight.w300, - ), + child: Column(children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + const TextoContornado( + texto: 'Hoje ', + tamanho: 32, + cor: Color(0xFF6385C3), + ), + Text( + ' ' + getDia(), + style: const TextStyle( + color: Colors.black, + fontSize: 16, + fontWeight: FontWeight.w300, ), - ], - ), - ), - cardItem( - cor: Color(0xFF6385C3), - nome: 'Correr', - horario: '09:30 até às 10:00', - ), - cardItem( - cor: Color(0xFFEF7E69), - nome: 'Estudar inglês', - horario: '13:00 até às 15:00', - ), - cardItem( - cor: Color(0xFF6BC8E4), - nome: 'Tomar vitamina D e vitamna F', - horario: '15:00', - ), - cardItem( - cor: Color(0xFFF7BC36), - nome: 'Pular corda', - horario: '07:30 até às 09:00', - ), - cardItem( - cor: Color(0xFF74C198), - nome: 'Pular corda', - horario: '07:30 até às 09:00', - ), - cardItem( - cor: Color(0xFF6385C3), - nome: 'Pular corda', - horario: '07:30 até às 09:00', + ), + ], ), - ], - ), + ), + FutureBuilder( + future: tarefaController.getTarefas(), + builder: (context, AsyncSnapshot snapshot) { + if (!snapshot.hasData) { + return const Center( + child: CircularProgressIndicator()); + } + + if (snapshot.hasError) { + return const Center( + child: Text('Ocorreu um erro!'), + ); + } + + if (snapshot.data.length == 0) { + return const Center( + child: Text( + "Você não tem nada pra fazer hoje!"), + ); + } + + return ListView.builder( + primary: false, + shrinkWrap: true, + itemCount: snapshot.data.length, + itemBuilder: (context, index) { + TarefaModel item = snapshot.data[index]; + return cardItem( + cor: Color(int.tryParse( + item.cor ?? '0xFF6385C3') ?? + 0), + nome: item.nome, + horario: item.data, + abrirDialog: () { + visualizarTarefa( + context, + tarefa: item, + fecharDialog: () { + setState(() {}); + }, + ); + // criarTarefa( + // context, + // tarefa: item, + // fecharDialog: () { + // setState(() {}); + // }, + // ); + }); + }); + }), + ]), ), - ), + ) ], ), - ), - ], + ], + ), ), ), + // floatingActionButton: FloatingActionButton( // onPressed: _incrementCounter, // tooltip: 'Increment', // child: const Icon(Icons.add), // ), - floatingActionButton: ButtonNavigatorBar() + floatingActionButton: ButtonNavigatorBar( + fecharDialog: () { + setState(() {}); + }, + ) // ); }