【问题标题】:How can i add multiple expand tile with checkbox list tile using Json in flutter如何在flutter中使用Json添加多个带有复选框列表磁贴的扩展磁贴
【发布时间】:2020-01-03 09:06:05
【问题描述】:

这个 json 有两个数据,第一个只有名称,第二个在里面还有名称,即服务名称。 ex 'Travel and Stay' 和 'Religious' 是必须显示在扩展磁贴名称中的主要名称,而 'Church/Baptism' 和 'Hindu Temples' 是显示在复选框列表磁贴中的子项。希望你能理解 :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 中有“男孩旅馆”,此数据需要进入复选框列表图块。希望你不明白请帮助我

【问题讨论】:

    标签: json checkbox flutter expandablelistview checkboxlist


    【解决方案1】:

    工作代码:您可以使用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"),
              )
            ],
          )),
        );
      }
    

    【讨论】:

    • 亲爱的,感谢您的回答。但是当我展开磁贴并从复选框列表磁贴中选择项目时,它会选择所有项目或不选择任何项目。我希望该用户可以根据选择进行选择。希望你能理解,请帮助我
    • 嘿,非常感谢,亲爱的 :) 你太棒了。
    • 你能告诉我如何根据国家代码格式化手机号码吗?请帮助我,您的回答将不胜感激
    • @RutvikGumasana - 根据国家/地区代码询问格式手机号码的不同问题。
    • 是的,我已经问过了,请在我的个人资料中查看并请帮助我:)
    猜你喜欢
    • 2022-10-24
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 2020-01-01
    • 2021-02-18
    • 2020-01-25
    • 2011-07-16
    • 2020-07-09
    相关资源
    最近更新 更多