[英]CheckBox ListTile in Flutter not working?
我正在構建一個 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!;
});
}),
),
],
),
),
),
],
),
);
}
}
看起來您的_valueCheck
變量是在 state 的build
方法中定義的。 嘗試將其定義為 class 成員,而不是_mainTaskScreenState
一個局部變量。
事實上, setState
可能會啟動另一個構建,但_valueCheck
值在構建發生時被重新定義為false
。
將 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.