showModalBottomSheet( context: context, builder: (context) { return ModalBottomSheet(this.selected, this.data, (selected, data) { this.selected = selected; this.data = data; }); }); import 'package:flutter/material.dart'; class ChoiceChipWidget extends StatefulWidget { final String label; final bool selected; final Function callback; ChoiceChipWidget(this.label, this.selected, {this.callback}); @override State createState() { return _ChoiceChipWidget(); } } class _ChoiceChipWidget extends State { bool _value; @override void initState() { _value = widget.selected; super.initState(); } @override Widget build(BuildContext context) { return ChoiceChip( label: Text(widget.label), selected: _value, onSelected: (value) { setState(() { _value = value; }); widget.callback(widget.label); }, ); } } import 'package:chip/choice_chip_widget.dart'; import 'package:flutter/material.dart'; class ModalBottomSheet extends StatefulWidget { final List selected; final List data; final Function synchronous; ModalBottomSheet(this.selected, this.data, this.synchronous); _ModalBottomSheetState createState() => _ModalBottomSheetState(); } enum ALertState { OK, CANCEL } class _ModalBottomSheetState extends State with SingleTickerProviderStateMixin { List selected; List data; @override void initState() { selected = widget.selected; data = widget.data; super.initState(); } @override void dispose() { widget.synchronous(selected, data); super.dispose(); } void increase(String label) { selected.contains(label) ? selected.remove(label) : selected.add(label); } final GlobalKey _formKey = GlobalKey(); Future _askedToLead() async { switch (await showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return AlertDialog( title: Form( key: _formKey, child: TextFormField( decoration: InputDecoration( hintText: '鱼饵名称', hintStyle: TextStyle( fontSize: 16.0, )), keyboardType: TextInputType.text, validator: (String value) { if (value.isEmpty) { return '鱼饵名称不能为空'; } }, onSaved: (String value) { setState(() { data.add(value); }); }), ), actions: [ FlatButton( onPressed: () { if (!_formKey.currentState.validate()) { return; } Navigator.pop(context, ALertState.OK); }, child: const Text('OK'), ), FlatButton( onPressed: () { Navigator.pop(context, ALertState.CANCEL); }, child: const Text('CANCEL'), ), ], ); })) { case ALertState.OK: // Let's go. // ... print('OK'); _formKey.currentState.save(); break; case ALertState.CANCEL: // ... print('CANCEL'); break; } } Widget build(BuildContext context) { List wrap = data .map((item) => ChoiceChipWidget(item, selected.contains(item), callback: increase)) .toList(); wrap.add(IconButton( icon: Icon(Icons.add), onPressed: () => _askedToLead(), )); return Container( padding: EdgeInsets.only(top: 10.0, left: 4.0), child: Column( children: [ Center( child: Text( '选择鱼类型', style: TextStyle(fontSize: 18.0), ), ), SizedBox( height: 20.0, ), Wrap( spacing: 10.0, children: wrap, ) ], ), ); } }