Guest User

Untitled

a guest
Jul 21st, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 17.36 KB | None | 0 0
  1. class CropNutritionProgramOverviewScreen extends StatefulWidget {
  2.   CropNutritionProgramOverviewScreen({Key? key, required this.showAppbar, required this.showBottombar}) : super(key: key);
  3.   bool showAppbar = false;
  4.   bool showBottombar = false;
  5.  
  6.   @override
  7.   State<CropNutritionProgramOverviewScreen> createState() => _CropNutritionProgramOverviewScreenState();
  8. }
  9.  
  10. class _CropNutritionProgramOverviewScreenState extends State<CropNutritionProgramOverviewScreen> {
  11.   Future<RegionsList?>? regionsListFuture;
  12.   Future<CropsModel?>? cropsListFuture;
  13.   Future<CropNutritionProgramModel?>? programsListFuture;
  14.   // List<CropNutritionProgram> programs = [];
  15.   bool accordianFlag = false;
  16.   bool _showContent = false;
  17.   String regionSelected = '0';
  18.   String cropSelected = '0';
  19.   List<DropdownMenuItem<String>> regionDropdownItems = [];
  20.   List<DropdownMenuItem<String>> cropDropdownItems = [];
  21.  
  22.   _CropNutritionProgramOverviewScreenState();
  23.  
  24.   @override
  25.   void initState() {
  26.     super.initState();
  27.     regionsListFuture = getRegions();
  28.     cropsListFuture = getCrops(0);
  29.     programsListFuture = getCropNutritionPrograms();
  30.   }
  31.  
  32.   Future<RegionsList?> getRegions() async {
  33.     RegionsList regionsList = await getRegionsAPI();
  34.     print("flutter - regions list: " + regionsList.toString());
  35.    
  36.     List<Region> regions = regionsList.regions ?? [];
  37.     regionDropdownItems.clear();
  38.     for (Region region in regions) {
  39.       regionDropdownItems.add(DropdownMenuItem(
  40.         child: Text(region.name),
  41.         value: region.id.toString(),
  42.       ));
  43.     }
  44.     if (regions.length > 0) {
  45.       regionSelected = regions[0].id.toString();
  46.     }
  47.  
  48.     return regionsList;
  49.   }
  50.  
  51.   Future<CropsModel?> getCrops(int regionId) async {
  52.     CropsModel cropsList = await getCropsAPI(regionId);
  53.     print("flutter - cropsList: " + cropsList.toString());
  54.  
  55.     List<Crop> crops = cropsList.cropsList ?? [];
  56.     cropDropdownItems.clear();
  57.     for (Crop crop in crops) {
  58.       cropDropdownItems.add(DropdownMenuItem(
  59.         child: Text(crop.name),
  60.         value: crop.id.toString(),
  61.       ));
  62.     }
  63.  
  64.     return cropsList;
  65.   }
  66.  
  67.   Future<CropNutritionProgramModel?> getCropNutritionPrograms() async {
  68.     var regionId = int.parse(regionSelected);
  69.     var cropId = int.parse(cropSelected);
  70.     CropNutritionProgramModel programsModel = await getCropNutritionProgramsAPI(regionId, cropId);
  71.     print("flutter - cropNutritionProgramsModel: " + programsModel.toString());
  72.     // programs = programsModel.cropNutritionPrograms;
  73.     return programsModel;
  74.   }
  75.  
  76.   Future<List<dynamic>> fetchData() async {
  77.     return await Future.wait([getRegions(), getCrops(0), getCropNutritionPrograms()]);
  78.   }
  79.  
  80.   @override
  81.   Widget build(BuildContext context) {
  82.     return Scaffold(
  83.       backgroundColor: Colors.white,
  84.       appBar: widget.showAppbar ? TopAppBar() : null,
  85.       drawer: widget.showAppbar ? TopDrawer() : null,
  86.       body: FutureBuilder<CropNutritionProgramModel?>(
  87.         future: programsListFuture,
  88.         builder: (context, snapshot) {
  89.           if (snapshot.connectionState == ConnectionState.waiting) {
  90.             return _buildLoadingIndicator();
  91.           } else if (snapshot.hasError) {
  92.             return Text('Error: ${snapshot.error}');
  93.           } else if (snapshot.hasData) {
  94.             List<CropNutritionProgram> programs = snapshot.data!.cropNutritionPrograms;
  95.             return Container(
  96.               child: Column(
  97.                 crossAxisAlignment: CrossAxisAlignment.start,
  98.                 children: [
  99.                   Padding(
  100.                     padding: const EdgeInsets.only(top: 16, left: 16, bottom: 8),
  101.                     child: Text(
  102.                       'Crop Nutrition Programs',
  103.                       style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  104.                     ),
  105.                   ),
  106.                   Expanded(
  107.                     child: SingleChildScrollView(
  108.                       child: Container(
  109.                         padding: EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 8.0),
  110.                         child: Column(
  111.                           crossAxisAlignment: CrossAxisAlignment.start,
  112.                           children: [
  113.                             SizedBox(height: 4.0),
  114.                             Image.asset(
  115.                               regionsAssetPath,
  116.                               fit: BoxFit.cover,
  117.                             ),
  118.                             SizedBox(height: 16.0),
  119.                             Center(
  120.                               child: Text(
  121.                                 "Discover Crop Nutrition Program recommendations for your state!",
  122.                                 textAlign: TextAlign.center,
  123.                               ),
  124.                             ),
  125.                             SizedBox(height: 16.0),
  126.                             Padding(
  127.                               padding: const EdgeInsets.only(left: 16.0, right: 16.0, top: 0.0, bottom: 8.0),
  128.                               child: StatefulBuilder(
  129.                                 builder: (BuildContext context, StateSetter setState) {
  130.                                   return Card(
  131.                                     shape: new RoundedRectangleBorder(
  132.                                       side: new BorderSide(color: Colors.red, width: 1.0),
  133.                                       borderRadius: BorderRadius.circular(4.0),
  134.                                     ),
  135.                                     child: Column(
  136.                                       children: [
  137.                                         Container(
  138.                                           height: 40,
  139.                                           child: Transform.translate(
  140.                                             offset: Offset(0, -8),
  141.                                             child: ListTile(
  142.                                               title: const Text("Filter By", style: TextStyle(fontWeight: FontWeight.w500, fontSize: 16, color: Colors.red)),
  143.                                               leading: Icon(Icons.filter_alt_outlined, color: Colors.red,),
  144.                                               trailing: IconButton(
  145.                                                 icon: Icon(
  146.                                                   _showContent ? Icons.arrow_drop_up : Icons.arrow_drop_down, color: Colors.red,),
  147.                                                 onPressed: () {
  148.                                                   setState(() {
  149.                                                     _showContent = !_showContent;
  150.                                                   });
  151.                                                 },
  152.                                               ),
  153.                                               onTap: () {
  154.                                                 setState(() {
  155.                                                   _showContent = !_showContent;
  156.                                                 });
  157.                                               },
  158.                                             ),
  159.                                           ),
  160.                                           decoration: new BoxDecoration(
  161.                                             border: new Border(bottom: new BorderSide(color: Colors.red)),
  162.                                           ),
  163.                                         ),
  164.                                         _showContent
  165.                                         ? Container(
  166.                                             padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 5),
  167.                                             child: Column(
  168.                                               crossAxisAlignment: CrossAxisAlignment.start,
  169.                                               mainAxisSize: MainAxisSize.min,
  170.                                               children: [
  171.                                                 SizedBox(
  172.                                                   height: 5,
  173.                                                 ),
  174.                                                 Padding(
  175.                                                   padding:
  176.                                                   const EdgeInsets.only(left: GlobalMargin.leftAccordian, right: GlobalMargin.rightAccordian),
  177.                                                   child: InputDecorator(
  178.                                                     decoration: const InputDecoration(
  179.                                                         contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 0),
  180.                                                         border: OutlineInputBorder(),
  181.                                                     ),
  182.                                                     child: DropdownButtonHideUnderline(
  183.                                                       child: DropdownButton<String>(
  184.                                                         value: regionSelected,
  185.                                                         isExpanded: true,
  186.                                                         isDense: true,
  187.                                                         hint: Text('Select Region'),
  188.                                                         icon:
  189.                                                         const Icon(Icons.arrow_drop_down_outlined),
  190.                                                         style: const TextStyle(color: Colors.black),
  191.                                                         onChanged: (value) {
  192.                                                           setState(() {
  193.                                                             accordianFlag = true;
  194.                                                             if (value != null) {
  195.                                                               regionSelected = value;
  196.                                                             }
  197.                                                           });
  198.                                                         },
  199.                                                         items: regionDropdownItems
  200.                                                       ),
  201.                                                     ),
  202.                                                   ),
  203.                                                 ),
  204.                                                 SizedBox(
  205.                                                   height: 10,
  206.                                                 ),
  207.                                                 Padding(
  208.                                                   padding:
  209.                                                   const EdgeInsets.only(left: GlobalMargin.leftAccordian, right: GlobalMargin.rightAccordian),
  210.                                                   child: InputDecorator(
  211.                                                     decoration: const InputDecoration(
  212.                                                         contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 0),
  213.                                                         border: OutlineInputBorder(),
  214.                                                     ),
  215.                                                     child: DropdownButtonHideUnderline(
  216.                                                       child: DropdownButton<String>(
  217.                                                         value: cropSelected,
  218.                                                         isExpanded: true,
  219.                                                         isDense: true,
  220.                                                         hint: Text('Select Crop'),
  221.                                                         icon:
  222.                                                         const Icon(Icons.arrow_drop_down_outlined),
  223.                                                         style: const TextStyle(color: Colors.black),
  224.                                                         onChanged: (value) {
  225.                                                           setState(() {
  226.                                                             accordianFlag = true;
  227.                                                             if (value != null) {
  228.                                                               cropSelected = value;
  229.                                                             }
  230.                                                           });
  231.                                                         },
  232.                                                         items: cropDropdownItems,
  233.                                                       ),
  234.                                                     ),
  235.                                                   ),
  236.                                                 ),
  237.                                                 SizedBox(
  238.                                                   height: 10,
  239.                                                 ),
  240.                                                 Container(
  241.                                                   width: double.infinity,
  242.                                                   alignment: Alignment.topCenter,
  243.                                                   child: Container(
  244.                                                     width: 200,
  245.                                                     child: ElevatedButton(
  246.                                                       style: ElevatedButton.styleFrom(
  247.                                                         primary: Colors.red,
  248.                                                       ),
  249.                                                       onPressed: () async {
  250.                                                         setState(() {
  251.                                                           accordianFlag = false;
  252.                                                         });
  253.                                                         var updatedProgramsList = await getCropNutritionPrograms();
  254.                                                         setState(() {
  255.                                                           programsListFuture = Future.value(updatedProgramsList);
  256.                                                         });
  257.                                                       },
  258.                                                       child: Text(
  259.                                                         'Update',
  260.                                                         style: TextStyle(
  261.                                                           color: Colors.white,
  262.                                                           fontWeight: FontWeight.bold
  263.                                                         ),
  264.                                                       ),
  265.                                                     )
  266.                                                   )
  267.                                                 ),
  268.                                               ],
  269.                                             ),
  270.                                           )
  271.                                         : Container(),
  272.                                       ],
  273.                                     ),
  274.                                   );
  275.                                 },
  276.                               ),
  277.                             ),
  278.                             ListView.separated(
  279.                               shrinkWrap: true,
  280.                               physics: NeverScrollableScrollPhysics(),
  281.                               itemCount: programs.length,
  282.                               separatorBuilder: (_, __) => Divider(height: 1),
  283.                               itemBuilder: (context, index) {
  284.                                 return Padding(
  285.                                   padding: EdgeInsets.fromLTRB(-8, 8, 0, 8),
  286.                                   child: ListTile(
  287.                                     title: Text("${programs[index].name}"),
  288.                                     trailing: Icon(Icons.arrow_forward, color: Colors.red),
  289.                                     onTap: () {
  290.                                       print("crop nutrition program tapped");
  291.                                       navigateToCropNutritionListScreen(context, int.parse(regionSelected), 0);
  292.                                     },
  293.                                   ),
  294.                                 );
  295.                               }
  296.                             ),
  297.                           ]
  298.                         ),
  299.                       ),
  300.                     ),
  301.                   ),  
  302.                 ],
  303.               ),
  304.             );
  305.           } else {
  306.             return Text('No data available');
  307.           }
  308.         },
  309.       ),
  310.     );
  311.   }
  312.  
  313.   void navigateToCropNutritionListScreen(BuildContext context, int regionId, int stateId) {
  314.     var provider = Provider.of<AppBarContitonProvider>(
  315.       context,
  316.       listen: false,
  317.     );
  318.     provider.setInnerPageLoaded(true);
  319.     provider.setPdfScreenLoaded(false, '');
  320.     NavKey.key.currentState?.pushNamed(Routes.cropNutritionList,
  321.       arguments: CropNutritionListArguments(regionId, stateId)).then((value) {
  322.         provider.setInnerPageLoaded(false);
  323.     });
  324.   }
  325.  
  326.   Widget _buildLoadingIndicator() {
  327.     return Padding(
  328.       padding: EdgeInsets.all(8.0),
  329.       child: Center(
  330.         child: CircularProgressIndicator(),
  331.       ),
  332.     );
  333.   }
  334. }
  335.  
Advertisement
Add Comment
Please, Sign In to add comment