Skip to content

Instantly share code, notes, and snippets.

@zzpmaster
Last active June 13, 2023 12:59
Show Gist options
  • Save zzpmaster/ee89fa97e5a0c02989fcc349456befdb to your computer and use it in GitHub Desktop.
Save zzpmaster/ee89fa97e5a0c02989fcc349456befdb to your computer and use it in GitHub Desktop.
flutter ModalBottomSheet update state
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<StatefulWidget> createState() {
return _ChoiceChipWidget();
}
}
class _ChoiceChipWidget extends State<ChoiceChipWidget> {
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<String> 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<ModalBottomSheet>
with SingleTickerProviderStateMixin {
List<String> 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<FormState> _formKey = GlobalKey<FormState>();
Future<void> _askedToLead() async {
switch (await showDialog<ALertState>(
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: <Widget>[
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<Widget> wrap = data
.map<Widget>((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: <Widget>[
Center(
child: Text(
'选择鱼类型',
style: TextStyle(fontSize: 18.0),
),
),
SizedBox(
height: 20.0,
),
Wrap(
spacing: 10.0,
children: wrap,
)
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment