简体   繁体   English

flutter 中开关盒的替代或简单方法

[英]alternative or easy way for switch case in flutter

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我有一个工作代码,它将获取一个索引并根据该索引构建一个小部件,但是我的代码使用 switch case 并且我必须手动对每个小部件进行编码是否有其他方法可以实现与下面 flutter 相同的效果

I am using getx controller here for every widget, and typing every widget manually is overwhelming我在这里为每个小部件使用 getx controller,手动输入每个小部件是压倒性的

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);
               },
             ),
           ),)
         ],
       ),
     );
   }
 }

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);
              },
            ),
          ),
        ],
      ),
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM