如何禁用复选框颤振

问题描述 投票:1回答:3

我在ListTile中使用Checkbox,如下所示:

 ListTile(
  leading: Checkbox(
  value: _isChecked,
    onChanged: (v) {
      setState(() {
          _isChecked = !_isChecked;
      });
    },
  ),
  title: Text("is Bathroom"),
);

如何禁用该复选框。我知道Checkbox小部件是无状态的。但是在材料子包中提供的任何其他Widget都可以做到这一点。像InputDecorator这样的东西。

我也有与DropdownButton相同的问题。我正在使用它如下从下拉列表中选择表单中的项目。

             InputDecorator(
                decoration: InputDecoration(
                  labelText: "Type",
                  hintText: "Choose the type",
                ),
                isEmpty: _type == null,
                child: DropdownButton<int>(
                  value: _type,
                  isDense: true,
                  onChanged: (value) {
                    setState(() {
                      _type = value;
                    });
                  },
                  items: _buildDropdownItemList(),
                ),
              );

我在InputDecoration中尝试了enable参数,但这只是改变了装饰。用户仍然可以更改选择。

dart flutter flutter-layout
3个回答
4
投票

您可以将null传递给onChanged属性,这将禁用该复选框。


2
投票
Checkbox(value: false, onChanged: null)

1
投票

您可以使用statefuldwiget中的setstate进行复选框状态更改,我将在youtube中找到一个示例。

Here你可以看一个关于如何使用它的例子。

你也可以从同一个人那里看到一个例子,他有一个关于个别小部件的完整系列,比如Dropdown ..

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.