onPressed在Flutter中创建CheckboxListTile的多个实例

时间:2020-07-21 05:12:51

标签: flutter checkboxlist flutter-onpressed

我是新手,所以我不确定该怎么做。我希望能够在用户按下最新CheckboxListTile下方的按钮时添加同一CheckboxListTile的另一个实例。

下面是当前代码。

Widget build(BuildContext context) {
return ListView(
  children: <Widget>[
    CheckboxListTile(
        title: TextField(
          autocorrect: true,
        ),
        value: checkBoxValue,
        secondary: Icon(Icons.assignment),
        onChanged: (bool newValue) {
          setState(() {
            checkBoxValue = newValue;
          });
        }
    ),
  ],

下面是我希望用户按下后如何显示应用程序的示例代码。

Widget build(BuildContext context) {
return ListView(
  children: <Widget>[
    CheckboxListTile(
        title: TextField(
          autocorrect: true,
        ),
        value: checkBoxValue,
        secondary: Icon(Icons.assignment),
        onChanged: (bool newValue) {
          setState(() {
            checkBoxValue = newValue;
          });
        }
    ),
    CheckboxListTile(
        title: TextField(
          autocorrect: true,
        ),
        value: checkBoxValue,
        secondary: Icon(Icons.assignment),
        onChanged: (bool newValue) {
          setState(() {
            checkBoxValue = newValue;
          });
        }
    ),
  ],

谢谢!

2 个答案:

答案 0 :(得分:2)

ListView中生成小部件的方法是在其子级中添加List<Widget>。简而言之,它看起来大致类似于。

ListView(
  children: <Widget>[widgetA, widgetA, widgetC]);

您需要做的是在List<Widget>中手动添加小部件。您可以创建一个List<Widget> _checkboxList并创建一个返回CheckboxListTile小部件的函数。

CheckboxListTile _checkboxListTile(){
  return CheckboxListTile(
    title: TextField(
      autocorrect: true,
    ),
    value: checkBoxValue,
    secondary: Icon(Icons.assignment),
    onChanged: (bool newValue) {
      setState(() {
        checkBoxValue = newValue;
      });
    }
  );
}

并在按钮上调用_checkboxList.add(_checkboxListTile());将其添加到List<Widget> _checkboxList

RaisedButton(
  onPressed: () {
    setState(() {
      _checkboxList.add(_checkboxListTile());
    });
  },
  child: Text('Add checkbox'),
),

要在屏幕上显示“列表_checkboxList”:

Widget build(BuildContext context) {
  return ListView(children: _checkboxList);
}

让我知道这是否有帮助。

答案 1 :(得分:1)

您可以使用的一种方法是像这样使用ListView.builder

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {

  List<bool> checkBoxesCheckedStates = [false];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: ListView.builder(
            itemCount: checkBoxesCheckedStates.length,
            itemBuilder: (BuildContext context, int index){
              return CheckboxListTile(
                title: TextField(
                  autocorrect: true,
                ),
                value: checkBoxesCheckedStates[index],
                secondary: Icon(Icons.assignment),
                onChanged: (bool newValue) {
                  setState(() {
                    checkBoxesCheckedStates[index] = newValue;
                  });
                },
              );
            },
          ),
        ),
        RaisedButton(
          child: Text('Add'),
          onPressed: (){
            setState(() {
              checkBoxesCheckedStates.add(false);
            });
          },
        ),
      ],
    );
  }
}