简体   繁体   English

Flutter 中的 CheckBox ListTile 不起作用?

[英]CheckBox ListTile in Flutter not working?

I'm building a ToDo App, and CheckboxListTile not working and I don't know what went wrong, can anyone help?我正在构建一个 ToDo 应用程序,但CheckboxListTile无法正常工作,我不知道出了什么问题,有人可以帮忙吗?

    class mainTaskScreen extends StatefulWidget {
      const mainTaskScreen({Key? key}) : super(key: key);
    
      @override
      State<mainTaskScreen> createState() => _mainTaskScreenState();
    }
    
    class _mainTaskScreenState extends State<mainTaskScreen> {
      @override
      Widget build(BuildContext context) {
        bool _valueCheck = false;
        return Scaffold(
          backgroundColor: const Color(0XFF7FC8F8),
    //------------------------------------------------------------------------------
    // AddTask Button...
          floatingActionButton: FloatingActionButton(
            onPressed: (() {}),
            backgroundColor: AppColor.mainColor,
            child: const Icon(FontAwesomeIcons.plus),
          ),
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding:
                    const EdgeInsets.only(top: 60, left: 30, right: 30, bottom: 10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
    //------------------------------------------------------------------------------
    // Menu Button..
                    (ElevatedButton(
                      style: ElevatedButton.styleFrom(
                          primary: AppColor.accentColor,
                          onPrimary: AppColor.mainColor,
                          fixedSize: const Size(70, 70),
                          shape: const CircleBorder()),
                      onPressed: (() {}),
                      child: const Icon(FontAwesomeIcons.listUl, size: 30),
                    )),
                    const SizedBox(height: 10),
    //------------------------------------------------------------------------------
    // Title...
                    Text('Todoey', style: AppFonts.titleStyle),
    //------------------------------------------------------------------------------
    // Task's Num...
                    Text('12 Task', style: AppFonts.smallStyle),
                  ],
                ),
              ),
    //------------------------------------------------------------------------------
    // Task's List...
              Expanded(
                child: Container(
                  padding: const EdgeInsets.only(left: 20),
                  width: double.infinity,
                  decoration: const BoxDecoration(
                    color: AppColor.accentColor,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(25),
                      topRight: Radius.circular(25),
                    ),
                  ),
                  child: ListView(
                    children: [
                      CheckboxListTile(
                        title: Text('Clean you room', style: taskText.smallStyle),
                        subtitle:
                            const Text('remove the trach + clean your cloths'),
                        activeColor: AppColor.accentColor,
                        checkColor: AppColor.mainColor,
                        value: _valueCheck,
                        selected: _valueCheck,
                        onChanged: ((value) {
                          setState(() {
                            _valueCheck = value!;
                          });
                        }),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      }

}

在此处输入图像描述

It looks like your _valueCheck variable is defined in the build method of the state.看起来您的_valueCheck变量是在 state 的build方法中定义的。 Try defining it as a class member of instead of _mainTaskScreenState a local variable instead.尝试将其定义为 class 成员,而不是_mainTaskScreenState一个局部变量。

As it is, the setState is likely kicking off another build, but the _valueCheck value is redefined as false when that build happens.事实上, setState可能会启动另一个构建,但_valueCheck值在构建发生时被重新定义为false

Move the bool outside the build method将 bool 移到 build 方法之外

class _mainTaskScreenState extends State<mainTaskScreen> {
bool _valueCheck = false; //<-- here
  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM