[英]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.