Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 如何创建CheckBoxTile列表视图?_Class_Flutter - Fatal编程技术网

Class 如何创建CheckBoxTile列表视图?

Class 如何创建CheckBoxTile列表视图?,class,flutter,Class,Flutter,因此,我正在制作一个应用程序,我需要一个复选框列表视图。然后,它需要在新屏幕上显示所有选中的项目。到目前为止,我尝试的是listview,但当我点击一个复选框时,所有复选框都被选中。任何帮助都将不胜感激。谢谢 bool yes = false Widget pro() => Expanded( child: ListView( children: ListTile.divideTiles( contex

因此,我正在制作一个应用程序,我需要一个复选框列表视图。然后,它需要在新屏幕上显示所有选中的项目。到目前为止,我尝试的是listview,但当我点击一个复选框时,所有复选框都被选中。任何帮助都将不胜感激。谢谢

    bool yes = false
    Widget pro() =>
      Expanded(
        child: ListView(
          children: ListTile.divideTiles(
            context: context,
            tiles: [
              options("Apple"),
              options("Banana"),
            ],
          ).toList(),
        ),
      );

  Widget options(String head) =>
      CheckboxListTile(
        title: Text(head),
        value: yes,
        onChanged: (bool value) {
          setState(() {
            yes = value;
          });
        },
      );
试试这个

 List<bool> isCheckedList = List.filled(2, false, growable: false);
 List<String> selectedItemsList = [];

    Widget pro() =>
      Expanded(
        child: ListView(
          children: ListTile.divideTiles(
            context: context,
            tiles: [
              options("Apple",0),
              options("Banana",1),
            ],
          ).toList(),
        ),
      );

  Widget options(String head,int index) =>
      CheckboxListTile(
        title: Text(head),
        value: isCheckedList[index],
        onChanged: (bool value) {
          setState(() {
             isCheckedList[index] = value;
             value ? selectedItemsList.add(head): selectedItemsList.removeWhere((item) => item == head);
          });
        },
      );
然后在新页面中,您可以通过这种方式检索它

class NewPage extends StatelessWidget {
final List<String> selectedItemsList;
NewPage({this.selectedItemsList});

  @override
  Widget build(BuildContext context) {
 

   
    return Container();
  }
}
class NewPage扩展了无状态小部件{
最终列表selectedItemsList;
新建页面({this.selectedItemsList});
@凌驾
小部件构建(构建上下文){
返回容器();
}
}
试试这个

 List<bool> isCheckedList = List.filled(2, false, growable: false);
 List<String> selectedItemsList = [];

    Widget pro() =>
      Expanded(
        child: ListView(
          children: ListTile.divideTiles(
            context: context,
            tiles: [
              options("Apple",0),
              options("Banana",1),
            ],
          ).toList(),
        ),
      );

  Widget options(String head,int index) =>
      CheckboxListTile(
        title: Text(head),
        value: isCheckedList[index],
        onChanged: (bool value) {
          setState(() {
             isCheckedList[index] = value;
             value ? selectedItemsList.add(head): selectedItemsList.removeWhere((item) => item == head);
          });
        },
      );
然后在新页面中,您可以通过这种方式检索它

class NewPage extends StatelessWidget {
final List<String> selectedItemsList;
NewPage({this.selectedItemsList});

  @override
  Widget build(BuildContext context) {
 

   
    return Container();
  }
}
class NewPage扩展了无状态小部件{
最终列表selectedItemsList;
新建页面({this.selectedItemsList});
@凌驾
小部件构建(构建上下文){
返回容器();
}
}

谢谢,似乎可以解决问题,但我仍需要打印选中的……您想打印在哪里?任何地方。最好是一个新的屏幕。@那家伙告诉我这是否有帮助。谢谢!我想知道我是否应该创建模型和AllThank,似乎这样做的技巧,但我仍然需要打印选中的…你想打印在哪里?任何地方。最好是一个新的屏幕。@那家伙告诉我这是否有帮助。谢谢!我想知道我是否应该创建模型和所有