我有一个ModalRoute,一旦我关闭,我想在调用.pop之前替换它下面的整个导航堆栈

我要弹出而不是推送的原因是,我想缩小模式以显示下面的新路线。

这是我的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,
    ),
  );
}

这就是我打开扩展ModalRoute的Modal的方式:

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);

更新

这是我的代码。我需要返回Modal的BuildContext来使anchorRoute替换直到。

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);

最佳答案

Navigator.replaceRouteBelow中的anchorRoute是必需的,因此不能将其设置为null。
Documentation

关于flutter - flutter 到新路线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62149443/

10-10 12:49