dev2019
dev2019

Reputation: 71

GoRouter Pop Animation Skipped When Dismissing Alert Dialog

I am using GoRouter in Flutter to handle navigation and show a page with a slide-up animation.

However, when I try to show a confirmation dialog (AlertDialog) before dismissing the page, the slide-down animation does not play unless I add

Future.delayed(Duration(milliseconds: 200)) before calling context.pop()

Without the delay, the dialog and the page both close instantly, skipping the expected transition animation.

My AlertDialog:

Future<bool?> _showBackDialog(BuildContext context) {
    return showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Are you sure?'),
          content: const Text('Are you sure you want to leave this page?'),
          actions: <Widget>[
            TextButton(
              style: TextButton.styleFrom(textStyle: Theme.of(context).textTheme.labelLarge),
              child: const Text('Cancel'),
              onPressed: () {
                context.pop(false);
              },
            ),
            TextButton(
              style: TextButton.styleFrom(textStyle: Theme.of(context).textTheme.labelLarge),
              child: const Text('Exit'),
              onPressed: () {
                context.pop(true);
              },
            ),
          ],
        );
      },
    );
  }

And part of the widget where I handle the logic:

...
return PopScope(
          canPop: false,
          onPopInvokedWithResult: (didPop, _) async {
            if (didPop) return;
            final bool shouldPop = await _showBackDialog(context) ?? false;
            if (shouldPop) {
              await Future.delayed(Duration(milliseconds: 200));
              if (context.mounted && context.canPop()) {
                context.pop();
              }
            }
          },
...

Is there a better way to handle this transition without using an artificial delay?

Upvotes: 0

Views: 32

Answers (0)

Related Questions