Skip to content

Commit ad60ae7

Browse files
committed
feat: Added Size Effect
1 parent 62c1204 commit ad60ae7

File tree

3 files changed

+141
-5
lines changed

3 files changed

+141
-5
lines changed

example/lib/examples/everything_view.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import 'dart:math';
55
import 'dart:ui' as ui;
66

7+
import 'package:flutter/cupertino.dart';
78
import 'package:flutter/material.dart';
89
import 'package:flutter_animate/flutter_animate.dart';
910

@@ -20,8 +21,8 @@ class EverythingView extends StatelessWidget {
2021
childAspectRatio: 0.85,
2122
children: [
2223
/***
23-
A few fun / interesting examples
24-
***/
24+
A few fun / interesting examples
25+
***/
2526
tile(
2627
'fade+tint+blur+scale',
2728
a
@@ -103,9 +104,9 @@ class EverythingView extends StatelessWidget {
103104
),
104105

105106
/***
106-
Catalog of minimal examples for all visual effects.
107-
In alphabetic order of the effect's class name.
108-
***/
107+
Catalog of minimal examples for all visual effects.
108+
In alphabetic order of the effect's class name.
109+
***/
109110

110111
tile('align', a.align()),
111112

@@ -175,6 +176,10 @@ class EverythingView extends StatelessWidget {
175176
tile('scaleY', a.scaleY()),
176177
tile('scaleXY', a.scaleXY()),
177178

179+
tile('sizeX', a.sizeX()),
180+
tile('sizeY', a.sizeY()),
181+
tile('sizeXY', a.size()),
182+
178183
tile('shake', a.shake()),
179184
tile('shakeX', a.shakeX()),
180185
tile('shakeY', a.shakeY()),

lib/src/effects/effects.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export 'then_effect.dart';
2626
export 'tint_effect.dart';
2727
export 'toggle_effect.dart';
2828
export 'visibility_effect.dart';
29+
export 'size_effect.dart';

lib/src/effects/size_effect.dart

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)