Flutter Getx列表UI在添加到模型内的列表时未更新

igsr9ssn  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(333)

我正在尝试将项目添加到WorkoutModel内的列表中。
addWorkoutItem函数可以成功完成此操作,但ui不会更新。
我看过以前类似的帖子,并尝试了他们的建议,包括:

  • 确保我在锻炼模型的末尾添加了.obs
  • 使用addWorkoutItem函数中的workout.refresh()刷新训练
  • 用Obx Package 我的ui(如下所示)

有趣的是,我的deleteWorkoutItem函数工作正常,ui立即更新。

我的控制器:

class WorkoutTemplateController extends GetxController {
  final workout = WorkoutModel(
    name: 'Workout A',
    circuits: [1, 2],
    workoutItems: [],
  ).obs;
  

  addWorkoutItem(ExerciseModel exercise) {
    workout().workoutItems.add(WorkoutItemModel(
        name: exercise.name,
        exerciseList: [],
        numberOfSets: 3,
        workoutSets: []));
    workout.refresh();
  }

  deleteWorkoutItem(index) {
    workout().workoutItems.removeAt(index);
    workout.refresh();
  }

  
}

我的界面

Widget buildWorkoutItemList() => Obx(
        () => ReorderableListView.builder(
          buildDefaultDragHandles: false,
          shrinkWrap: true,
          primary: false,
          itemCount: controller.workout().workoutItems.length,
          itemBuilder: (context, index) {
            final item = controller.workout().workoutItems[index];

            return Slidable(
              key: Key('$index'),
              groupTag: 0,
              endActionPane: ActionPane(
                  extentRatio: 0.5,
                  motion: const DrawerMotion(),
                  children: [
                    SlidableAction(
                        onPressed: (BuildContext context) {
                          print('hello');
                        },
                        backgroundColor: const Color(0xff2e2e2e),
                        icon: Icons.refresh,
                        label: 'Swap'),
                    SlidableAction(
                        onPressed: (BuildContext context) {
                          controller.deleteWorkoutItem(index);
                          controller.activeIndex.value =
                              0; //TODO: update active index correctly
                        },
                        backgroundColor: Colors.red,
                        icon: Icons.delete,
                        label: 'Delete'),
                  ]),
              child: ListTile(
                key: Key('$index'),
                tileColor: const Color(0xff1e1e1e),
                title: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(item.numberOfSets.toString()),
                    const SizedBox(width: 10),
                    const Text('x'),
                    const SizedBox(width: 10),
                    Expanded(child: Text(item.name)),
                    Container(
                      width: 10,
                      height: 10,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        color: index == controller.activeIndex.value
                            ? Colors.blue
                            : Colors.transparent,
                      ),
                    ),
                    const SizedBox(width: 10),
                    ReorderableDragStartListener(
                        index: index,
                        child: const Icon(
                          Icons.drag_indicator,
                          color: Color(0xff3e3e3e),
                        )),
                  ],
                ),
                onTap: () {
                  controller.expandSheet();
                  controller.setActiveIndex(index);
                  controller.jumpTo(index);
                },
              ),
            );
          },
          onReorder: controller.reorderList,
        ),
      );
}

我的ui中调用addWorkoutItem的函数:

return Obx(
          () => ListTile(
            title: Text(result.name),
            onTap: () {
              controller.addWorkoutItem(result);
            },
          ),
        );
qcbq4gxm

qcbq4gxm1#

用户界面

class MyHomePage extends StatelessWidget {
     MyHomePage({super.key,});

  final controller = Get.put(SampleController2());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

        title: const Text('Sample'),
      ),
      body: Obx(() {
        return ListView.builder(
          itemBuilder: (c, i) => Text('$i Sample'),
          itemCount: controller.sampleModel.value.numberList.length,
        );
      }),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.sampleModel.value.numberList.add(8);
          controller.sampleModel.refresh();
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

控制器

class SampleController2 extends GetxController {

  Rx<SampleModel> sampleModel = SampleModel(
      'sampleName', '124578', [0, 1, 2, 3, 4, 5, 6,]).obs;  
}

视图模型

class SampleModel {

  String name;
  String id;
  List<int> numberList;

  SampleModel(this.name, this.id, this.numberList);
}

相关问题