从列表中删除数据,当复选框未被选中时。

huangapple go评论203阅读模式
英文:

Remove data from list when checkbox in unchecked

问题

I used this command widgets.removeAt(data[index]['title']); but when I tried to uncheck its checkbox not all titles of checkboxes are removed.

在这个命令中使用了widgets.removeAt(data[index]['title']),但是当我尝试取消选中复选框时,并没有移除所有复选框的标题。

英文:

I used this command widgets.removeAt(data[index]['title']); but when I tried to uncheck its checkbox not all titles of checkboxs are removed.

class _MyHomePageState extends State<MyHomePage> {
  List<Widget> widgets = [];
  List<Map<String, dynamic>> data = [
    {'title': 'checkbox1', 'value': false},
    {'title': 'checkbox2', 'value': false},
    {'title': 'checkbox3', 'value': false}
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          Column(children: widgets),
          ListView.builder(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (context, index) {
              return CheckboxListTile(
                  title: Text(data[index]['title']),
                  value: data[index]['value'],
                  onChanged: (value) {
                    setState(() {
                      // bool check=data[index]['value'] = value!;
                      if (data[index]['value'] = value!) {
                        widgets.add(Text(data[index]['title'].toString()));
                        print(data[index]['title'].toString());
                      } else {
                        widgets.removeAt(index);
                      }
                    });
                  });
            },
          ),
        ],
      ),
    );  }
}

Screenshot here

从列表中删除数据,当复选框未被选中时。

In that picture checkbox title not removed;
in the console I got this error:

> Value not in range: 2

答案1

得分: 1

Here is the translated code:

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> checkedItem = [];
  List<Map<String, dynamic>> data = [
    {'title': 'checkbox1', 'value': false},
    {'title': 'checkbox2', 'value': false},
    {'title': 'checkbox3', 'value': false}
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          // title: Text(widget.title),
          ),
      body: Column(
        children: [
          Column(
            children: checkedItem.map((e) => Text(e)).toList(),
          ),
          ListView.builder(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (context, index) {
              return CheckboxListTile(
                  title: Text(data[index]['title']),
                  value: data[index]['value'],
                  onChanged: (value) {
                    setState(() {
                      if (data[index]['value'] = value!) {
                        checkedItem.add(data[index]['title'].toString());
                      } else {
                        checkedItem.remove(data[index]['title']);
                      }
                    });
                  });
            },
          ),
        ],
      ),
    );
  }
}

I've translated the code for you as requested.

英文:

Instead of using List&lt;Widget&gt; you can use list of String to hold items and then you can easily remove items,

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  List&lt;String&gt; checkedItem = [];
  List&lt;Map&lt;String, dynamic&gt;&gt; data = [
    {&#39;title&#39;: &#39;checkbox1&#39;, &#39;value&#39;: false},
    {&#39;title&#39;: &#39;checkbox2&#39;, &#39;value&#39;: false},
    {&#39;title&#39;: &#39;checkbox3&#39;, &#39;value&#39;: false}
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          // title: Text(widget.title),
          ),
      body: Column(
        children: [
          Column(
            children: checkedItem.map((e) =&gt; Text(e)).toList(),
          ),
          ListView.builder(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (context, index) {
              return CheckboxListTile(
                  title: Text(data[index][&#39;title&#39;]),
                  value: data[index][&#39;value&#39;],
                  onChanged: (value) {
                    setState(() {
                      // bool check=data[index][&#39;value&#39;] = value!;
                      if (data[index][&#39;value&#39;] = value!) {
                        checkedItem.add(data[index][&#39;title&#39;].toString());
                      } else {
                        checkedItem.remove(data[index][&#39;title&#39;]);
                      }
                    });
                  });
            },
          ),
        ],
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年5月14日 20:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定