我正在尝试将项目添加到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);
},
),
);
1条答案
按热度按时间qcbq4gxm1#
用户界面
控制器
视图模型