如何在Flutter中使用Json添加带有复选框列表磁贴的多个扩展磁贴

时间:2019-08-30 05:53:42

标签: json checkbox flutter expandablelistview checkboxlist

此json有两个数据,第一个是唯一的名称,第二个在其中,还有一个名称是服务名称。例如,“旅行和住宿”和“宗教”是主要名称,必须在扩展图块名称中显示,“教堂/洗礼”和“印度庙宇”是一个子项目,它显示在复选框列表图块中。希望你能理解:slightly_smiling_face:请帮助我

class _MyHomePageState extends State<MyHomePage> {
  var lovCountryServices = [
    {
      "services": [
        {
          "service_group_id": 22,
          "service_category": "B",
          "name": "Air Tickets",
          "id": 228
        },
        {
          "service_group_id": 22,
          "service_category": "W",
          "name": "Boys Hostel",
          "id": 229
        },
      ],
      "name": "Travel and Stay",
      "is_active": true,
      "id": 22
    },
    {
      "services": [
        {
          "service_group_id": 19,
          "service_category": "B",
          "name": "Church/ Baptism",
          "id": 193
        },
        {
          "service_group_id": 19,
          "service_category": "B",
          "name": "Hindu Temples",
          "id": 194
        }
      ],
      "name": "Religious",
      "is_active": true,
      "id": 19
    }
  ];
  List<_Result> _results = [];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        children: <Widget>[
          ListView.builder(
            shrinkWrap: true,
            padding: const EdgeInsets.all(8.0),
            itemCount: lovCountryServices.length,
            itemBuilder: (BuildContext context, int index) {
              var item = lovCountryServices[index];
              var items= lovCountryServices[index]['services'];
              return ExpansionTile(
                title: Text(item['name']),
                children: <Widget>[
                  CheckboxListTile(
                    title: Text("temp"),
                    value: item['is_active'],
                    onChanged: (val) {
                      setState(() {
                        item['is_active'] = val;
                      });
                    },
                  ),
                ],
              );
            },
          ),
          RaisedButton(
            onPressed: () => print("sending to backend"),
            child: Text("SEND"),
          )
        ],
      )),
    );
  }
}

我希望复选框列表中的数据正确,其中有一个名为TEMP的虚拟数据,我希望json中的json中的数据现在存在“ Boys Hostel”,此数据需要进入复选框列表中。希望你能理解我,请帮助我

1 个答案:

答案 0 :(得分:0)

工作代码:您可以使用Map变量来存储布尔值。

  Map<String, bool> _selection = {};

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dummy'),
      ),
      body: Center(
          child: Column(
        children: <Widget>[
          ListView.builder(
            shrinkWrap: true,
            padding: const EdgeInsets.all(8.0),
            itemCount: lovCountryServices.length,
            itemBuilder: (BuildContext context, int index) {
              var item =
                  lovCountryServices[index]; // should be outside build function
              List items = item['services']; // should be outside build function
              return ExpansionTile(
                title: Text(item['name']),
                children: List.generate(items.length, (i) {
                  _selection[items[i]['name']] =
                      _selection[items[i]['name']] ?? item['is_active'];
                  return CheckboxListTile(
                    title: Text(items[i]['name']),
                    value: _selection[items[i]['name']],
                    onChanged: (val) {
                      setState(() {
                        _selection[items[i]['name']] = val;
                      });
                    },
                  );
                }),
              );
            },
          ),
          RaisedButton(
            onPressed: () => print("sending to backend"),
            child: Text("SEND"),
          )
        ],
      )),
    );
  }