|
| 1 | +import 'dart:math' as math; |
| 2 | +import 'package:flutter/widgets.dart'; |
| 3 | +import 'package:flutter_animate/flutter_animate.dart'; |
| 4 | + |
| 5 | +/// An effect that adjust the size of target between the specified [begin] and [end] |
| 6 | +/// offset values. unlike [ScaleEffect], this effect does affect the real size |
| 7 | +/// of the widget. |
| 8 | +/// Defaults to `begin=0.0, end=1.0`. |
| 9 | +@immutable |
| 10 | +class SizeEffect extends Effect<double> { |
| 11 | + static const double neutralValue = 1.0; |
| 12 | + static const double defaultValue = 0.0; |
| 13 | + static const double defaultAxisAlignment = 0.0; |
| 14 | + |
| 15 | + const SizeEffect({ |
| 16 | + super.delay, |
| 17 | + super.duration, |
| 18 | + super.curve, |
| 19 | + double? begin, |
| 20 | + double? end, |
| 21 | + this.fixedWidthFactor, |
| 22 | + this.fixedHeightFactor, |
| 23 | + this.alignment, |
| 24 | + }) : super( |
| 25 | + begin: begin ?? (end == null ? defaultValue : neutralValue), |
| 26 | + end: end ?? neutralValue, |
| 27 | + ); |
| 28 | + |
| 29 | + final AlignmentGeometry? alignment; |
| 30 | + final double? fixedWidthFactor; |
| 31 | + final double? fixedHeightFactor; |
| 32 | + |
| 33 | + @override |
| 34 | + Widget build( |
| 35 | + BuildContext context, |
| 36 | + Widget child, |
| 37 | + AnimationController controller, |
| 38 | + EffectEntry entry, |
| 39 | + ) { |
| 40 | + final animation = buildAnimation(controller, entry); |
| 41 | + return getOptimizedBuilder<double>( |
| 42 | + animation: animation, |
| 43 | + builder: (_, __) { |
| 44 | + return ClipRect( |
| 45 | + child: Align( |
| 46 | + alignment: alignment ?? Alignment.center, |
| 47 | + widthFactor: fixedWidthFactor ?? math.max(animation.value, 0.0), |
| 48 | + heightFactor: fixedHeightFactor ?? math.max(animation.value, 0.0), |
| 49 | + child: child, |
| 50 | + ), |
| 51 | + ); |
| 52 | + }, |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// Adds [SizeEffect] related extensions to [AnimateManager]. |
| 58 | +extension SizeEffectExtensions<T extends AnimateManager<T>> on T { |
| 59 | + /// Adds a [SizeEffect] that adjust the size of target between |
| 60 | + /// the specified [begin] and [end] offset values. |
| 61 | + /// |
| 62 | + T size({ |
| 63 | + Duration? delay, |
| 64 | + Duration? duration, |
| 65 | + Curve? curve, |
| 66 | + double? begin, |
| 67 | + double? end, |
| 68 | + double? fixedWidthFactor, |
| 69 | + double? fixedHeightFactor, |
| 70 | + AlignmentGeometry? alignment, |
| 71 | + }) => |
| 72 | + addEffect(SizeEffect( |
| 73 | + delay: delay, |
| 74 | + duration: duration, |
| 75 | + curve: curve, |
| 76 | + begin: begin, |
| 77 | + end: end, |
| 78 | + alignment: alignment, |
| 79 | + fixedWidthFactor: fixedWidthFactor, |
| 80 | + fixedHeightFactor: fixedHeightFactor, |
| 81 | + )); |
| 82 | + |
| 83 | + /// Adds a [SizeEffect] that adjust the size of target horizontally between |
| 84 | + /// the specified [begin] and [end] values. |
| 85 | + /// |
| 86 | + /// [axisAlignment] describes how to align the child along the horizontal axis |
| 87 | + /// that [sizeFactor] is modifying. |
| 88 | + T sizeX({ |
| 89 | + Duration? delay, |
| 90 | + Duration? duration, |
| 91 | + Curve? curve, |
| 92 | + double? begin, |
| 93 | + double? end, |
| 94 | + double? axisAlignment, |
| 95 | + }) => |
| 96 | + addEffect(SizeEffect( |
| 97 | + delay: delay, |
| 98 | + duration: duration, |
| 99 | + curve: curve, |
| 100 | + begin: begin, |
| 101 | + end: end, |
| 102 | + fixedHeightFactor: 1.0, |
| 103 | + alignment: AlignmentDirectional( |
| 104 | + axisAlignment ?? SizeEffect.defaultAxisAlignment, -1.0))); |
| 105 | + |
| 106 | + /// Adds a [SizeEffect] that adjust the size of target vertically between |
| 107 | + /// the specified [begin] and [end] values. |
| 108 | + /// |
| 109 | + /// [axisAlignment] describes how to align the child along the vertical axis |
| 110 | + /// that [sizeFactor] is modifying. |
| 111 | + T sizeY({ |
| 112 | + Duration? delay, |
| 113 | + Duration? duration, |
| 114 | + Curve? curve, |
| 115 | + double? begin, |
| 116 | + double? end, |
| 117 | + double? axisAlignment, |
| 118 | + }) => |
| 119 | + addEffect(SizeEffect( |
| 120 | + delay: delay, |
| 121 | + duration: duration, |
| 122 | + curve: curve, |
| 123 | + begin: begin, |
| 124 | + end: end, |
| 125 | + fixedWidthFactor: 1.0, |
| 126 | + alignment: AlignmentDirectional( |
| 127 | + -1.0, |
| 128 | + axisAlignment ?? SizeEffect.defaultAxisAlignment, |
| 129 | + ))); |
| 130 | +} |
0 commit comments