Advertisement
danielbrito1987

lista-categorias.dart

Apr 3rd, 2020
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.91 KB | None | 0 0
  1. import 'package:PhotoReport/blocs/categoria_bloc.dart';
  2. import 'package:PhotoReport/helpers/categoria_helper.dart';
  3. import 'package:PhotoReport/helpers/obra_helper.dart';
  4. import 'package:PhotoReport/widgets/CustomShowDialog.dart';
  5. import 'package:flutter/material.dart';
  6.  
  7. import 'add_categoria.dart';
  8.  
  9. class ListaCategoriasPage extends StatefulWidget {
  10.   final Obra obra;
  11.  
  12.   ListaCategoriasPage(this.obra);
  13.  
  14.   @override
  15.   _ListaCategoriasPageState createState() => _ListaCategoriasPageState();
  16. }
  17.  
  18. class _ListaCategoriasPageState extends State<ListaCategoriasPage> {
  19.   Obra obra;
  20.   CategoriaBloc _bloc;
  21.  
  22.   @override
  23.   void initState() {
  24.     super.initState();
  25.  
  26.     obra = widget.obra;
  27.     _bloc = CategoriaBloc(buildingId: obra.id);
  28.  
  29.     _bloc.outState.listen((state) {
  30.       switch (state) {
  31.         case CategoriaState.LOADING:
  32.           _buildLoadingWidget();
  33.           break;
  34.         case CategoriaState.SUCCESS:
  35.         case CategoriaState.IDLE:
  36.         case CategoriaState.FAIL:
  37.         default:
  38.           _buildingBody();
  39.           break;
  40.       }
  41.     });
  42.   }
  43.  
  44.   @override
  45.   Widget build(BuildContext context) {
  46.     return Scaffold(
  47.       appBar: AppBar(
  48.         title: Text("Categorias"),
  49.       ),
  50.       body: StreamBuilder<CategoriaState>(
  51.         stream: _bloc.outState,
  52.         builder: (context, snapshot) {
  53.           switch (snapshot.data) {
  54.             case CategoriaState.LOADING:
  55.               return _buildLoadingWidget();
  56.               break;
  57.             default:
  58.               return _buildingBody();
  59.               break;
  60.           }
  61.         },
  62.       ),
  63.       floatingActionButton: FloatingActionButton(
  64.         heroTag: "ListaCategorias",
  65.         onPressed: () async {
  66.           var resultado = await Navigator.of(context).push(MaterialPageRoute(
  67.               builder: (BuildContext context) => AddCategoriaPage(
  68.                     "add",
  69.                     obra: obra,
  70.                   ),
  71.               fullscreenDialog: true));
  72.  
  73.           if (resultado != null) {
  74.             setState(() {
  75.               _bloc.obterCategorias(obra.id);
  76.             });
  77.           }
  78.         },
  79.         child: Icon(Icons.add),
  80.       ),
  81.     );
  82.   }
  83.  
  84.   Widget _buildingBody() {
  85.     return ListView.builder(
  86.       itemCount: _bloc.categorias.length,
  87.       itemBuilder: (context, index) {
  88.         return _bloc.categorias.length > 0
  89.             ? _buildCategoriaWidget(index)
  90.             : Container();
  91.       },
  92.     );
  93.   }
  94.  
  95.   Widget _buildLoadingWidget() {
  96.     return Center(
  97.         child: Column(
  98.       mainAxisAlignment: MainAxisAlignment.center,
  99.       children: [
  100.         CircularProgressIndicator(),
  101.         SizedBox(
  102.           height: 10.0,
  103.         ),
  104.         Text("Carregando...")
  105.       ],
  106.     ));
  107.   }
  108.  
  109.   Widget _buildErrorWidget(String error) {
  110.     return Center(
  111.         child: Column(
  112.       mainAxisAlignment: MainAxisAlignment.center,
  113.       children: [
  114.         Text("Ocorreu um erro: $error"),
  115.       ],
  116.     ));
  117.   }
  118.  
  119.   Widget _buildCategoriaWidget(int index) {
  120.     return GestureDetector(
  121.       child: Card(
  122.         color: Theme.of(context).cardColor,
  123.         child: Padding(
  124.           padding: EdgeInsets.all(10.0),
  125.           child: Column(
  126.             crossAxisAlignment: CrossAxisAlignment.start,
  127.             children: <Widget>[
  128.               Text(_bloc.categorias[index].nome,
  129.                   style: Theme.of(context)
  130.                       .textTheme
  131.                       .title
  132.                       .copyWith(color: Colors.black)),
  133.               Text(
  134.                   _bloc.categorias[index].qtdImagens > 1
  135.                       ? "${_bloc.categorias[index].qtdImagens} imagens"
  136.                       : "${_bloc.categorias[index].qtdImagens} imagem",
  137.                   style: Theme.of(context)
  138.                       .textTheme
  139.                       .subtitle
  140.                       .copyWith(color: Colors.black))
  141.             ],
  142.           ),
  143.         ),
  144.       ),
  145.       onTap: () {
  146.         setState(() {
  147.           _settingModalBottomSheet(context, _bloc.categorias[index]);
  148.         });
  149.       },
  150.     );
  151.   }
  152.  
  153.   void _settingModalBottomSheet(BuildContext context, Categoria categoria) {
  154.     showModalBottomSheet(
  155.         context: context,
  156.         builder: (BuildContext bc) {
  157.           return Container(
  158.             child: new Wrap(
  159.               children: <Widget>[
  160.                 new ListTile(
  161.                     leading: new Icon(Icons.edit),
  162.                     title: new Text('Editar'),
  163.                     onTap: () async {
  164.                       setState(() async {
  165.                         await Navigator.of(context).push(MaterialPageRoute(
  166.                             builder: (context) => AddCategoriaPage("edit",
  167.                                 obra: obra, categoria: categoria)));
  168.                         Navigator.of(context).pop();
  169.                         setState(() {
  170.                           _bloc.getCategorias(obra.id);
  171.                         });
  172.                       });
  173.                     }),
  174.                 new ListTile(
  175.                   leading: new Icon(Icons.delete),
  176.                   title: new Text('Deletar'),
  177.                   onTap: () {
  178.                     Navigator.of(context).pop();
  179.  
  180.                     showDialog(
  181.                         context: context,
  182.                         barrierDismissible: false,
  183.                         builder: (BuildContext context) {
  184.                           return WillPopScope(
  185.                             onWillPop: () => Future(() => false),
  186.                             child: CustomAlertDialog(
  187.                               title: Text("Atenção!"),
  188.                               content: Text(
  189.                                   "Deseja excluir a categoria selecionada?"),
  190.                               actions: <Widget>[
  191.                                 FlatButton(
  192.                                   child: Text("Sim"),
  193.                                   onPressed: () {
  194.                                     setState(() async {
  195.                                       Navigator.of(context).pop();
  196.                                       setState(() {
  197.                                         _bloc.deletar(categoria);
  198.                                       });
  199.                                       //await categoriaBloc.getCategorias(obra.id);
  200.                                     });
  201.                                   },
  202.                                 ),
  203.                                 FlatButton(
  204.                                   child: Text("Não"),
  205.                                   onPressed: () {
  206.                                     Navigator.of(context).pop();
  207.                                   },
  208.                                 ),
  209.                               ],
  210.                             ),
  211.                           );
  212.                         });
  213.                   },
  214.                 ),
  215.               ],
  216.             ),
  217.           );
  218.         });
  219.   }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement