简体   繁体   中英

onPressed create multiple instances of CheckboxListTile in Flutter

I'm new to flutter so I'm not sure how to go about this. I want to be able to add another instance of the same CheckboxListTile when a user presses a button right below the most recent CheckboxListTile.

The code for how it currently is is below.

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;
          });
        }
    ),
  ],

The code of an example of how I would want the app to appear after a user presses is below.

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;
          });
        }
    ),
  ],

Thanks in advance!

Generating widgets in a ListView is by adding List<Widget> in its children. Simply put, it roughly looks similar to.

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

What you need to do is to manually add widgets in List<Widget> . You can create a List<Widget> _checkboxList and create a function that returns a CheckboxListTile widget.

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

and on your button, call _checkboxList.add(_checkboxListTile()); to add it on List<Widget> _checkboxList

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

To display List _checkboxList on your screen:

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

Let me know if this helps.

One way you could do it is by using a ListView.builder like this..

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);
            });
          },
        ),
      ],
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM