What would be a best practice for (provider based) state management of modal widgets in flutter, where when user makes an edit changes do not propagate to parent page until user confirms/closes modal widget. Optionally, user has a choice to discard the changes.
In a nutshell:
- modal widget with OK and cancel actions, or
- modal widget where changes are applied when modal is closed
Currently, my solution looks like this
- Create a copy of the current state
- Call flutter's show___() function and wrap widgets with a provider (using .value constructor) to expose copy of the state
- If needed, update original state when modal widget is closed
Example of case #2:
Future<void> showEditDialog() async {
// Create a copy of the current state
final orgState = context.read<MeState>();
final tmpState = MeState.from(orgState);
// show modal widget with new provider
await showDialog<void>(
context: context,
builder: (_) => ChangeNotifierProvider<MeState>.value(
value: tmpState,
builder: (context, _) => _buildEditDialogWidgets(context)),
);
// update original state (no discard option to keep it simple)
orgState.update(tmpState);
}
But there are issues with this, like:
- Where should I dispose tmpState?
- ProxyProvider doesn't have .value constructor.
- If temporary state is created in Provider's
create:
instead, how can I safely access that temporary state when modal is closed?