简体   繁体   中英

How do you dynamically add widgets to a row?

I have a DropdownButton (entries of type String) and an IconButton in a Row widget. Each time an entry from the DropdownButton is selected and the IconButton is tapped, an InputChip with the selected entry as its child should be created in a Row widget below the initial Row. How can this be done?

class _MyHomePageState extends State<MyHomePage> {
  List<String> templist = new List();

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Scaffold(
        body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Wrap(
          children: templist
              .map(
                (f) => Chip(
                  label: Text(f),
                  deleteIcon: CircleAvatar(
                    minRadius: 10.0,
                    child: Icon(
                      Icons.clear,
                      color: Colors.white,
                      size: 20,
                    ),
                  ),
                  deleteIconColor: Colors.red,
                  onDeleted: () {
                    setState(() {
                      templist.remove(f);
                    });
                  },
                ),
              )
              .toList(),
        ),
        new DropdownButton<String>(
          isExpanded: true,
          hint: Text("Click here"),
          items: <String>['A', 'B', 'C', 'D'].map((String value) {
            return new DropdownMenuItem<String>(
              value: value,
              child: new Text(value),
            );
          }).toList(),
          onChanged: (value) {
            setState(() {
              templist.add(value);
            });
          },
        )
      ],
    ));
  }
} 

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