cah*_*why 6 dart flutter flutter-dependencies flutter-layout
我想CheckboxListTile在这个小部件对话框中构建一个复选框,但是当我点击复选框时,复选框checked上的复选框不会改变。
这是我的代码:
Future<Null> _showGroupDialog(BuildContext context) async {
await showDialog(
context: context,
builder: (BuildContext dialogContext) =>
Dialog(child: _buildCheckboxGroups(context)));
}
Widget _buildCheckboxGroups(BuildContext context) {
List<Widget> childrens = List.generate(_groups.length, (index) {
return CheckboxListTile(
title: Text(_groups[index].name),
value: _groups[index].checked,
onChanged: (bool val) {
setState(() {
_groups[index].checked = val;
});
},
);
});
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: childrens,
));
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,onChange当我点击复选框时会调用该方法。任何人都可以解决这个问题吗?
小智 3
class _MyHomePageState extends State<MyHomePage>{
//<Here you have to set default value for _groups[index].checked to false/true>
@override
Widget _buildCheckboxGroups(BuildContext context) {
List<Widget> childrens = List.generate(_groups.length, (index) {
return CheckboxListTile(
title: Text(_groups[index].name),
value: _groups[index].checked,
onChanged: (bool val) {
setState(() {
_groups[index].checked = val;
});
},
);
});}
Run Code Online (Sandbox Code Playgroud)
下面给出工作示例
class _MyHomePageState extends State<MyHomePage> {
bool flagWarranty=false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: new Form(
key: this._formKey,
child: new ListView(
children: <Widget>[
new Checkbox(
value: flagWarranty,
onChanged: (bool value) {
setState((
) {
flagWarranty=value;
});
},
)
],),)));
}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8869 次 |
| 最近记录: |