Repository: flutter
Issue: Transition animations fail to complete when sequentially popping a pageless route and a page-based route
Labels: framework,a: animation,f: routes,P2,team-framework,triaged-framework

## Steps to Reproduce

1. Execute `flutter run` on the code sample.
2. Click the button to push `SecondScreen` onto the navigation stack as a page route.
3. Click the button to push `AlertDialog` onto the navigation stack as a pageless route.
4. Click the `Exit` button in the dialog to pop `AlertDialog` and then `SecondScreen`.

**Expected results:**

`AlertDialog` should pop with a transition animation, and then `SecondScreen` should pop with a transition animation.

**Actual results:**

The `AlertDialog` pop animation begins but stops abruptly when `AlertDialog` and `SecondScreen` disappear suddenly.

**More Details**

The `DefaultTransitionDelegate` calls `markForComplete` rather than `markForPop` on the `PageRoute` for `SecondScreen` here:

```dart
        final bool hasPagelessRoute = pageRouteToPagelessRoutes.containsKey(exitingPageRoute);
        final bool isLastExitingPageRoute = isLast && !locationToExitingPageRoute.containsKey(exitingPageRoute);
        if (isLastExitingPageRoute && !hasPagelessRoute) {
          exitingPageRoute.markForPop(exitingPageRoute.route.currentResult);
        } else {
          exitingPageRoute.markForComplete(exitingPageRoute.route.currentResult);
        }
```

Perhaps there are reasons for this, but in my case this behavior is unexpected. Shouldn't the default behavior be to wait for existing pop transition animations to complete? There does not appear to be an easy solution for me. I can add a delay between the pop of the dialog and the pop of the page, but that is not ideal. There does not appear to be a way to await the transition animation between popping the dialog and the page. I could write my own transition delegate, but that seems over-complicated to achieve what in my case should be default behavior.

<details>
<summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      builder: (context, child) {
        return Builder(
          builder: (context) {
            return const CustomNavigator();
          },
        );
      },
    );
  }
}

class CustomNavigator extends StatefulWidget {
  const CustomNavigator({super.key});

  @override
  State<CustomNavigator> createState() => _CustomNavigatorState();
}

class _CustomNavigatorState extends State<CustomNavigator> {
  List<MaterialPage> pages = <MaterialPage>[];

  void push(Widget x) {
    pages.add(MaterialPage(child: x));
    if (mounted) setState(() {});
  }

  void pop() {
    pages.removeLast();
    if (mounted) setState(() {});
  }

  @override
  void initState() {
    super.initState();
    push(FirstScreen(push: push, pop: pop));
  }

  @override
  Widget build(BuildContext context) {
    return Navigator(
      onPopPage: (route, result) => false,
      pages: List.of(pages),
    );
  }
}

class FirstScreen extends StatelessWidget {
  const FirstScreen({
    super.key,
    required this.push,
    required this.pop,
  });

  final ValueChanged<Widget> push;
  final VoidCallback pop;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text('FirstScreen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () => push(SecondScreen(pop: pop)),
          child: const Text('SecondScreen'),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  const SecondScreen({
    super.key,
    required this.pop,
  });

  final VoidCallback pop;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: const Text('SecondScreen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            final exit = await showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  title: const Text('Pop SecondScreen?'),
                  actions: [
                    ElevatedButton(
                      onPressed: () => Navigator.pop(context, true),
                      child: const Text('Yes'),
                    ),
                    ElevatedButton(
                      onPressed: () => Navigator.pop(context, false),
                      child: const Text('No'),
                    ),
                  ],
                );
              },
            );
            if (exit ?? false) pop();
          },
          child: const Text('AlertDialog'),
        ),
      ),
    );
  }
}
```
</details>