flutter 普通路由切换动画效果

矫情吗;* 2021-07-24 20:06 616阅读 0赞
  1. 1、构造类来继承 PageRouteBuilder
  2. class xx extends PageRouteBuilder
  3. 2、构造函数传入组件,并重写父类构造器
  4. final Widget x;
  5. CusRoute(this.x):super(
  6. transitionDuration:Duration(seconds: 1), 动画时间
  7. pageBuilder:(BuildContext context,Animation<double> animation1,Animation<double> animation2)
  8. {
  9. return x;
  10. },
  11. transitionsBuilder:(
  12. BuildContext context,
  13. Animation<double> animation1,
  14. Animation<double> animation2,
  15. Widget child
  16. ){
  17. 实现动画
  18. return
  19. 1、渐变
  20. FadeTransition(
  21. child: child, 若混合其他动画,child内容直接放其他动画,优先级次于主动画
  22. opacity: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.动画曲线)),
  23. );
  24. 2、缩放
  25. ScaleTransition(
  26. child: child,
  27. scale: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.动画曲线)),
  28. );
  29. 3、旋转
  30. RotationTransition(
  31. child: child,
  32. turns: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.动画曲线)),
  33. );
  34. 4、先旋转后缩放
  35. RotationTransition(
  36. child:ScaleTransition(
  37. child: child,
  38. scale: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.动画曲线)),
  39. ) ,
  40. turns: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.动画曲线)),
  41. );
  42. 5、平滑
  43. SlideTransition(
  44. child: child,
  45. position: Tween<Offset>(begin:Offset(-1.0,0.0),end:Offset(0.0,0.0)).animate(CurvedAnimation(parent: animation1, curve: Curves.动画曲线)),
  46. -1.0代表最小位置
  47. 0.0代表最大位置
  48. );
  49. }
  50. );
  51. 3、使用动画
  52. 先引入动画类,在回调中
  53. Navigator.of(context).push(xx(路由组件()));

代码示例:
动画类:

  1. import 'package:flutter/material.dart';
  2. class CusRoute extends PageRouteBuilder{
  3. final Widget comp;
  4. CusRoute(this.comp):super(
  5. transitionDuration:Duration(seconds: 1),
  6. pageBuilder:(BuildContext context,Animation<double> animation1,Animation<double> animation2)
  7. {
  8. return comp;
  9. },
  10. transitionsBuilder:(
  11. BuildContext context,
  12. Animation<double> animation1,
  13. Animation<double> animation2,
  14. Widget child
  15. ){
  16. //实现动画
  17. return
  18. //渐变
  19. // FadeTransition(
  20. // child: child,
  21. // opacity: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.easeIn)),
  22. // );
  23. //缩放
  24. // ScaleTransition(
  25. // child: child,
  26. // scale: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.easeIn)),
  27. // );
  28. //旋转
  29. // RotationTransition(
  30. // child: child,
  31. // turns: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.easeIn)),
  32. // );
  33. //先旋转后缩放
  34. // RotationTransition(
  35. // child:ScaleTransition(
  36. // child: child,
  37. // scale: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.easeIn)),
  38. // ) ,
  39. // turns: Tween(begin: 0.0,end:1.0).animate(CurvedAnimation(parent: animation1, curve: Curves.easeIn)),
  40. // );
  41. //平滑
  42. SlideTransition(
  43. child: child,
  44. position: Tween<Offset>(begin:Offset(-1.0,0.0),end:Offset(0.0,0.0)).animate(CurvedAnimation(parent: animation1, curve: Curves.easeIn)),
  45. );
  46. }
  47. );
  48. }

跳转dart:

  1. import "package:flutter/material.dart";
  2. import "drag.dart";
  3. import 'page/1.dart';
  4. import 'page/2.dart';
  5. import 'page/3.dart';
  6. import 'xuan.dart';
  7. void main()
  8. {
  9. runApp(App());
  10. }
  11. class App extends StatelessWidget {
  12. const App({ Key key}) : super(key: key);
  13. @override
  14. Widget build(BuildContext context) {
  15. return App2();
  16. }
  17. }
  18. class App2 extends StatefulWidget {
  19. App2({ Key key}) : super(key: key);
  20. @override
  21. _AppState createState() => _AppState();
  22. }
  23. class _AppState extends State<App2> {
  24. List _page=[Home3(),Home2(),Home4()];
  25. int index=0;
  26. @override
  27. Widget build(BuildContext context) {
  28. return MaterialApp(
  29. home:Scaffold(
  30. appBar:AppBar(
  31. title: Text('小案例'),
  32. ),
  33. body:Home() ,
  34. floatingActionButton: FloatingActionButton(
  35. onPressed: (){ },
  36. tooltip: 'js',
  37. child: Icon(
  38. Icons.add,
  39. color: Colors.white,
  40. ),
  41. ),
  42. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  43. //导航栏
  44. bottomNavigationBar:BottomAppBar(
  45. color: Colors.lightBlue,
  46. //有缺口的圆形矩阵,用来放置浮动按钮
  47. shape: CircularNotchedRectangle(),
  48. child: Row(
  49. mainAxisAlignment:MainAxisAlignment.spaceEvenly,
  50. children: <Widget>[
  51. IconButton(
  52. icon:Icon(Icons.home),
  53. color: Colors.white,
  54. onPressed: (){ },
  55. ),
  56. IconButton(
  57. icon:Icon(Icons.category),
  58. color: Colors.white,
  59. onPressed: (){ },
  60. ),
  61. ],),
  62. )
  63. ),
  64. routes: {
  65. '/test':(context)=>Home4()
  66. },
  67. //去掉右上角的debug贴纸
  68. debugShowCheckedModeBanner: false,
  69. theme: ThemeData(
  70. primarySwatch: Colors.blue
  71. ),
  72. );
  73. }
  74. }
  75. class Home extends StatefulWidget {
  76. @override
  77. _HomeState createState() => _HomeState();
  78. }
  79. class _HomeState extends State<Home> {
  80. Color dragColor=Colors.orange;
  81. @override
  82. Widget build(BuildContext context) {
  83. return Container(
  84. width: double.infinity,
  85. color: Colors.red,
  86. child: Column(children: <Widget>[
  87. //跳转
  88. RaisedButton(
  89. child: Text('跳转'),
  90. onPressed: (){
  91. Navigator.of(context).push(CusRoute(Home4()));
  92. },
  93. ),
  94. ],)
  95. );
  96. }
  97. }

发表评论

表情:
评论列表 (有 0 条评论,616人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Css3动画切换效果

    开发工具与关键技术:DW 作者:文泽钦 撰写时间:2019年3月21日 实在不知道写什么,就翻了老师给的一些css样式看看有什么好玩的,老师用伪类做了一个鼠标移入到d