diff --git a/lib/components/estate_card.dart b/lib/components/estate_card.dart index 89fca41..a75d738 100644 --- a/lib/components/estate_card.dart +++ b/lib/components/estate_card.dart @@ -1,3 +1,4 @@ +import 'package:caderneta_campo_digital/global/global.dart'; import 'package:caderneta_campo_digital/models/PropriedadeModel.dart'; import 'package:caderneta_campo_digital/pages/estate/estate_page.dart'; import 'package:caderneta_campo_digital/utils/utils.dart'; @@ -16,7 +17,7 @@ class EstateCard extends StatelessWidget { } String getQtdTalhoes() { - return 'Número de talhões: '+ estate.talhoes.length.toString(); + return 'Número de talhões: ' + estate.talhoes.length.toString(); } @override @@ -39,7 +40,10 @@ class EstateCard extends StatelessWidget { context, MaterialPageRoute( builder: (BuildContext context) { - return EstatePage(estate: estate); + return EstatePage( + estate: estate, + isProductorTheViewer: SharedInfo.actualUser.isProductor, + ); }, ), ); diff --git a/lib/components/topbar_arrow_back.dart b/lib/components/topbar_arrow_back.dart index 39b93ef..fc43f0c 100644 --- a/lib/components/topbar_arrow_back.dart +++ b/lib/components/topbar_arrow_back.dart @@ -1,3 +1,4 @@ +import 'package:caderneta_campo_digital/global/global.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; @@ -80,7 +81,7 @@ class TopbarArrowBack extends StatelessWidget implements PreferredSizeWidget { bottomRight: Radius.circular(20), ), ), - actions: hasActions + actions: hasActions && SharedInfo.actualUser.isProductor ? [ Padding( padding: const EdgeInsets.only(right: 10), @@ -128,7 +129,30 @@ class TopbarArrowBack extends StatelessWidget implements PreferredSizeWidget { ), ), ] - : [], + : hasActions + ? [ + Padding( + padding: const EdgeInsets.symmetric(horizontal: 5), + child: SizedBox( + width: size.width * 0.1, + height: size.height * 0.09, + child: MaterialButton( + padding: EdgeInsets.symmetric(horizontal: 5), + onPressed: () { + if (onPressedHistoric != null) { + onPressedHistoric!(); + } + }, + highlightColor: Colors.transparent, + child: Icon( + Icons.history_edu, + color: Color(0xFFFFFFFF), + ), + ), + ), + ), + ] + : [], ); } } diff --git a/lib/controllers/home_tecnico/home_tecnico_controller.dart b/lib/controllers/home_tecnico/home_tecnico_controller.dart new file mode 100644 index 0000000..309f069 --- /dev/null +++ b/lib/controllers/home_tecnico/home_tecnico_controller.dart @@ -0,0 +1,104 @@ +import 'package:caderneta_campo_digital/models/CulturaModel.dart'; +import 'package:caderneta_campo_digital/models/PlantioModel.dart'; +import 'package:caderneta_campo_digital/models/ProdutorModel.dart'; +import 'package:caderneta_campo_digital/models/PropriedadeModel.dart'; +import 'package:caderneta_campo_digital/models/TalhaoModel.dart'; +import 'package:caderneta_campo_digital/models/TecnicoModel.dart'; +import 'package:caderneta_campo_digital/services/home_tecnico/home_tecnico_service.dart'; +import 'package:dio/dio.dart'; + +class HomeTecnicoController { + HomeTecnicoService homeTecnicoService = HomeTecnicoService(); + bool loading = false; + List estates = []; + + Future getEstates() async { + Response response = await homeTecnicoService.getData(); + loading = false; + + if (response.data == null) { + return false; + } + + setData(response.data); + + return true; + } + + List getPlantios(array) { + List plantios = []; + + for (dynamic plantio in array) { + plantios.add( + PlantioModel( + plantio['idPlantio'], + CulturaModel( + plantio['cultura']['idCultura'], + plantio['cultura']['nome'], + ), + plantio['dataPlantio'], + plantio['estado'], + '', + false, + ), + ); + } + + return plantios; + } + + List getTalhoes(array) { + List talhoes = []; + + for (dynamic talhao in array) { + talhoes.add( + TalhaoModel( + talhao['idTalhao'], + talhao['idPropriedade'], + talhao['numero'], + getPlantios(talhao['plantio']), + ), + ); + } + + return talhoes; + } + + void setData(data) { + estates = []; + for (dynamic estate in data) { + var produtor = estate['produtor']; + TecnicoModel tecnico = estate['tecnico'] == null + ? TecnicoModel.nulo() + : TecnicoModel( + estate['tecnico']['usuario']['cpf'], + estate['tecnico']['usuario']['dataNascimento'], + estate['tecnico']['usuario']['telefone'], + estate['tecnico']['usuario']['nome'], + estate['tecnico']['crea'], + estate['tecnico']['formacao'], + ); + + estates.add(Propriedade( + estate['idPropriedade'], + estate['cep'], + estate['estado'], + estate['cidade'], + estate['bairro'], + estate['complemento'], + estate['numeroCasa'], + estate['hectares'], + estate['logradouro'], + ProdutorModel( + produtor['usuario']['cpf'], + produtor['usuario']['dataNascimento'], + produtor['usuario']['telefone'], + produtor['usuario']['nome'], + produtor['dap'], + ), + tecnico, + getTalhoes(estate['talhao']), + )); + } + } +} diff --git a/lib/models/PropriedadeModel.dart b/lib/models/PropriedadeModel.dart index 5278238..1b2ba6d 100644 --- a/lib/models/PropriedadeModel.dart +++ b/lib/models/PropriedadeModel.dart @@ -33,24 +33,30 @@ class Propriedade { this.talhoes, ); - Tuple2, List> getPlotsActive({required List plots}) { + Tuple2, List> getPlots({ + bool isProductorTheViewer = false, + }) { List activePlots = []; List nonActivePlots = []; bool isActive = false; - for (TalhaoModel plot in plots) { + for (TalhaoModel plot in talhoes) { isActive = false; for (PlantioModel plantation in plot.plantios) { - if(plantation.estado == "Plantado") { - plot.setButtonsToNotEmptyTalhao(); + if (plantation.estado == "Plantado") { + if (isProductorTheViewer) { + plot.setButtonsToNotEmptyTalhao(); + } activePlots.add(plot); isActive = true; break; } } - if(!isActive) { - plot.setButtonsToEmptyTalhao(); + if (!isActive) { + if (isProductorTheViewer) { + plot.setButtonsToEmptyTalhao(); + } nonActivePlots.add(plot); } } diff --git a/lib/pages/estate/estate_page.dart b/lib/pages/estate/estate_page.dart index d13a992..9a6741f 100644 --- a/lib/pages/estate/estate_page.dart +++ b/lib/pages/estate/estate_page.dart @@ -7,19 +7,23 @@ import 'package:tuple/tuple.dart'; class EstatePage extends StatefulWidget { final Propriedade estate; - const EstatePage({Key? key, required this.estate}) : super(key: key); + final bool isProductorTheViewer; + const EstatePage({ + Key? key, + required this.estate, + required this.isProductorTheViewer, + }) : super(key: key); @override State createState() => _EstatePageState(); } class _EstatePageState extends State { - @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; - Tuple2 plots = widget.estate.getPlotsActive(plots: widget.estate.talhoes); + Tuple2 plots = widget.estate.getPlots(isProductorTheViewer: widget.isProductorTheViewer); return Scaffold( appBar: TopbarArrowBack( @@ -27,32 +31,33 @@ class _EstatePageState extends State { hasActions: true, title: widget.estate.complemento, ), - body: widget.estate.talhoes.isNotEmpty ? SingleChildScrollView( - child: Column( - children: [ - PlotsList( - plots: plots.item1, - title: 'Ativos', - ), - Container( - margin: EdgeInsets.only( - top: 10, - bottom: 10, + body: widget.estate.talhoes.isNotEmpty + ? SingleChildScrollView( + child: Column( + children: [ + PlotsList( + plots: plots.item1, + title: 'Ativos', + ), + Container( + margin: EdgeInsets.only( + top: 10, + bottom: 10, + ), + width: size.width, + height: 1, + color: Color(0xFF000000), + ), + PlotsList( + plots: plots.item2, + title: 'Inativos', + ), + ], ), - width: size.width, - height: 1, - color: Color(0xFF000000), + ) + : Center( + child: Text('Não existem talhões', style: Utils.estateTextStyle), ), - PlotsList( - plots: plots.item2, - title: 'Inativos', - ), - ], - ), - ) - : Center( - child: Text('Não existem talhões', style: Utils.estateTextStyle), - ), ); } } diff --git a/lib/pages/home_tecnico/home_tecnico.dart b/lib/pages/home_tecnico/home_tecnico.dart index 0e7ffe5..611b144 100644 --- a/lib/pages/home_tecnico/home_tecnico.dart +++ b/lib/pages/home_tecnico/home_tecnico.dart @@ -1,9 +1,23 @@ +import 'package:caderneta_campo_digital/components/estate_card.dart'; +import 'package:caderneta_campo_digital/components/loading.dart'; import 'package:caderneta_campo_digital/components/topbar.dart'; +import 'package:caderneta_campo_digital/controllers/home_tecnico/home_tecnico_controller.dart'; import 'package:caderneta_campo_digital/pages/home_tecnico/components/pendency_card.dart'; +import 'package:caderneta_campo_digital/utils/utils.dart'; import 'package:flutter/material.dart'; -class HomeTecnicoPage extends StatelessWidget { + + +class HomeTecnicoPage extends StatefulWidget { const HomeTecnicoPage({Key? key}) : super(key: key); + + @override + State createState() => _HomeTecnicoPageState(); +} + +class _HomeTecnicoPageState extends State { + HomeTecnicoController homeTecnicoController = HomeTecnicoController(); + @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; @@ -12,28 +26,35 @@ class HomeTecnicoPage extends StatelessWidget { appBar: Topbar(topbarHeight: (size * 0.14)), body: SingleChildScrollView( child: Column( - children: const [ + children: [ PendencyCard(), - // EstateCard( - // estateName: "Propriedade 1", - // estateAddress: "ENDERECO", - // qtdPlot: 5, - // ), - // EstateCard( - // estateName: "Propriedade 1", - // estateAddress: "ENDERECO", - // qtdPlot: 5, - // ), - // EstateCard( - // estateName: "Propriedade 1", - // estateAddress: "ENDERECO", - // qtdPlot: 5, - // ), - // EstateCard( - // estateName: "Propriedade 1", - // estateAddress: "ENDERECO", - // qtdPlot: 5, - // ), + Container( + padding: EdgeInsets.symmetric(vertical: 4), + height: size.height * 0.64, + child: FutureBuilder( + future: homeTecnicoController.getEstates(), + builder: (context, snapshot) { + return snapshot.connectionState == ConnectionState.done + ? snapshot.data == true + ? ListView.builder( + padding: const EdgeInsets.all(8), + itemCount: homeTecnicoController.estates.length, + itemBuilder: (context, index) { + return EstateCard( + estate: homeTecnicoController.estates[index], + ); + }, + ) + : Center( + child: Text( + 'Não existem propriedades', + style: Utils.estateTextStyle, + ), + ) + : Loading(); + }, + ), + ), ], ), ), diff --git a/lib/services/home_tecnico/home_tecnico_service.dart b/lib/services/home_tecnico/home_tecnico_service.dart new file mode 100644 index 0000000..5757686 --- /dev/null +++ b/lib/services/home_tecnico/home_tecnico_service.dart @@ -0,0 +1,13 @@ +import 'package:caderneta_campo_digital/services/dio.dart'; +import 'package:dio/dio.dart'; + +class HomeTecnicoService { + Future getData() async { + + Response getResponse; + + getResponse = await DioClient.dioClient.fetch('propriedade/'); + + return getResponse; + } +}