148 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
		
		
			
		
	
	
			148 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Dart
		
	
	
	
|  | 
 | ||
|  | import 'package:flutter/material.dart'; | ||
|  | 
 | ||
|  | enum TransitionType { | ||
|  |   ///左侧弹出
 | ||
|  |   inFromLeft, | ||
|  |   ///右侧弹出
 | ||
|  |   inFromRight, | ||
|  |   ///顶部弹出
 | ||
|  |   inFromTop, | ||
|  |   ///底部弹出
 | ||
|  |   inFromBottom, | ||
|  |   ///缩放
 | ||
|  |   scale, | ||
|  |   ///渐变
 | ||
|  |   fade, | ||
|  |   ///旋转缩放
 | ||
|  |   rotation, | ||
|  |   ///放大
 | ||
|  |   size, | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  |   //弹出动画
 | ||
|  |   // barrierDismissible:点击背景是否消失
 | ||
|  |   // child,builder:子 widget
 | ||
|  |   // useRootNavigator: 是否推入传入 context的 导航器
 | ||
|  |   // routeSettings: 弹窗路由设置
 | ||
|  |   Future<T?> showAnimationDialog<T>({ | ||
|  |     required BuildContext context, | ||
|  |     bool barrierDismissible = true, | ||
|  |     Widget? child, | ||
|  |     WidgetBuilder? builder, | ||
|  |     bool useRootNavigator = true, | ||
|  |     RouteSettings? routeSettings, | ||
|  |     TransitionType? transitionType, | ||
|  |   }) { | ||
|  |     assert(child == null || builder == null); | ||
|  |     assert(debugCheckHasMaterialLocalizations(context)); | ||
|  | 
 | ||
|  |     final ThemeData theme = Theme.of(context); | ||
|  |     return showGeneralDialog( | ||
|  |       context: context, | ||
|  |       pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) { | ||
|  |         final Widget pageChild = child ?? Builder(builder: builder!); | ||
|  |         return SafeArea( | ||
|  |           child: Builder(builder: (BuildContext context) { | ||
|  |             return Theme(data: theme, child: pageChild); | ||
|  |           }), | ||
|  |         ); | ||
|  |       }, | ||
|  |       barrierDismissible: barrierDismissible, ///点击背景是否消失
 | ||
|  |       barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel, | ||
|  |       barrierColor: Colors.black38, ///背景色
 | ||
|  |       transitionDuration: const Duration(milliseconds: 200),///动画时间
 | ||
|  |       transitionBuilder: (context, animation1, animation2, child) { ///动画效果
 | ||
|  |         return _buildDialogTransitions(context, animation1, animation2, child, transitionType?? TransitionType.inFromBottom); | ||
|  |       }, | ||
|  |       useRootNavigator: useRootNavigator, | ||
|  |       routeSettings: routeSettings, | ||
|  |     ); | ||
|  |   } | ||
|  | 
 | ||
|  | 
 | ||
|  |   ///返回动画效果
 | ||
|  |   Widget _buildDialogTransitions( | ||
|  |       BuildContext context, Animation<double> animaton1, Animation<double> secondaryAnimation, Widget child, TransitionType type) { | ||
|  | 
 | ||
|  |     /// 渐变效果
 | ||
|  |     if (type == TransitionType.fade) { | ||
|  |       return FadeTransition( | ||
|  |         // 从0开始到1
 | ||
|  |         opacity: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation( | ||
|  |           // 传入设置的动画
 | ||
|  |           parent: animaton1, | ||
|  |           // 设置效果,快进漫出   这里有很多内置的效果
 | ||
|  |           curve: Curves.fastOutSlowIn, | ||
|  |         )), | ||
|  |         child: child, | ||
|  |       ); | ||
|  | 
 | ||
|  |       ///缩放
 | ||
|  |     } else if (type == TransitionType.scale) { | ||
|  |       return ScaleTransition( | ||
|  |         scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: animaton1, curve: Curves.easeOutBack)),//Curves.easeOutBack
 | ||
|  |         child: child, | ||
|  |       ); | ||
|  | 
 | ||
|  |       /// 旋转加缩放动画效果
 | ||
|  |     } else if (type == TransitionType.rotation) { | ||
|  | 
 | ||
|  |       return RotationTransition( | ||
|  |         turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation( | ||
|  |           parent: animaton1, | ||
|  |           curve: Curves.fastOutSlowIn, | ||
|  |         )), | ||
|  |         child: ScaleTransition( | ||
|  |           scale: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)), | ||
|  |           child: child, | ||
|  |         ), | ||
|  |       ); | ||
|  | 
 | ||
|  |       /// 左滑出动画效果
 | ||
|  |     } else if (type == TransitionType.inFromLeft) { | ||
|  | 
 | ||
|  |       return SlideTransition( | ||
|  |         position: Tween<Offset>(begin: Offset(-1.0, 0.0), end: Offset(0.0, 0.0)) | ||
|  |             .animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)), | ||
|  |         child: child, | ||
|  |       ); | ||
|  | 
 | ||
|  |       /// 右滑出动画效果
 | ||
|  |     } else if (type == TransitionType.inFromRight) { | ||
|  |       return SlideTransition( | ||
|  |         position: Tween<Offset>(begin: Offset(1.0, 0.0), end: Offset(0.0, 0.0)) | ||
|  |             .animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)), | ||
|  |         child: child, | ||
|  |       ); | ||
|  | 
 | ||
|  |       /// 顶部滑出动画效果
 | ||
|  |     } else if (type == TransitionType.inFromTop) { | ||
|  |       return SlideTransition( | ||
|  |         position: Tween<Offset>(begin: Offset(0.0, -1.0), end: Offset(0.0, 0.0)) | ||
|  |             .animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)), | ||
|  |         child: child, | ||
|  |       ); | ||
|  | 
 | ||
|  |       /// 底部滑出动画效果
 | ||
|  |     } else if (type == TransitionType.inFromBottom) { | ||
|  |       return SlideTransition( | ||
|  |         position: Tween<Offset>(begin: Offset(0.0, 1.0), end: Offset(0.0, 0.0)) | ||
|  |             .animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)), | ||
|  |         child: child, | ||
|  |       ); | ||
|  | 
 | ||
|  |       /// 放大出显动画效果
 | ||
|  |     } else if (type == TransitionType.size) { | ||
|  |       return ScaleTransition( | ||
|  |         child: child, | ||
|  |         scale: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(parent: animaton1, curve: Curves.fastOutSlowIn)), | ||
|  |       ); | ||
|  |     } else { | ||
|  |       return child; | ||
|  |     } | ||
|  |   } | ||
|  | 
 | ||
|  | 
 |