有一個帶有串列圖塊的抽屜,其中包含“全選”和“取消全選”按鈕,當我單擊“全選”按鈕時,它應該選中所有串列項的復選框,但只有在單擊任何其他按鈕(取消選中按鈕)后 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());
}
uj5u.com熱心網友回復:
雖然看起來很奇怪,但這樣做會解決它:
....
todoController.todos.assignAll(todoController.todos.toList());
GetStorage().write('todos', todoController.todos.toList());
為什么?
假設todoController.todos是一個RxList,更改串列中各個元素的屬性對底層流沒有影響。因此,盡管更改了值,它也不會觸發Obx.
但是做一些類似的事情todoController.todos.assignAll(todoController.todos.toList());實際上會改變 RxList 的底層流/值。因此觸發重建,因為它應該是。
使用Rx或observables 時,請始終記住,Obx/GetX只有value在Rx/Observable的實際值發生更改時才會觸發重建。類似的todoController.todos[i].done = true;意思是更改RxList 的第i個元素的done屬性值,而不是實際RxList的值。這樣做。assignAll
這同樣適用于其他Rx。假設以下是該類的Rx物件SomeModel:
final someModel= SomeModel().obs;
做類似的事情:
someModel.value.someProperty = someValue;
不會觸發 UI 重建,而是執行以下操作:
someModel.value.someProperty = someValue;
someModel.value = someModel.value;
將要。
但這有點奇怪吧?在這種情況下,copyWith實用程式方法之類的東西會很有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387466.html
下一篇:在Windows上按X時,PySimpleGUIone_line_progress_meter不會回傳False
