Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 15.83 KB | None | 0 0
  1. import 'package:app_esdudez/controller/appTema.dart';
  2. import 'package:app_esdudez/controller/sizeConfig.dart';
  3. import 'package:app_esdudez/controller/stringsStatics.dart';
  4. import 'package:app_esdudez/models/infoMaterias_data.dart';
  5. import 'package:app_esdudez/widgets/customDialogAdd.dart';
  6. import 'package:cloud_firestore/cloud_firestore.dart';
  7. import 'package:flutter/material.dart';
  8. import 'dart:math';
  9. import 'dart:convert';
  10. import 'package:random_color/random_color.dart';
  11. import 'escolhaordemcicloView.dart';
  12. import 'package:app_esdudez/widgets/bordaContainer.dart';
  13.  
  14. class EscolhaMateriaTab extends StatefulWidget {
  15.   EscolhaMateriaTab({this.idMateriasArea});
  16.   final List<dynamic> idMateriasArea;
  17.  
  18.   @override
  19.   _EscolhaMateriaTab createState() => _EscolhaMateriaTab();
  20. }
  21.  
  22. class _EscolhaMateriaTab extends State<EscolhaMateriaTab> {
  23.   List<InfoMateriasData> listaMateriasArea = List<InfoMateriasData>();
  24.   Map<String, bool> values = Map();
  25.   final _scaffoldKey = GlobalKey<ScaffoldState>();
  26.   int controle = 1;
  27.  
  28.   @override
  29.   Widget build(BuildContext context) {
  30.     return Scaffold(
  31.         key: _scaffoldKey,
  32.         backgroundColor: AppTema.appColor,
  33.         appBar: AppBar(
  34.           elevation: 0,
  35.           title: Text(
  36.             Strings.titleEscolhaMateria,
  37.             style: Theme.of(context).textTheme.title
  38.           ),
  39.           centerTitle: true,
  40.           actions: <Widget>[
  41.             botaoAddNovaMateria(),
  42.           ],
  43.         ),
  44.         resizeToAvoidBottomPadding: false,
  45.         body: Container(
  46.           color: AppTema.appColor,
  47.           child: Container(
  48.             decoration: bordaContainer(),
  49.             child: FutureBuilder<QuerySnapshot>(
  50.               future: Firestore.instance
  51.                 .collection("materia")
  52.                 .getDocuments(),
  53.               builder: (context, snapshot) {
  54.                 if (!snapshot.hasData)
  55.                   return Center(
  56.                     child: CircularProgressIndicator(),
  57.                   );
  58.                 else {
  59.                   if (controle == 1) {
  60.                     listaMateriasArea.clear(); // limpar lista quando clicar na tela
  61.                     for (DocumentSnapshot doc in snapshot.data.documents) {
  62.                       InfoMateriasData data = InfoMateriasData.fromDocument(doc);
  63.                       for (int i = 0; i < widget.idMateriasArea.length; i++) {
  64.                         if (data.id.contains(widget.idMateriasArea[i])) {
  65.                           listaMateriasArea.add(data);
  66.                           values.putIfAbsent(
  67.                             data.nome, () => true); // Preenche o mapa
  68.                         }
  69.                       }
  70.                     }
  71.                   }
  72.                   return Container(
  73.                     margin: EdgeInsets.only(
  74.                       top: 0.7 * SizeConfig.heightMultiplier,
  75.                       left: 3.2 * SizeConfig.widthMultiplier,
  76.                       right: 3.2 * SizeConfig.widthMultiplier,
  77.                     ),
  78.                     child: listaMaterias(), //Lista de materias
  79.                   );
  80.                 }
  81.               },
  82.             ),
  83.           )
  84.         ),
  85.         floatingActionButton: FloatingActionButton.extended(
  86.           heroTag: 'tagAvancarMateria',
  87.           label: Text(
  88.             Strings.botaoAvancarMateria,
  89.             style: Theme.of(context).textTheme.button
  90.           ),
  91.           backgroundColor: AppTema.appColor,
  92.           onPressed:
  93.             onpressedAvancar(),
  94.         ),
  95.         floatingActionButtonLocation: FloatingActionButtonLocation.endFloat
  96.     );
  97.   }
  98.  
  99.   String gerenateId(){
  100.     var random = Random.secure();
  101.     var valuesRandom = List<int>.generate(
  102.       15,
  103.       (i) => random.nextInt(256)
  104.     );
  105.     var result = base64.encode(valuesRandom).toString();
  106.     result = result.replaceAll('+', '9');
  107.     result = result.replaceAll('/', '0');
  108.     result = result.replaceAll("\\", '1');
  109.     return result;
  110.   }
  111.  
  112.   Widget botaoAddNovaMateria() {
  113.     return IconButton(
  114.       icon: Icon(
  115.         Icons.add_box,
  116.       ),
  117.       onPressed: () {
  118.         janelaAddNovaMateria2(context);
  119.       },
  120.     );
  121.   }
  122.  
  123.   Widget listaMaterias() {
  124.     return ListView(
  125.       padding: EdgeInsets.only(
  126.         bottom: 70), // precisa ser fixo
  127.       children: values.keys.map((String key) {
  128.         return Container(
  129.           padding: EdgeInsets.only(
  130.             bottom: 0.14 * SizeConfig.heightMultiplier),
  131.           child: Card(
  132.             shape: BeveledRectangleBorder(
  133.               borderRadius: BorderRadius.circular(
  134.                 0.7 * SizeConfig.heightMultiplier)
  135.             ),
  136.             elevation: 4,
  137.             child: CheckboxListTile(
  138.               title: Text(
  139.                 key,
  140.                 style: Theme.of(context).textTheme.body1,
  141.               ),
  142.               value: values[key],
  143.               onChanged: (bool value) {
  144.                 setState(() {
  145.                   values[key] = value;
  146.                 });
  147.               },
  148.             ),
  149.           ),
  150.         );
  151.       }).toList(),
  152.     );
  153.   }
  154.  
  155.   Function onpressedAvancar() {
  156.     return () {
  157.       List<bool> listbool = values.values.toList();
  158.       List<InfoMateriasData> listaMateriasCiclo = List<InfoMateriasData>();
  159.       for (int i = 0; i < listbool.length; i++) {
  160.         if (listbool[i] == true) {
  161.           listaMateriasCiclo.add(
  162.             listaMateriasArea[i]);
  163.         }
  164.       }
  165.       if (listaMateriasCiclo.length > 0 && listaMateriasCiclo.length <= 30) {
  166.         Navigator.of(context).push(
  167.           MaterialPageRoute(
  168.             builder: (context) =>
  169.               EscolhaOrdemCicloTab(
  170.                 listaMaterias: listaMateriasCiclo)));
  171.       } else {
  172.         if (listaMateriasCiclo.length == 0) {
  173.           _scaffoldKey.currentState.showSnackBar(
  174.             SnackBar(
  175.               content: Text(
  176.                 Strings.onFailSelecionaMateria,
  177.                 style: Theme.of(context).textTheme.body1.copyWith(
  178.                   color: Colors.white)  
  179.               ),
  180.               backgroundColor: AppTema.erroColor,
  181.               duration: Duration(
  182.                 seconds: 2),
  183.             )
  184.           );
  185.         } else {
  186.           int qtdmaterias = listaMateriasCiclo.length;
  187.           _scaffoldKey.currentState.showSnackBar(
  188.             SnackBar(
  189.               content: Text(
  190.                 "Voce selecionou " +
  191.                 '$qtdmaterias materias' +
  192.                 Strings.onFailNumeroMaxMateria,
  193.                 style: Theme.of(context).textTheme.body1.copyWith(
  194.                   color: Colors.white)  
  195.               ),
  196.               backgroundColor: AppTema.erroColor,
  197.               duration: Duration(
  198.                 seconds: 5),
  199.             )
  200.           );
  201.         }
  202.       }
  203.     };
  204.   }
  205.  
  206.   Future<String> janelaAddNovaMateria2 (BuildContext context) async {
  207.     String novaMateria = '';
  208.  
  209.     return showDialog<String>(
  210.       context: context,
  211.       barrierDismissible: true,
  212.       builder: (BuildContext context) {
  213.         return Dialog(
  214.           shape: RoundedRectangleBorder(
  215.             borderRadius: BorderRadius.circular(
  216.               2.3 * SizeConfig.heightMultiplier),
  217.           ),      
  218.           elevation: 0.0,
  219.           backgroundColor: Colors.transparent,
  220.           child: Stack(
  221.             children: <Widget>[
  222.               Container(
  223.                 padding: EdgeInsets.only(
  224.                   top: 11.6 * SizeConfig.widthMultiplier + 2.3 * SizeConfig.heightMultiplier,
  225.                   bottom: 2 * SizeConfig.heightMultiplier,
  226.                   left: 5 * SizeConfig.widthMultiplier,
  227.                   right: 5 * SizeConfig.widthMultiplier,
  228.                 ),
  229.                 margin: EdgeInsets.only(
  230.                   top: 11.6 * SizeConfig.widthMultiplier),
  231.                 decoration: BoxDecoration(
  232.                   color: Colors.white,
  233.                   shape: BoxShape.rectangle,
  234.                   borderRadius: BorderRadius.circular(
  235.                     2.3 * SizeConfig.heightMultiplier),
  236.                   boxShadow: [
  237.                     BoxShadow(
  238.                       color: Colors.black26,
  239.                       blurRadius: 10.0,
  240.                       offset: const Offset(0.0, 10.0),
  241.                     ),
  242.                   ],
  243.                 ),
  244.                 child: SingleChildScrollView(
  245.                   child: Column(
  246.                     mainAxisSize: MainAxisSize.min,
  247.                     children: <Widget>[
  248.                       Align(
  249.                         alignment: Alignment.topCenter,
  250.                         child: Text(
  251.                           Strings.titleDialogAddMateria,
  252.                           style: Theme.of(context).textTheme.title.copyWith(
  253.                             color: AppTema.appColor)
  254.                         ),
  255.                       ),
  256.                       SizedBox(
  257.                         height: 2 * SizeConfig.heightMultiplier),
  258.                       Align(
  259.                         alignment: Alignment.center,
  260.                         child: TextField(
  261.                           style: Theme.of(context).textTheme.body1,
  262.                           maxLines: 1,
  263.                           maxLength: 70,
  264.                           autofocus: true,
  265.                           decoration: InputDecoration(
  266.                             labelText: 'Nome da materia',
  267.                             labelStyle: TextStyle(
  268.                               fontSize: 2.2 * SizeConfig.textMultiplier,),
  269.                             hintText: 'Lingua Francesa',
  270.                             hintStyle: TextStyle(
  271.                               fontSize: 2.2 * SizeConfig.textMultiplier,
  272.                             )
  273.                           ),
  274.                           onChanged: (value) {
  275.                             setState(() {
  276.                               novaMateria = value;
  277.                             });
  278.                           },
  279.                         )
  280.                       ),
  281.                       SizedBox(
  282.                         height: 2 * SizeConfig.heightMultiplier),
  283.                       Flexible(
  284.                         child: Align(
  285.                           alignment: Alignment.bottomCenter,
  286.                           child: Row(
  287.                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  288.                               children: <Widget>[
  289.                                 Flexible(
  290.                                   child: RaisedButton(
  291.                                     child: Text(
  292.                                       "Cancelar",
  293.                                       style: Theme.of(context).textTheme.button
  294.                                     ),
  295.                                     color: AppTema.appColor,
  296.                                     shape:  RoundedRectangleBorder(
  297.                                       borderRadius: BorderRadius.circular(
  298.                                         4.4 * SizeConfig.heightMultiplier)),
  299.                                     onPressed: () {
  300.                                       Navigator.of(context).pop();
  301.                                     }
  302.                                   )
  303.                                 ),
  304.                                 Flexible(
  305.                                   child: RaisedButton(
  306.                                     child: Text(
  307.                                       "Adicionar",
  308.                                       style: Theme.of(context).textTheme.button.copyWith(
  309.                                         fontWeight: FontWeight.bold,)
  310.                                     ),
  311.                                     color: AppTema.appColor,
  312.                                     shape:  RoundedRectangleBorder(
  313.                                       borderRadius: BorderRadius.circular(
  314.                                         4.4 * SizeConfig.heightMultiplier)),
  315.                                     onPressed: (){
  316.                                       setState(() {
  317.                                         controle = 0;
  318.                                         if (novaMateria.isEmpty == false) {
  319.                                           RandomColor _randomColor = RandomColor();
  320.                                           Color _color = _randomColor.randomColor();
  321.                                           InfoMateriasData nova = InfoMateriasData.novaMateria( // id, nome, cor, tempo, estudou, desempenho, quantidadeTopicos
  322.                                             gerenateId(),
  323.                                             '$novaMateria',
  324.                                             _color
  325.                                               .toString()
  326.                                               .replaceAll('Color(0xff', '')
  327.                                               .replaceAll(')', '')
  328.                                               .toUpperCase(),
  329.                                             '00:00:00',
  330.                                             false,
  331.                                             '0.00',
  332.                                             '0'
  333.                                           );
  334.                                           listaMateriasArea.add(nova);
  335.                                           values.putIfAbsent(
  336.                                             nova.nome, () => true);
  337.                                           Navigator.of(context).pop();
  338.                                         }
  339.                                       });
  340.                                     },
  341.                                   )
  342.                                 ),
  343.                               ],
  344.                             ),
  345.                           )
  346.                         )
  347.                       ],
  348.                     ),
  349.                 )
  350.               ),
  351.               Positioned(
  352.                 left: 3.7 * SizeConfig.widthMultiplier,
  353.                 right: 3.7 * SizeConfig.widthMultiplier,
  354.                 child: CircleAvatar(
  355.                   backgroundColor: AppTema.appBackgroundColor,
  356.                   radius: 7.3 * SizeConfig.heightMultiplier,
  357.                   child:  Container(
  358.                     decoration: BoxDecoration(
  359.                       borderRadius: BorderRadius.all(
  360.                         Radius.circular(
  361.                           7.3 * SizeConfig.heightMultiplier))),
  362.                     child: Icon(
  363.                       Icons.error,
  364.                       color: AppTema.appColor,
  365.                       size: 14 * SizeConfig.heightMultiplier,
  366.                     )
  367.                   ),
  368.                 ),
  369.               ),
  370.             ],
  371.           ),
  372.         );
  373.       }
  374.     );
  375.   }
  376.  
  377.   Future<String> janelaAddNovaMateria(BuildContext context) async {
  378.     String novaMateria2 = '';
  379.  
  380.     return showDialog<String>(
  381.       context: context,
  382.       barrierDismissible: true,
  383.       builder: (BuildContext context) => CustomDialogAdd(
  384.         title: Strings.titleDialogAddMateria,
  385.         novaMateria: novaMateria2,
  386.         onPressedAddMateria: (){
  387.           setState(() {
  388.             controle = 0;
  389.             if (novaMateria2.isEmpty == false) {
  390.               RandomColor _randomColor = RandomColor();
  391.               Color _color = _randomColor.randomColor();
  392.               InfoMateriasData nova = InfoMateriasData.novaMateria( // id, nome, cor, tempo, estudou, desempenho, quantidadeTopicos
  393.                 gerenateId(),
  394.                 '$novaMateria2',
  395.                 _color
  396.                   .toString()
  397.                   .replaceAll('Color(0xff', '')
  398.                   .replaceAll(')', '')
  399.                   .toUpperCase(),
  400.                 '00:00:00',
  401.                 false,
  402.                 '0.00',
  403.                 '0'
  404.               );
  405.               listaMateriasArea.add(nova);
  406.               values.putIfAbsent(
  407.                 nova.nome, () => true);
  408.             }
  409.           });
  410.         },
  411.       ),
  412.     );
  413.   }
  414.  
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement