首页 > 解决方案 > 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();
          },
        );
      },
    );
  }
}

标签: flutterdart

解决方案


推荐阅读