如何在推动路线抖动上创建淡入淡出过渡?

时间:2019-03-25 14:08:42

标签: flutter flutter-animation

我正在尝试为推入路线创建一个淡入淡出过渡,为此创建了一个自定义路线,例如

class CustomPageRoute<T> extends MaterialPageRoute<T> {
  CustomPageRoute({WidgetBuilder builder, RouteSettings settings})
      : super(builder: builder, settings: settings);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(opacity:animation, child: child,  );
  }
}

并通过按钮按下来调用

onPressed: () {
   Navigator.push(context, CustomPageRoute(builder: (context) {
       return FirstScreen();
   }));
}

但是,这给出了带有滑动+淡入淡出的怪异动画。 如何避免这种滑动动画?

这是我的代码的输出:

Here is the output

1 个答案:

答案 0 :(得分:1)

您应该从PageRoute开始而不是MaterialPageRoute

    class CustomPageRoute<T> extends PageRoute<T> {
      CustomPageRoute(this.child);
      @override
      // TODO: implement barrierColor
      Color get barrierColor => Colors.black;

      @override
      String get barrierLabel => null;

      final Widget child;

      @override
      Widget buildPage(BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) {
        return FadeTransition(
          opacity: animation,
          child: child,
        );
      }

      @override
      bool get maintainState => true;

      @override
      Duration get transitionDuration => Duration(milliseconds: 500);
    }

用法:

          final page = YourNewPage();
          Navigator.of(context).push(CustomPageRoute(page));