【发布时间】:2020-09-20 18:29:27
【问题描述】:
我有一个 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);
【问题讨论】: