flutter - ReorderableListView - 更改列表中的项目顺序后数据不刷新(我正在使用GetX)
问题描述
我想问为什么我的页面(数据)不刷新。我使用 ReorderableListView 对项目进行重新排序。更改位置后,我更新了我用作 obs (GetX) 的 RxList。但是当我重新排序这些项目时,它会变回原来的状态。我已经尝试过删除列表并重新创建该列表,尝试在更改项目顺序后刷新 RxList,但没有任何效果。我也尝试过 Obx() ,但也没有用。
我解决重新排序的视图类:
class SensorListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetX(
init: SensorListController(),
builder: (controller) {
List sensors = controller.sensors; //get data
RxList<Widget> listItems = <Widget>[].obs; //list where I change reordering of items
for (Sensor sensor in sensors) {
listItems.addAll([ // adding widgets to RxList
Padding(
key: ValueKey(sensor.manufacturerID),
padding: EdgeInsets.only(left: 24, top: 16, bottom: 8),
child: Text(
sensor.name ??
'Sensor ${sensor.manufacturerID}:${sensor.deviceID}',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
),
]);
for (SensorValue sensorValue in sensor.values) {
listItems.add(SensorTile(sensorValue, ValueKey(sensorValue.id))); // adding widgets to RxList
}
}
return ReorderableListView(
children: listItems.toList(),
onReorder: (int oldIndex, int newIndex) { //changing order of items
if (newIndex > listItems.length - 1) newIndex = listItems.length;
if (oldIndex < newIndex) newIndex -= 1;
final Widget item = listItems[oldIndex];
listItems.removeAt(oldIndex);
listItems.insert(newIndex, item);
listItems.refresh();
},
);
},
);
}
}
解决方案
推荐阅读
- node.js - 使用knex查找数据节点js
- c++ - 如何将委托^传递给c ++ / cli中的本机类
- android - 我想获取 json 数据的布尔“状态”的值,并根据 kotlin 中的值执行一些操作
- python - 二进制分类的损失没有减少
- python - 数据帧行的快速笛卡尔总和
- bash - 将 BASH 脚本转换为在 SH 上运行(通过 BusyBox)
- java - Payara 5.2020.4 未启动
- java - 从 Spring Security 过滤器返回自定义 http 错误消息
- css - Inline CSS looks different when looping through
- ios - Specific arbitrary location of movable views in a ScrollView in SwiftUI (no auto-layout)