Advertisement
danielbrito1987

categorias-page

Mar 30th, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 14.29 KB | None | 0 0
  1. import 'package:PhotoReport/blocs/categoria_bloc.dart';
  2. import 'package:PhotoReport/blocs/image_bloc.dart';
  3. import 'package:PhotoReport/helpers/categoria_helper.dart';
  4. import 'package:PhotoReport/helpers/obra_helper.dart';
  5. import 'package:PhotoReport/pages/add_relatorio.dart';
  6. import 'package:PhotoReport/pages/home_page.dart';
  7. import 'package:PhotoReport/pages/relatorios_page.dart';
  8. import 'package:PhotoReport/widgets/Drawer.dart';
  9. import 'package:PhotoReport/widgets/FabBottomAppBar.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  12. import 'package:shimmer/shimmer.dart';
  13. import 'camera.dart';
  14. import 'galeria_page.dart';
  15. import 'lista_categorias.dart';
  16.  
  17. class CategoriasPage extends StatefulWidget {
  18.   final Obra obra;
  19.   final PageController controller;
  20.  
  21.   CategoriasPage(this.controller, this.obra);
  22.  
  23.   @override
  24.   _CategoriasPageState createState() => _CategoriasPageState();
  25. }
  26.  
  27. class _CategoriasPageState extends State<CategoriasPage> {
  28.   Obra _obra;
  29.   PageController _pageCtrl;
  30.   PageController _pageCtrlCategorias = new PageController();
  31.   String title = "Obras";
  32.   int bottomSelectedIndex = 0;
  33.   CategoriaBloc _bloc;
  34.   int currentIndex = 0;
  35.  
  36.   @override
  37.   void initState() {
  38.     super.initState();
  39.  
  40.     _pageCtrl = widget.controller;
  41.  
  42.     _obra = widget.obra;
  43.     _bloc = CategoriaBloc(buildingId: _obra.id);
  44.  
  45.     _bloc.outState.listen((state) {
  46.       switch (state) {
  47.         case CategoriaState.FAIL:
  48.           showDialog(
  49.               context: context,
  50.               builder: (context) => AlertDialog(
  51.                     title: Text("Erro"),
  52.                     content: Text("Ocorreu um erro ao obter as obras!"),
  53.                   ));
  54.           break;
  55.         case CategoriaState.SUCCESS:
  56.         case CategoriaState.LOADING:
  57.           _buildLoadingWidget();
  58.           break;
  59.         case CategoriaState.IDLE:
  60.       }
  61.     });
  62.   }
  63.  
  64.   @override
  65.   void dispose() {
  66.     super.dispose();
  67.  
  68.     _bloc.dispose();
  69.   }
  70.  
  71.   void bottomTapped(int index) {
  72.     setState(() {
  73.       currentIndex = index;
  74.       switch (index) {
  75.         case 0:
  76.           setState(() {
  77.             _bloc = CategoriaBloc(buildingId: _obra.id);
  78.             title = _obra.nome;
  79.           });
  80.           break;
  81.         case 1:
  82.           setState(() {
  83.             title = "Galeria";
  84.             imageBloc.getImages(_obra.id);
  85.           });
  86.           break;
  87.         case 2:
  88.           setState(() {
  89.             title = "Relatórios";
  90.           });
  91.           break;
  92.       }
  93.       bottomSelectedIndex = index;
  94.       _pageCtrlCategorias.jumpToPage(index);
  95.     });
  96.   }
  97.  
  98.   void pageChanged(int index) {
  99.     setState(() {
  100.       bottomSelectedIndex = index;
  101.       currentIndex = index;
  102.     });
  103.   }
  104.  
  105.   @override
  106.   Widget build(BuildContext context) {
  107.     return WillPopScope(
  108.       child: Scaffold(
  109.         body: PageView(
  110.           controller: _pageCtrlCategorias,
  111.           physics: NeverScrollableScrollPhysics(),
  112.           children: <Widget>[
  113.             Scaffold(
  114.                 appBar: AppBar(
  115.                   title: Text(_obra.nome),
  116.                   centerTitle: true,
  117.                 ),
  118.                 drawer: DrawerPage(
  119.                   pageCtrl: _pageCtrl,
  120.                 ),
  121.                 body: StreamBuilder<CategoriaState>(
  122.                   stream: _bloc.outState,
  123.                   builder: (context, snapshot) {
  124.                     if (snapshot.data == CategoriaState.LOADING)
  125.                       return _buildLoadingWidget();
  126.  
  127.                     return _bloc.categorias.length > 0
  128.                         ? _createCardCategoria(_bloc.categorias)
  129.                         : Center(
  130.                             child: Container(
  131.                               padding: EdgeInsets.only(top: 10.0),
  132.                               child: Column(
  133.                                 mainAxisAlignment: MainAxisAlignment.start,
  134.                                 crossAxisAlignment: CrossAxisAlignment.center,
  135.                                 children: <Widget>[
  136.                                   Icon(Icons.cancel),
  137.                                   Text("Nenhuma categoria foi encontrada!")
  138.                                 ],
  139.                               ),
  140.                             ),
  141.                           );
  142.                   },
  143.                 )),
  144.             Scaffold(
  145.               appBar: AppBar(
  146.                 title: Text(_obra.nome),
  147.                 centerTitle: true,
  148.               ),
  149.               drawer: DrawerPage(
  150.                 pageCtrl: _pageCtrl,
  151.               ),
  152.               body: GaleriaPage(categorias: _bloc.categorias, obra: _obra),
  153.             ),
  154.             Scaffold(
  155.                 appBar: AppBar(
  156.                   title: Text(_obra.nome),
  157.                   centerTitle: true,
  158.                 ),
  159.                 drawer: DrawerPage(
  160.                   pageCtrl: _pageCtrl,
  161.                 ),
  162.                 body: RelatoriosPage(_obra))
  163.           ],
  164.         ),
  165.         floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
  166.         floatingActionButton: FloatingActionButton(
  167.           onPressed: () {
  168.             setState(() async {
  169.               if (currentIndex == 0) {
  170.                 await Navigator.of(context).push(MaterialPageRoute(
  171.                     builder: (context) => ListaCategoriasPage(_obra)));
  172.               } else if (currentIndex == 2) {
  173.                 await Navigator.of(context).push(MaterialPageRoute(
  174.                     builder: (context) => AddRelatorioPage(),
  175.                     fullscreenDialog: true));
  176.               }
  177.             });
  178.           },
  179.           child: Icon(Icons.add),
  180.         ),
  181.         bottomNavigationBar: FABBottomAppBar(
  182.           backgroundColor: Theme.of(context).primaryColor,
  183.           color: Colors.white,
  184.           selectedColor: Colors.yellow,
  185.           onTabSelected: bottomTapped,
  186.           notchedShape: CircularNotchedRectangle(),
  187.           items: [
  188.             FABBottomAppBarItem(iconData: Icons.photo_camera, text: 'Captura'),
  189.             FABBottomAppBarItem(iconData: Icons.photo_library, text: 'Galeria'),
  190.             FABBottomAppBarItem(
  191.                 iconData: Icons.insert_chart, text: 'Relatórios'),
  192.           ],
  193.         ),
  194.       ),
  195.       onWillPop: () {
  196.         if (_pageCtrlCategorias.page != 0) {
  197.           _pageCtrlCategorias.jumpToPage(0);
  198.           setState(() {
  199.             bottomSelectedIndex =
  200.                 int.parse(_pageCtrlCategorias.page.toString());
  201.             currentIndex = int.parse(_pageCtrlCategorias.page.toString());
  202.           });
  203.         } else
  204.           Navigator.of(context).pushReplacement(
  205.               MaterialPageRoute(builder: (context) => HomePage("categoria")));
  206.       },
  207.     );
  208.   }
  209.  
  210.   Widget _buildLoadingWidget() {
  211.     return Shimmer.fromColors(
  212.       baseColor: Colors.grey[300],
  213.       highlightColor: Colors.grey[100],
  214.       //enabled: snapshot.data.length,
  215.       child: ListView.builder(
  216.         padding: EdgeInsets.all(16.0),
  217.         shrinkWrap: true,
  218.         itemCount: 6,
  219.         itemBuilder: (_, __) => Padding(
  220.           padding: const EdgeInsets.only(bottom: 8.0),
  221.           child: Row(
  222.             crossAxisAlignment: CrossAxisAlignment.start,
  223.             children: <Widget>[
  224.               Expanded(
  225.                 child: Column(
  226.                   crossAxisAlignment: CrossAxisAlignment.start,
  227.                   children: <Widget>[
  228.                     Container(
  229.                       width: double.infinity,
  230.                       height: 8.0,
  231.                       color: Colors.white,
  232.                     ),
  233.                     const Padding(
  234.                       padding: EdgeInsets.symmetric(vertical: 2.0),
  235.                     ),
  236.                     Container(
  237.                       width: double.infinity,
  238.                       height: 8.0,
  239.                       color: Colors.white,
  240.                     ),
  241.                     const Padding(
  242.                       padding: EdgeInsets.symmetric(vertical: 2.0),
  243.                     ),
  244.                     Container(
  245.                       width: 40.0,
  246.                       height: 8.0,
  247.                       color: Colors.white,
  248.                     ),
  249.                   ],
  250.                 ),
  251.               )
  252.             ],
  253.           ),
  254.         ),
  255.       ),
  256.     );
  257.   }
  258.  
  259.   Widget _createCardCategoria(List<Categoria> categorias) {
  260.     return ListView.builder(
  261.       padding: EdgeInsets.all(10.0),
  262.       shrinkWrap: true,
  263.       itemCount: categorias.length,
  264.       itemBuilder: (context, index) {
  265.         return categorias.length > 0
  266.             ? GestureDetector(
  267.                 child: Card(
  268.                   clipBehavior: Clip.antiAlias,
  269.                   child: Column(
  270.                     crossAxisAlignment: CrossAxisAlignment.start,
  271.                     children: <Widget>[
  272.                       SizedBox(
  273.                         height: 124.0,
  274.                         child: Stack(
  275.                           children: <Widget>[
  276.                             DecoratedBox(
  277.                               decoration: BoxDecoration(
  278.                                 gradient: LinearGradient(
  279.                                   colors: <Color>[Colors.black, Colors.black38],
  280.                                 ),
  281.                               ),
  282.                               child: Image.asset(
  283.                                 'images/engenharia.jpg',
  284.                                 color:
  285.                                     const Color.fromRGBO(255, 255, 255, 0.190),
  286.                                 colorBlendMode: BlendMode.modulate,
  287.                                 fit: BoxFit.cover,
  288.                                 width: double.infinity,
  289.                               ),
  290.                             ),
  291.                             Padding(
  292.                               padding: EdgeInsets.only(top: 70.0),
  293.                               child: Container(
  294.                                 height: 60.0,
  295.                                 color: Colors.black.withOpacity(0.2),
  296.                               ),
  297.                             ),
  298.                             Padding(
  299.                               padding: const EdgeInsets.fromLTRB(
  300.                                   16.0, 16.0, 16.0, 0.0),
  301.                               child: DefaultTextStyle(
  302.                                 softWrap: false,
  303.                                 overflow: TextOverflow.ellipsis,
  304.                                 style: Theme.of(context).textTheme.subhead,
  305.                                 child: Column(
  306.                                   crossAxisAlignment: CrossAxisAlignment.start,
  307.                                   children: <Widget>[
  308.                                     // three line description
  309.                                     Padding(
  310.                                       padding: const EdgeInsets.only(
  311.                                           bottom: 2.0, top: 63),
  312.                                       child: Text(
  313.                                         categorias[index].nome,
  314.                                         style: Theme.of(context)
  315.                                             .textTheme
  316.                                             .title
  317.                                             .copyWith(color: Colors.white),
  318.                                       ),
  319.                                     ),
  320.                                   ],
  321.                                 ),
  322.                               ),
  323.                             ),
  324.                           ],
  325.                         ),
  326.                       ),
  327.                     ],
  328.                   ),
  329.                 ),
  330.                 onTap: () async {
  331.                   //await obraBloc.setActive(_obra);
  332.  
  333.                   await Navigator.of(context).push(MaterialPageRoute(
  334.                       builder: (context) => CameraPage(categorias[index])));
  335.                 },
  336.               )
  337.             : Container();
  338.       },
  339.     );
  340.   }
  341.  
  342.   Widget _buildPageView() {
  343.     return WillPopScope(
  344.         onWillPop: () {
  345.           if (_pageCtrlCategorias.page != 0) {
  346.             _pageCtrlCategorias.jumpToPage(0);
  347.             setState(() {
  348.               bottomSelectedIndex =
  349.                   int.parse(_pageCtrlCategorias.page.toString());
  350.               currentIndex = int.parse(_pageCtrlCategorias.page.toString());
  351.             });
  352.           } else
  353.             Navigator.of(context).pushReplacement(
  354.                 MaterialPageRoute(builder: (context) => HomePage("categoria")));
  355.         },
  356.         child: PageView(
  357.           physics: NeverScrollableScrollPhysics(),
  358.           controller: _pageCtrlCategorias,
  359.           children: <Widget>[
  360.             Scaffold(
  361.               appBar: AppBar(
  362.                 title: Text(_obra.nome),
  363.                 centerTitle: true,
  364.               ),
  365.               drawer: DrawerPage(
  366.                 pageCtrl: _pageCtrl,
  367.               ),
  368.               body: _bloc.categorias.length > 0
  369.                   ? _createCardCategoria(_bloc.categorias)
  370.                   : Center(
  371.                       child: Container(
  372.                         padding: EdgeInsets.only(top: 10.0),
  373.                         child: Column(
  374.                           mainAxisAlignment: MainAxisAlignment.start,
  375.                           crossAxisAlignment: CrossAxisAlignment.center,
  376.                           children: <Widget>[
  377.                             Icon(Icons.cancel),
  378.                             Text("Nenhuma categoria foi encontrada!")
  379.                           ],
  380.                         ),
  381.                       ),
  382.                     ),
  383.             ),
  384.             //_createCardGaleria(data.results),
  385.             Scaffold(
  386.               appBar: AppBar(
  387.                 title: Text(_obra.nome),
  388.                 centerTitle: true,
  389.               ),
  390.               drawer: DrawerPage(
  391.                 pageCtrl: _pageCtrl,
  392.               ),
  393.               body: GaleriaPage(categorias: _bloc.categorias, obra: _obra),
  394.             ),
  395.             Scaffold(
  396.                 appBar: AppBar(
  397.                   title: Text(_obra.nome),
  398.                   centerTitle: true,
  399.                 ),
  400.                 drawer: DrawerPage(
  401.                   pageCtrl: _pageCtrl,
  402.                 ),
  403.                 body: RelatoriosPage(_obra))
  404.           ],
  405.         ));
  406.   }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement