gpt4 book ai didi

flutter - 如何在推送路由 flutter 时创建淡入淡出过渡?

转载 作者:IT王子 更新时间:2023-10-29 06:57:42 27 4
gpt4 key购买 nike

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

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

最佳答案

您应该从 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));

关于flutter - 如何在推送路由 flutter 时创建淡入淡出过渡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55339718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com