Home > database >  alternative or easy way for switch case in flutter
alternative or easy way for switch case in flutter

Time:01-21

I have a working code that will take an index and build a widget according to that index, but my code uses switch case and I have to code every widget manually is there any other way to achieve the same as below in flutter

I am using getx controller here for every widget , and typing every widget manually is overwhelming

code

Widget buildchoices(BuildContext context, int index) {
    if (kDebugMode) {
      print('building choice with index$index');
    }
    switch (index) {
      case 0:
        return animalChoices();
      case 1:
        return donation();
      case 2:
        return firstaid();
      case 3:
        return helpsearch();
      case 4:
        return serviceneeded();
      case 5:
        return transportation();
      case 6:
        return vehicleissue();
      case 7:
        return wellnesscheck();
      default:
        return const Text('Select one above ');
    }
  }
  // widgets
   Widget animalChoices() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width: double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: animalslist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox:index, checklist:animalslist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget donation() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width: double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: donationslist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox: index,checklist:donationslist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget helpsearch() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width:double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: helpsearchlist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox: index, checklist:helpsearchlist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget firstaid() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width: double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: firstaidlist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox:index, checklist:firstaidlist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget transportation() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width:double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: transportationlist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox:index, checklist:transportationlist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget vehicleissue() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width: double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: vehicleissuelist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox: index,checklist:vehicleissuelist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget wellnesscheck() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width: double.maxFinite,
       child: Column(
         children: [
           Expanded(
             child: ListView.builder(
               itemCount: wellnesschecklist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox:index, checklist:wellnesschecklist);
               },
             ),
           ),
         ],
       ),
     );
   }
   Widget serviceneeded() {
     BarController barController = Get.find();
     return SizedBox(
       height: 500,width: double.maxFinite,
       child: Column(
         children: [
           GetBuilder<BarController>(builder:(_)=>Expanded(
             child: ListView.builder(
               itemCount: serviceneededlist.length,
               itemBuilder: (context, index) {
                 barController.ischecklist.clear();
                 for(int i=0;i<serviceneededlist.length;i  ){
                   barController.ischecklist.add(false);
                 }
                 return TaskTile(indexofbox:index, checklist:serviceneededlist);
               },
             ),
           ),)
         ],
       ),
     );
   }
 }

CodePudding user response:

You can avoid code repetition this way.

 Widget buildchoices(BuildContext context, int index) {
    BarController barController = Get.find();
    var listOfLists = [
      animalChoicesList,
      donationList,
      firstaidList,
      helpsearchList,
      serviceneededList,
      transportationList,
      vehicleissueList,
      wellnesscheckList
    ];
    var selectedList = listOfLists[index];
    return SizedBox(
      height: 500,
      width: double.maxFinite,
      child: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: selectedList.length,
              itemBuilder: (context, index) {
                barController.ischecklist.clear();
                for (int i = 0; i < serviceneededlist.length; i  ) {
                  barController.ischecklist.add(false);
                }
                return TaskTile(indexofbox: index, checklist: selectedList);
              },
            ),
          ),
        ],
      ),
    );
  }
  • Related