Skip to content

Commit

Permalink
feat: add extensions for creating AnimationSpec and Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Sep 2, 2022
1 parent 49126dc commit 97ec321
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
Expand Up @@ -33,7 +33,7 @@ class _MyHomePageState extends State<MyHomePage> {
return Scaffold(
body: Center(
child: Animated(
animation: AnimationSpec.ease(const Duration(seconds: 1)),
animation: AnimationSpec.ease(1.s),
value: _expanded,
child: ASizedBox.fromSize(
size: _expanded ? const Size.square(400) : const Size.square(200),
Expand Down
11 changes: 4 additions & 7 deletions packages/fleet/example/lib/simple_imperative_animation.dart
Expand Up @@ -41,13 +41,10 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _expand() {
setStateWithAnimation(
AnimationSpec.curve(Curves.easeInOutExpo, const Duration(seconds: 1)),
() {
_color = _expandedColor;
_size = (context.findRenderObject()! as RenderBox).size;
},
);
setStateWithAnimation(AnimationSpec.curve(Curves.easeInOutExpo, 1.s), () {
_color = _expandedColor;
_size = (context.findRenderObject()! as RenderBox).size;
});
}

@override
Expand Down
7 changes: 2 additions & 5 deletions packages/fleet/example/lib/stack.dart
Expand Up @@ -52,11 +52,8 @@ class StackElement extends StatelessWidget {
final bool left;

AnimationSpec _buildAnimation() {
return AnimationSpec.curve(
Curves.easeInOutCubic,
const Duration(milliseconds: 500) -
const Duration(milliseconds: 25) * index,
).delay(const Duration(milliseconds: 50) * index);
return AnimationSpec.curve(Curves.easeInOutCubic, 500.ms - 25.ms * index)
.delay(50.ms * index);
}

Color _buildColor() =>
Expand Down
3 changes: 2 additions & 1 deletion packages/fleet/lib/fleet.dart
Expand Up @@ -27,4 +27,5 @@ export 'src/animatable_widgets.dart'
show AAlign, AColoredBox, AContainer, ASizedBox;
export 'src/animate.dart'
show Animated, FleetBinding, withAnimation, SetStateWithAnimationExtension;
export 'src/animation.dart' show AnimationSpec;
export 'src/animation.dart' show AnimationSpec, AnimationFromCurveExtension;
export 'src/duration.dart' show DurationFromIntExtension;
16 changes: 16 additions & 0 deletions packages/fleet/lib/src/animation.dart
Expand Up @@ -165,6 +165,22 @@ class AnimationSpec with Diagnosticable {
_speed.hashCode;
}

/// Extension for creating [AnimationSpec]s from [Curve]s.
///
/// See also:
///
/// - [AnimationSpec.curve]
extension AnimationFromCurveExtension on Curve {
/// Creates an [AnimationSpec] which uses this [Curve].
///
/// See also:
///
/// - [AnimationSpec.curve]
AnimationSpec animation([Duration duration = AnimationSpec.defaultDuration]) {
return AnimationSpec.curve(this, duration);
}
}

/// The current [AnimationSpec] that will be used to animated visual changes.
AnimationSpec? get currentAnimation => _currentAnimation;
AnimationSpec? _currentAnimation;
Expand Down
20 changes: 20 additions & 0 deletions packages/fleet/lib/src/duration.dart
@@ -0,0 +1,20 @@
/// Extension for creating [Duration]s from [int]s.
extension DurationFromIntExtension on int {
/// A [Duration] that is as many **days** long as this value.
Duration get d => Duration(days: this);

/// A [Duration] that is as many **hours** long as this value.
Duration get h => Duration(hours: this);

/// A [Duration] that is as many **minutes** long as this value.
Duration get m => Duration(minutes: this);

/// A [Duration] that is as many **seconds** long as this value.
Duration get s => Duration(seconds: this);

/// A [Duration] that is as many **milliseconds** long as this value.
Duration get ms => Duration(milliseconds: this);

/// A [Duration] that is as many **microseconds** long as this value.
Duration get us => Duration(microseconds: this);
}

0 comments on commit 97ec321

Please sign in to comment.