2

我是新手,所以我不知道该怎么做。当用户按下最近的 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;
          });
        }
    ),
  ],

提前致谢!

4

2 回答 2

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'),
),

要在屏幕上显示 List _checkboxList:

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

让我知道这是否有帮助。

于 2020-07-21T05:27:10.310 回答
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);
            });
          },
        ),
      ],
    );
  }
}
于 2020-07-21T05:57:10.333 回答