【问题标题】:Flutter pop to new route颤振流行到新路线
【发布时间】: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);

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-24
      • 2018-02-11
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2021-12-11
      相关资源
      最近更新 更多