首页 > 解决方案 > Flutter:CheckboxTile刻度线不变

问题描述

我正在尝试在 Flutter 中使用 Checkboxtile。但是,点击它并不会改变刻度的状态。这是代码。

bool checkedValue = false;
    CheckboxListTile(
    title: Text("title text"),
    value: checkedValue,
    onChanged: (newValue) { 
                 setState(() {
                   checkedValue = newValue; 
                 }); 
               },
    //onChanged: (newValue) { ... },
    controlAffinity: ListTileControlAffinity.leading,  //  <-- leading Checkbox
  ),

你能帮我找到问题吗。为什么它不起作用。

标签: flutter

解决方案


这可能对你有用。

class CheckBoxCustom extends StatefulWidget {
  @override
  _CheckBoxCustomState createState() => _CheckBoxCustomState();
}

class _CheckBoxCustomState extends State<CheckBoxCustom> {
  bool checkedValue = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: CheckboxListTile(
          title: Text("title text"),
          value: checkedValue,
          onChanged: (newValue) {
            setState(() {
              checkedValue = newValue;
            });
          },
          controlAffinity:
              ListTileControlAffinity.leading, //  <-- leading Checkbox
        ),
      ),
    );
  }
}

推荐阅读