Skip to content

Commit

Permalink
Complete #75: add animationBuilder(Support highly custom animation)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdd666t committed Sep 2, 2022
1 parent a41123b commit a5afab4
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Add displayTime (show/showAttach/showLoading)
- Add SmartMaskTriggerType ([#71](https://github.com/fluttercandies/flutter_smart_dialog/issues/71))
- Fix [#72](https://github.com/fluttercandies/flutter_smart_dialog/issues/72)
- Complete [#75](https://github.com/fluttercandies/flutter_smart_dialog/issues/75): add animationBuilder(Support highly custom animation)

# [4.3.x]

Expand Down
1 change: 1 addition & 0 deletions lib/flutter_smart_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export 'src/config/smart_config_toast.dart';
export 'src/init_dialog.dart';
export 'src/smart_dialog.dart';
export 'src/widget/dialog_scope.dart' show SmartDialogController;
export 'src/data/animation_param.dart' show AnimationParam;
4 changes: 4 additions & 0 deletions lib/src/compatible/compatible_smart_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class CompatibleSmartDialog {
animationType: isLoadingTemp ?? config.isLoading
? SmartAnimationType.centerFade_otherSlide
: SmartAnimationType.centerScale_otherSlide,
animationBuilder: null,
usePenetrate: isPenetrateTemp ?? config.isPenetrate,
useAnimation: isUseAnimationTemp ?? config.isUseAnimation,
animationTime: animationDurationTemp ?? config.animationDuration,
Expand Down Expand Up @@ -281,6 +282,7 @@ class CompatibleSmartDialog {
animationType: isLoadingTemp ?? false
? SmartAnimationType.fade
: SmartAnimationType.scale,
animationBuilder: null,
scalePointBuilder: null,
usePenetrate: isPenetrateTemp ?? config.isPenetrate,
useAnimation: isUseAnimationTemp ?? config.isUseAnimation,
Expand Down Expand Up @@ -377,6 +379,7 @@ class CompatibleSmartDialog {
animationType: isLoadingTemp ?? true
? SmartAnimationType.fade
: SmartAnimationType.scale,
animationBuilder: null,
usePenetrate: isPenetrateTemp ?? false,
useAnimation: isUseAnimationTemp ?? config.isUseAnimation,
animationTime: animationDurationTemp ?? config.animationDuration,
Expand Down Expand Up @@ -477,6 +480,7 @@ class CompatibleSmartDialog {
animationType: isLoadingTemp ?? true
? SmartAnimationType.fade
: SmartAnimationType.scale,
animationBuilder: null,
usePenetrate: isPenetrateTemp ?? true,
useAnimation: isUseAnimationTemp ?? true,
animationTime: animationDurationTemp ?? Duration(milliseconds: 200),
Expand Down
5 changes: 5 additions & 0 deletions lib/src/custom/custom_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter_smart_dialog/src/util/view_utils.dart';
import 'package:flutter_smart_dialog/src/widget/attach_dialog_widget.dart';

import '../config/enum_config.dart';
import '../data/animation_param.dart';
import '../data/base_dialog.dart';
import '../smart_dialog.dart';
import '../widget/smart_overlay_entry.dart';
Expand Down Expand Up @@ -41,6 +42,7 @@ class CustomDialog extends BaseDialog {
required bool useAnimation,
required Duration animationTime,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required Color maskColor,
required bool clickMaskDismiss,
required bool debounce,
Expand Down Expand Up @@ -72,6 +74,7 @@ class CustomDialog extends BaseDialog {
useAnimation: useAnimation,
animationTime: animationTime,
animationType: animationType,
animationBuilder: animationBuilder,
maskColor: maskColor,
maskWidget: maskWidget,
onDismiss: _handleDismiss(onDismiss, displayTime),
Expand All @@ -97,6 +100,7 @@ class CustomDialog extends BaseDialog {
required bool useAnimation,
required Duration animationTime,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required ScalePointBuilder? scalePointBuilder,
required Color maskColor,
required bool clickMaskDismiss,
Expand Down Expand Up @@ -133,6 +137,7 @@ class CustomDialog extends BaseDialog {
useAnimation: useAnimation,
animationTime: animationTime,
animationType: animationType,
animationBuilder: animationBuilder,
scalePointBuilder: scalePointBuilder,
maskColor: maskColor,
highlightBuilder: highlightBuilder,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/custom/custom_loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_smart_dialog/src/helper/dialog_proxy.dart';

import '../data/animation_param.dart';
import '../data/base_dialog.dart';
import '../widget/smart_overlay_entry.dart';

Expand All @@ -19,6 +20,7 @@ class CustomLoading extends BaseDialog {
required Widget widget,
required bool clickMaskDismiss,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required bool usePenetrate,
required bool useAnimation,
required Duration animationTime,
Expand All @@ -43,6 +45,7 @@ class CustomLoading extends BaseDialog {
return mainDialog.show<T>(
widget: widget,
animationType: animationType,
animationBuilder: animationBuilder,
alignment: Alignment.center,
maskColor: maskColor,
maskWidget: maskWidget,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/custom/custom_toast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:collection';
import 'package:flutter/material.dart';

import '../config/enum_config.dart';
import '../data/animation_param.dart';
import '../data/base_dialog.dart';
import '../smart_dialog.dart';
import '../widget/smart_overlay_entry.dart';
Expand All @@ -26,6 +27,7 @@ class CustomToast extends BaseDialog {
required AlignmentGeometry alignment,
required bool clickMaskDismiss,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required bool usePenetrate,
required bool useAnimation,
required Duration animationTime,
Expand Down Expand Up @@ -54,6 +56,7 @@ class CustomToast extends BaseDialog {
maskWidget: maskWidget,
animationTime: animationTime,
animationType: animationType,
animationBuilder: animationBuilder,
useAnimation: useAnimation,
usePenetrate: usePenetrate,
onDismiss: null,
Expand Down
7 changes: 6 additions & 1 deletion lib/src/custom/main_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_smart_dialog/src/widget/attach_dialog_widget.dart';
import 'package:flutter_smart_dialog/src/widget/smart_dialog_widget.dart';

import '../config/enum_config.dart';
import '../data/animation_param.dart';
import '../widget/smart_overlay_entry.dart';

///main function : customize dialog
Expand Down Expand Up @@ -41,11 +42,12 @@ class MainDialog {
required bool reuse,
required SmartAwaitOverType awaitOverType,
required SmartMaskTriggerType maskTriggerType,
required AnimationBuilder? animationBuilder,
}) {
//custom dialog
_widget = SmartDialogWidget(
key: reuse ? _uniqueKey : UniqueKey(),
controller: _controller = SmartDialogController(),
controller: _controller = SmartDialogWidgetController(),
alignment: alignment,
usePenetrate: usePenetrate,
useAnimation: useAnimation,
Expand All @@ -56,6 +58,7 @@ class MainDialog {
child: widget,
onMask: onMask,
maskTriggerType: maskTriggerType,
animationBuilder: animationBuilder,
);

_handleCommonOperate(
Expand All @@ -80,6 +83,7 @@ class MainDialog {
required bool useAnimation,
required Duration animationTime,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required ScalePointBuilder? scalePointBuilder,
required Color maskColor,
required Widget? maskWidget,
Expand All @@ -102,6 +106,7 @@ class MainDialog {
useAnimation: useAnimation,
animationTime: animationTime,
animationType: animationType,
animationBuilder: animationBuilder,
scalePointBuilder: scalePointBuilder,
maskColor: maskColor,
maskWidget: maskWidget,
Expand Down
131 changes: 131 additions & 0 deletions lib/src/data/animation_param.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import 'package:flutter/material.dart';

/// example:
///
/// ```dart
/// SmartDialog.show(
/// animationTime: const Duration(milliseconds: 3000),
/// animationBuilder: (
/// AnimationController controller,
/// Widget child,
/// AnimationParam animationParam,
/// ) {
/// return RotationTransition(
/// turns: CurvedAnimation(parent: controller, curve: Curves.elasticIn),
/// child: child,
/// );
/// },
/// builder: (_) {
/// return Container(
/// color: Colors.white,
/// padding: const EdgeInsets.all(30),
/// child: const Text('custom animation dialog'),
/// );
/// },
/// );
/// ```
typedef AnimationBuilder = Widget Function(
AnimationController controller,
Widget child,
AnimationParam animationParam,
);

/// advanced usage:
///
/// ```dart
/// SmartDialog.show(
/// animationTime: const Duration(milliseconds: 3000),
/// animationBuilder: (
/// AnimationController controller,
/// Widget child,
/// AnimationParam animationParam,
/// ) {
/// return CustomAnimation(child: child, animationParam: animationParam);
/// },
/// builder: (_) {
/// return Container(
/// color: Colors.white,
/// padding: const EdgeInsets.all(30),
/// child: const Text('custom animation dialog'),
/// );
/// },
/// );
///
/// class CustomAnimation extends StatefulWidget {
/// const CustomAnimation({
/// Key? key,
/// required this.child,
/// required this.animationParam,
/// }) : super(key: key);
///
/// final Widget child;
///
/// final AnimationParam animationParam;
///
/// @override
/// State<CustomAnimation> createState() => _CustomAnimationState();
/// }
///
/// class _CustomAnimationState extends State<CustomAnimation>
/// with TickerProviderStateMixin {
/// late AnimationController _controller;
///
/// @override
/// void initState() {
/// _controller = AnimationController(
/// vsync: this,
/// duration: widget.animationParam.animationTime,
/// );
/// widget.animationParam.onForward = () {
/// _controller.value = 0;
/// _controller.forward();
/// };
/// widget.animationParam.onDismiss = () {
/// _controller.reverse();
/// };
/// super.initState();
/// }
///
/// @override
/// Widget build(BuildContext context) {
/// return RotationTransition(
/// turns: CurvedAnimation(parent: _controller, curve: Curves.elasticIn),
/// child: widget.child,
/// );
/// }
///
/// @override
/// void dispose() {
/// _controller.dispose();
/// super.dispose();
/// }
/// }
/// ```
class AnimationParam {
AnimationParam({
required this.alignment,
required this.animationTime,
this.onForward,
this.onDismiss,
});

/// showXxx#alignment
AlignmentGeometry alignment;

/// showXxx#animationTime
Duration animationTime;

/// The animation starts callback, indicating that the dialog is about to open;
/// if you use custom or multiple AnimationControllers, the animation should be run using forward() in this callback
///
/// 动画开始回调, 表示该dialog即将打开; 如果使用自定义或多个AnimationController时,
/// 应该在此回调中使用forward()运行动画
VoidCallback? onForward;

/// The animation end callback, indicates that the dialog is about to close,
/// if you use custom or multiple AnimationController, you should use reverse() in this callback to reverse the animation
///
/// 动画结束回调, 表示该dialog即将关闭; 如果使用自定义或多个AnimationController时,
/// 应该在此回调中使用reverse()反转运行动画
VoidCallback? onDismiss;
}
9 changes: 9 additions & 0 deletions lib/src/helper/dialog_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter_smart_dialog/src/widget/toast_helper.dart';

import '../config/enum_config.dart';
import '../config/smart_config.dart';
import '../data/animation_param.dart';
import '../init_dialog.dart';
import '../widget/smart_overlay_entry.dart';

Expand Down Expand Up @@ -58,6 +59,7 @@ class DialogProxy {
required bool useAnimation,
required Duration animationTime,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required Color maskColor,
required bool clickMaskDismiss,
required Widget? maskWidget,
Expand All @@ -84,6 +86,7 @@ class DialogProxy {
useAnimation: useAnimation,
animationTime: animationTime,
animationType: animationType,
animationBuilder: animationBuilder,
maskColor: maskColor,
maskWidget: maskWidget,
clickMaskDismiss: clickMaskDismiss,
Expand All @@ -110,6 +113,7 @@ class DialogProxy {
required bool useAnimation,
required Duration animationTime,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required ScalePointBuilder? scalePointBuilder,
required Color maskColor,
required bool clickMaskDismiss,
Expand Down Expand Up @@ -141,6 +145,7 @@ class DialogProxy {
useAnimation: useAnimation,
animationTime: animationTime,
animationType: animationType,
animationBuilder: animationBuilder,
scalePointBuilder: scalePointBuilder,
maskColor: maskColor,
maskWidget: maskWidget,
Expand All @@ -162,6 +167,7 @@ class DialogProxy {
Future<T?> showLoading<T>({
required bool clickMaskDismiss,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required bool usePenetrate,
required bool useAnimation,
required Duration animationTime,
Expand All @@ -176,6 +182,7 @@ class DialogProxy {
return _loading.showLoading<T>(
clickMaskDismiss: clickMaskDismiss,
animationType: animationType,
animationBuilder: animationBuilder,
maskColor: maskColor,
maskWidget: maskWidget,
usePenetrate: usePenetrate,
Expand All @@ -193,6 +200,7 @@ class DialogProxy {
required AlignmentGeometry alignment,
required bool clickMaskDismiss,
required SmartAnimationType animationType,
required AnimationBuilder? animationBuilder,
required bool usePenetrate,
required bool useAnimation,
required Duration animationTime,
Expand All @@ -208,6 +216,7 @@ class DialogProxy {
alignment: alignment,
clickMaskDismiss: clickMaskDismiss,
animationType: animationType,
animationBuilder: animationBuilder,
usePenetrate: usePenetrate,
useAnimation: useAnimation,
animationTime: animationTime,
Expand Down
Loading

0 comments on commit a5afab4

Please sign in to comment.