Flutter:自定义过渡(PageRouteBuilder)禁用Hero动画

问题描述 投票:0回答:1

我目前正在尝试使用PageRouteBuilder进行自定义转换。但是,当我使用它时,我的英雄动画将停止工作。

注意:它适用于其他过渡,甚至其他自定义过渡。

EnterExitRoute.dart

import 'package:flutter/material.dart';

class EnterExitRoute extends PageRouteBuilder {
  final Widget enterPage;
  final Widget exitPage;
  EnterExitRoute({this.exitPage, this.enterPage})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) {
            return enterPage;
          } ,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) {
              return Stack(
                children: <Widget>[
                  SlideTransition(
                    position: new Tween<Offset>(
                      begin: const Offset(0.0, 0.0),
                      end: const Offset(-1.0, 0.0),
                    ).animate(animation),
                    child: exitPage,
                  ),
                  SlideTransition(
                    position: new Tween<Offset>(
                      begin: const Offset(1.0, 0.0),
                      end: Offset.zero,
                    ).animate(animation),
                    child: enterPage,
                  )
                ],
              );
          }
        );
}

我的猜测是Hero动画无法跟上SlideTransition

关于如何解决此问题的任何想法?

flutter dart flutter-animation
1个回答
0
投票

正如pskink所说,问题不在使用child参考。

这里是更新后的工作代码

import 'package:flutter/material.dart';

class EnterExitRoute extends PageRouteBuilder {
  final Widget enterPage;
  final Widget exitPage;
  EnterExitRoute({this.exitPage, this.enterPage})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) {
            return enterPage;
          } ,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) {
              var curvedAnimation = CurvedAnimation(
              parent: animation,
              curve: Curves.ease,
              );
              return Stack(
                children: <Widget>[
                  SlideTransition(
                    position: new Tween<Offset>(
                      begin: const Offset(0.0, 0.0),
                      end: const Offset(-1.0, 0.0),
                    ).animate(curvedAnimation),
                    child: exitPage,
                  ),
                  SlideTransition(
                    position: new Tween<Offset>(
                      begin: const Offset(1.0, 0.0),
                      end: Offset.zero,
                    ).animate(curvedAnimation),
                    child: child,
                  )
                ],
              );
          }
        );
}
© www.soinside.com 2019 - 2024. All rights reserved.