dialog.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:flutter/material.dart';
  2. enum TransitionType {
  3. inFromLeft,
  4. inFromRight,
  5. inFromTop,
  6. inFromBottom,
  7. scale,
  8. fade,
  9. rotation,
  10. size,
  11. }
  12. Future<T?> showAnimationDialog<T>({
  13. required BuildContext context,
  14. bool barrierDismissible = true,
  15. Widget? child,
  16. required WidgetBuilder builder,
  17. bool useRootNavigator = true,
  18. RouteSettings? routeSettings,
  19. TransitionType transitionType = TransitionType.fade,
  20. }) {
  21. assert(debugCheckHasMaterialLocalizations(context));
  22. final ThemeData theme = Theme.of(context);
  23. return showGeneralDialog(
  24. context: context,
  25. pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
  26. final Widget pageChild = child ?? Builder(builder: builder);
  27. return Theme(data: theme, child: pageChild,
  28. );
  29. },
  30. barrierDismissible: barrierDismissible,
  31. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  32. barrierColor: Colors.transparent,
  33. transitionDuration: const Duration(milliseconds: 200),
  34. transitionBuilder: (context, animation1, animation2, child) {
  35. return _buildDialogTransitions(context, animation1, animation2, child, transitionType);
  36. },
  37. useRootNavigator: useRootNavigator,
  38. routeSettings: routeSettings,
  39. );
  40. }
  41. Widget _buildDialogTransitions(BuildContext context, Animation<double> animaton1, Animation<double> secondaryAnimation, Widget child, TransitionType type) {
  42. if (type == TransitionType.fade) {
  43. // 渐变效果
  44. return FadeTransition(
  45. // 从0开始到1
  46. opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
  47. // 传入设置的动画
  48. parent: animaton1,
  49. // 设置效果,快进漫出 这里有很多内置的效果
  50. curve: Curves.fastOutSlowIn,
  51. )),
  52. child: child,
  53. );
  54. } else if (type == TransitionType.scale) {
  55. return ScaleTransition(
  56. scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
  57. child: child,
  58. );
  59. } else if (type == TransitionType.rotation) {
  60. // 旋转加缩放动画效果
  61. return RotationTransition(
  62. turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
  63. parent: animaton1,
  64. curve: Curves.fastOutSlowIn,
  65. )),
  66. child: ScaleTransition(
  67. scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
  68. child: child,
  69. ),
  70. );
  71. } else if (type == TransitionType.inFromLeft) {
  72. // 左右滑动动画效果
  73. return SlideTransition(
  74. position: Tween<Offset>(begin: Offset(-1.0, 0.0), end: Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
  75. child: child,
  76. );
  77. } else if (type == TransitionType.inFromRight) {
  78. return SlideTransition(
  79. position: Tween<Offset>(begin: Offset(1.0, 0.0), end: Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
  80. child: child,
  81. );
  82. } else if (type == TransitionType.inFromTop) {
  83. return SlideTransition(
  84. position: Tween<Offset>(begin: Offset(0.0, -1.0), end: Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
  85. child: child,
  86. );
  87. } else if (type == TransitionType.inFromBottom) {
  88. return SlideTransition(
  89. position: Tween<Offset>(begin: Offset(0.0, 1.0), end: Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)),
  90. child: child,
  91. );
  92. } else if (type == TransitionType.size) {
  93. return SizeTransition(
  94. child: child,
  95. sizeFactor: Tween<double>(begin: 0.1, end: 1.0).animate(CurvedAnimation(parent: animaton1, curve: Curves.linear)),
  96. );
  97. } else {
  98. return child;
  99. }
  100. }