Flutter 向上滑动屏幕更改动画
我对颤振开发还不熟悉,我想在更改屏幕时创建这种动画(第一页也会向上滑动)。我搜索了有关这方面的教程和帖子,但没有找到。有办法做到这一点吗Flutter 向上滑动屏幕更改动画,flutter,flutter-animation,Flutter,Flutter Animation,我对颤振开发还不熟悉,我想在更改屏幕时创建这种动画(第一页也会向上滑动)。我搜索了有关这方面的教程和帖子,但没有找到。有办法做到这一点吗 这是工作代码。您可以尝试从更改反向动画的持续时间和值。但目前看来,它的效果相当不错 class _MyWidgetState extends State<MyWidget> with SingleTickerProviderStateMixin { AnimationController _animationController;
这是工作代码。您可以尝试从更改反向动画的持续时间和值。但目前看来,它的效果相当不错
class _MyWidgetState extends State<MyWidget>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
@override
void initState() {
_animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 2000)); // you can try to set another duration
super.initState();
}
@override
Widget build(BuildContext context) {
_animationController.reverse(from: 0.2); // you can try to set another value for from
return SlideTransition(
position: Tween<Offset>(
begin: Offset.zero,
end: const Offset(0.0, -1.0),
).animate(_animationController),
child: Scaffold(
appBar: AppBar(
title: Text('MyWidget'),
),
body: _createBody(),
));
}
Widget _createBody() {
// create body here
// perform this action on click:
Navigator.of(context).push(getRoute());
}
PageRoute getRoute() {
_animationController.forward(from: 0.0);
return PageRouteBuilder(
pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
return MySecondScreen();
},
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(0.0, 1.0),
end: Offset.zero,
).animate(animation),
child: SlideTransition(
position: Tween<Offset>(
begin: Offset.zero,
end: const Offset(0.0, 1.0),
).animate(secondaryAnimation),
child: child,
),
);
},
);
}
dispose() {
super.dispose();
_animationController.dispose();
}
}
class\u MyWidgetState扩展状态
使用SingleTickerProviderStateMixin{
AnimationController _AnimationController;
@凌驾
void initState(){
_animationController=animationController(vsync:this,duration:duration(毫秒:2000));//您可以尝试设置另一个持续时间
super.initState();
}
@凌驾
小部件构建(构建上下文){
_animationController.reverse(from:0.2);//您可以尝试为from设置另一个值
返回幻灯片转换(
位置:吐温(
开始:Offset.zero,
结束:常数偏移(0.0,-1.0),
).animate(_animationController),
孩子:脚手架(
appBar:appBar(
标题:文本(“MyWidget”),
),
正文:_createBody(),
));
}
小部件_createBody(){
//在这里创建身体
//在单击时执行此操作:
Navigator.of(context.push)(getRoute());
}
PageRoute getRoute(){
_animationController.forward(从:0.0);
返回页路由生成器(
pageBuilder:(构建上下文、动画、动画辅助动画){
返回MySecondScreen();
},
transitionsBuilder:(构建上下文、动画、动画辅助动画、小部件子对象){
返回幻灯片转换(
位置:吐温(
开始:常数偏移(0.0,1.0),
结束:偏移0.0,
).制作动画(动画),
子:幻灯片转换(
位置:吐温(
开始:Offset.zero,
结束:常数偏移(0.0,1.0),
).制作动画(第二动画),
孩子:孩子,
),
);
},
);
}
处置{
super.dispose();
_animationController.dispose();
}
}
这应该是可能的;Flitter非常关注UI。看看这个,它起作用了!我刚刚添加了曲线动画以实现平滑过渡。美好的