【发布时间】:2022-01-20 21:38:25
【问题描述】:
有一个带有列表图块的抽屉,其中包含选中所有和取消选中所有按钮,当我单击选中所有按钮时,它应该选中所有列表项的复选框,但只有在我单击任何其他按钮后 UI 才会更改或更新,取消选中按钮也会产生同样的问题。
drawer: Drawer(
child: Obx(() => ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
decoration: BoxDecoration(
color: (Get.isDarkMode) ? Colors.black26 : Colors.grey[300],
),
accountName: Text('Reminders',
style: TextStyle(
fontSize: 30.0,
color: Theme.of(context).textTheme.headline1!.color,
)),
currentAccountPicture: CircleAvatar(
backgroundColor: Colors.white,
child: Image(
image: AssetImage("assets/App_icon.png"),
)),
accountEmail: null,
),
ListTile(
title: Text("Check all",
style: TextStyle(
fontSize: 17.0,
color: Theme.of(context).textTheme.headline1!.color)),
leading: Icon(Icons.check_box),
onTap: () {
checkAll();
},
),
ListTile(
title: Text("Uncheck all",
style: TextStyle(
fontSize: 17.0,
color: Theme.of(context).textTheme.headline1!.color)),
leading: Icon(Icons.check_box_outline_blank),
onTap: () {
unCheckAll();
},
),
ListTile(
title: Text(
(Get.isDarkMode)
? 'Change theme: light'
: 'change theme: dark',
style: TextStyle(
fontSize: 17.0,
color: Theme.of(context).textTheme.headline1!.color),
),
leading: Icon(Icons.color_lens_sharp),
onTap: () {
if (Get.isDarkMode) {
Get.changeThemeMode(ThemeMode.light);
} else {
Get.changeThemeMode(ThemeMode.dark);
}
},
),
ListTile(
title: Text(
"Delete all",
style: TextStyle(
fontSize: 17.0,
color: Theme.of(context).textTheme.headline1!.color),
),
leading: Icon(Icons.delete),
onTap: () {
todoController.todos.clear();
},
),
ListTile(
title: Text(
"No of tasks: ${todoController.todos.length}",
style: TextStyle(
fontSize: 20,
color: Theme.of(context).textTheme.headline1!.color),
),
onTap: () {},
),
],
)),
全选和取消全选功能:
void checkAll() {
TodoController todoController = Get.put(TodoController());
for (var i = 0; i < todoController.todos.length; i++) {
todoController.todos[i].done = true;
}
GetStorage().write('todos', todoController.todos.toList());
}
void unCheckAll() {
TodoController todoController = Get.put(TodoController());
for (var i = 0; i < todoController.todos.length; i++) {
todoController.todos[i].done = false;
}
GetStorage().write('todos', todoController.todos.toList());
}
【问题讨论】:
-
在
checkAll和unCheckAll中使用setState。 -
我试过了,它奏效了,我之前确实尝试过,将它设为有状态小部件并添加设置状态,但在将 Obx 与其他按钮一起使用时它有效,所以不知道是什么导致了问题.
标签: flutter user-interface flutter-getx