dart - 颤振复选框不起作用
问题描述
我想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,
));
}
顺便说一句,onChange
当我点击复选框时会调用该方法。谁能解决这个问题?
解决方案
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;
});
},
);
});}
工作示例如下
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;
});
},
)
],),)));
}}
推荐阅读
- angularjs - 视图更改上的定位卡
- reactjs - 如何在 DevOps 中根据其发布阶段设置变量?
- delphi - 如何使用 Postman 向 Datasnap 服务器发送多个值
- css - 如何将字符串添加到 Django 模板?
- android - 尝试在真实设备上运行实时视频时,Exoplayer 显示空白屏幕
- javascript - 控制台日志返回 null
- scala - scala 中的“不符合 trait Builder 的类型参数界限”是什么意思?
- python - python中的字符串是可变的,如果不解释这个?
- android - 证明开源 Android
- c# - 验证几个字段不重复