简体   繁体   中英

Add the widgets to the list dynamically and conditionally flutter

I am trying to add the widgets of the items that are selected from list of checkbox. I am getting the list of items checked but unable to add the widgets conditionally. I have implemented as follows:

Checkbox list items

responseData = [
  MachinesData(0, "गाई", false),
  MachinesData(1, "भैंसी    ", false),
  MachinesData(2, " चौरी/याक    ", false),
  MachinesData(3, "गोरु/राँगा   ", false),
  MachinesData(4, "भेँडा/बाख्रा/च्याङ्ग्रा ", false),
  MachinesData(5, "बङ्गुर/ सुङ्गुर/ बँदेल", false),
  MachinesData(6, "कुखुरा ", false),
  MachinesData(7, "अन्य पन्छीहरु  ", false),
  MachinesData(8, "मत्स्य  ", false),
  MachinesData(9, "घाँसबाली   ", false),
  MachinesData(10, "अन्य (पशुपन्छी तथा मत्स्य)    ", false),
];

When these list of items from dialog are selected I am getting the list of index, names and checkedValues in main class as follows:

onChecked(var value, int i, String name) {
setState(() {
  if (responseData[i].isChecked == true) {
    checkIndex.add(i);
    nonRepated.add(responseData[i].name);             
  } else {
    nonRepated.remove(responseData[i].name);       
  }
});  
}

Now I need to check the name the items and add their respective widgets or any other better methods to check condition are appreciated. They may be one or more selected items with different layouts also I need to remove respect layout if user presses an icon. Could not move forward from these above steps.

I added the identifier to the responseData as:

 responseData = [
  PData(0, "गाई", 'cow', false),
  PData(1, "भैंसी   ", 'buffalo', false),
  PData(2, " चौरी/याक   ", 'yak', false),
  PData(3, "गोरु/राँगा  ", 'ox', false),
  PData(4, "भेँडा/बाख्रा/च्याङ्ग्रा ", 'sheep', false),
  PData(5, "बङ्गुर/ सुङ्गुर/ बँदेल", 'pig', false),
  PData(6, "कुखुरा ", 'chicken', false),
  PData(7, "अन्य पन्छीहरु  ", 'birds', false),
  PData(8, "मत्स्य  ", 'fish', false),
  PData(9, "घाँसबाली   ", 'grass', false),
  PData(10, "अन्य (पशुपन्छी तथा मत्स्य)    ", 'other', false),
];

Callback function:

 onChecked(var value, int i, String name) {
    setState(() {
      if (responseData[i].isChecked == true) {
      
  identifiers.add(responseData[i].identifier);
        nonRepated.add(responseData[i].name);             
      } else {
 identifiers.remove(responseData[i].identifier);
        nonRepated.remove(responseData[i].name);       
      }
    });  
    }

Build Method:

 for (int i = 0; i < identifiers.length; i++)
        _MachineData(identifiers, i, nonRepated[i], checkIndex[i]),

MachineData widget

 Widget _MachineData(var params, int i, String nonRepate, int checkInde) {
switch (params[i]) {
  case 'cow':
    return Cow(nonRepated, i, checkInde, nonRepate, onDelete);
  case 'buffalo':
    return Buffalo(nonRepated, i, checkInde, nonRepate, onDelete);
  
  default:
    return Container();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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