Dustin Silk
Dustin Silk

Reputation: 4600

Flutter pop to new route

I have a ModalRoute that once I close, I'd like to replace the entire Navigation stack below it before calling .pop

The reason I want to pop rather than push is because I want to scale the modal down to reveal the new route underneath.

This is my ScaledRoute:

import 'package:flutter/material.dart';

class ScaleRoute extends PageRouteBuilder {
  final Widget page;

  ScaleRoute({this.page}) : super(
    pageBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
    ) => page,
    transitionsBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child,
    ) => ScaleTransition(
      scale: Tween<double>(
        begin: 0.0,
        end: 1.0,
      ).animate(
        CurvedAnimation(
          parent: animation,
          curve: Curves.fastOutSlowIn,
        ),
      ),
      child: child,
    ),
  );
}

This is how I'm opening the Modal that extends ModalRoute:

final confettiModal = ConfettiModal(
  onComplete: () {
    // Callback to close the modal to remove itself from the stack
    Navigator.pop(context);
  }
);

// New page is also a named route '/newPage'
final PageRouteBuilder route = ScaleRoute(page: NewPage());

// I can't seem to get this to work
Navigator.replaceRouteBelow(context, anchorRoute: null, newRoute: route);

Update

Here's my code that works. I needed to return the Modal's BuildContext to get the anchorRoute to replace up until.

final confettiModal = ConfettiModal(
  onComplete: (BuildContext modalContext) {
    final anchor = ModalRoute.of(modalContext);
    final page = MaterialPageRoute(builder: (context) => NewPage());
    Navigator.replaceRouteBelow(context, anchorRoute: anchor, newRoute: page);
    Navigator.pop(context);
  }
);

Navigator.push(context, confettiModal);

Upvotes: 1

Views: 1051

Answers (1)

returnVoid
returnVoid

Reputation: 644

anchorRoute in Navigator.replaceRouteBelow is required so it cannot be set to null. Documentation

Upvotes: 1

Related Questions