【问题标题】:How to disable checkbox flutter如何禁用复选框颤动
【发布时间】:2019-02-09 22:44:27
【问题描述】:

我在 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


    【解决方案1】:

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

    【讨论】:

    • 知道如何禁用下拉按钮吗?
    • 我不确定是否有直接的方法可以做到这一点。您可能需要使用自定义小部件复制按钮。或者也许您可以扩展下拉按钮并使用实现。抱歉,我对此没有更好的想法
    【解决方案2】:
    Checkbox(value: false, onChanged: null)
    

    【讨论】:

      【解决方案3】:

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

      Here你可以看一个例子来了解如何使用它。

      您还可以从同一个人那里看到一个示例,他有一个关于单个小部件的完整系列,例如 Dropdown..

      希望对你有帮助。

      【讨论】:

      • Checkbox 小部件本身是无状态的(至少在它没有checked 状态的意义上)
      • 不明白为什么要把 Checkbox 设为无状态(大多数时候,你总是想跟踪状态)。必须将其包装在一个有状态的小部件中,以维护其状态。为什么不将 Checkbox 作为有状态小部件来维护状态本身?
      【解决方案4】:

      这是可以解决您问题的示例代码。

          class TaskTile extends StatefulWidget {
            const TaskTile({Key? key, required this.index}) : super(key: key);
            final int index;
      
            @override
            State<TaskTile> createState() => _TaskTileState();
          }
      
          class _TaskTileState extends State<TaskTile> {
            bool isChecked = false;
      
            @override
            Widget build(BuildContext context) {
              return ListTile(
                trailing: TaskCheckBox(
                    checkBoxState: isChecked, toggleCheckBoxState:  (bool? newCheckBoxState) {
              setState(() {
                isChecked = newCheckBoxState ?? isChecked;
              });
            }),
                title: Text(
                  'Task ${widget.index}',
                  style: TextStyle(
                    decoration:
                        isChecked ? TextDecoration.lineThrough : TextDecoration.none,
                  ),
                ),
              );
            }
          }
      
          class TaskCheckBox extends StatelessWidget {
            final bool checkBoxState;
            final void Function(bool?)? toggleCheckBoxState;
      
            const TaskCheckBox(
                {Key? key,
                required this.checkBoxState,
                required this.toggleCheckBoxState})
                : super(key: key);
            @override
            Widget build(BuildContext context) {
              return Checkbox(
                  fillColor: MaterialStateProperty.all<Color>(Colors.lightBlue),
                  value: checkBoxState,
                  onChanged: toggleCheckBoxState);
            }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多