Reset order of children in ReorderableListView in Flutter

114 Views Asked by At

I am using the Flutterflow tool, if that matters.

I have created a ReorderableListView widget to display a list of exercises as part of a workout app ("workoutReLV"). The exercises are stored in a persistent app state variable. The workoutReLV is part of a "bottom sheet" (i.e. modal).

The workoutReLV works, but appears to preserve the order of its children after the modal closes. Shouldn't the order of the children be rebuilt using the persistent app state variable, and thus reset to that order? I'd expect that dismissing the modal and then opening it again would reset the order to whatever is saved on the disk.

Is this a feature of the ReorderableListView? I couldn't find a property to change this behavior. Can I implement a fix or workaround without making a custom ReorderableListView widget?

Here is the code for my workoutReLV:

// Automatic FlutterFlow imports
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:gains2/workout/workout_exercise/workout_exercise_widget.dart';

class WorkoutExerciseReorderableListView extends StatefulWidget {
  const WorkoutExerciseReorderableListView({
    Key? key,
    this.width,
    this.height,
    required this.workoutExercises,
    required this.userWorkoutsIndex,
  }) : super(key: key);

  final double? width;
  final double? height;
  final List<WorkoutExerciseStruct> workoutExercises;
  final int userWorkoutsIndex;

  @override
  _WorkoutExerciseReorderableListState createState() =>
      _WorkoutExerciseReorderableListState();
}

class _WorkoutExerciseReorderableListState
    extends State<WorkoutExerciseReorderableListView> {

  @override
  Widget build(BuildContext context) {
    List<WorkoutExerciseStruct> workoutExercises = widget.workoutExercises;

    return ReorderableListView(
      padding: const EdgeInsets.symmetric(horizontal: 40),
      children: <Widget>[
        for (int index = 0; index < workoutExercises.length; index += 1)
          WorkoutExerciseWidget(
            key: Key('${workoutExercises[index]}'),
            userExercisesIndex: workoutExercises[index].userExercisesIndex,
            exerciseSets: workoutExercises[index].sets,
          )
      ],
      onReorder: (int oldIndex, int newIndex) {
        setState(() {
          if (oldIndex < newIndex) {
            newIndex -= 1;
          }

          final WorkoutExerciseStruct movedWorkoutExerciseStruct =
              workoutExercises.removeAt(oldIndex);
          workoutExercises.insert(newIndex, movedWorkoutExerciseStruct);

          FFAppState().update(() {
            FFAppState().currentLiveWorkoutExercises = workoutExercises;
          });
        });
      },
    );
  }
}
0

There are 0 best solutions below