dart - flutter 复选框不起作用

标签 dart flutter flutter-layout flutter-dependencies

我想在这个小部件对话框中使用 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;
                });
              },
            )
       ],),)));
 }}

关于dart - flutter 复选框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51578824/

相关文章:

flutter - 如何将提供者的数据设置为Flutter中存储在SharedPreferences中的数据?

firebase - flutter 问题 : type 'Timestamp' is not a subtype of type 'DateTime'

angular - Dart中的 “Unable to find modules for some sources…”错误

flutter - Flutter for Web 生成的标签,<flt-*> 是什么?

flutter - 如何使用 google_maps_flutter 在标记内创建文本?

勾选框上的 flutter 圆角

flutter - 通过pubspec.yaml从内部存储库中提取依赖项

material-design - Flutter:如何让卡片可点击?

button - 为新的行 flutter 创建按钮

Flutter:使用 ListTile 时未布局 RenderBox