Skip to content

Instantly share code, notes, and snippets.

@RobertApikyan
Created January 6, 2024 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertApikyan/6b62e30a284f679d79a90fd11b0da07d to your computer and use it in GitHub Desktop.
Save RobertApikyan/6b62e30a284f679d79a90fd11b0da07d to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
typedef ValueTransitionBuilder = Widget Function(BuildContext context, Widget? child,double value);
class AnimatedValue extends ImplicitlyAnimatedWidget {
/// Creates a widget that animates its rotation implicitly.
///
/// The [value] argument must not be null.
/// The [curve] and [duration] arguments must not be null.
const AnimatedValue({
super.key,
required this.builder,
required this.value,
this.alignment = Alignment.center,
this.filterQuality,
super.curve,
required super.duration,
super.onEnd,
});
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.ProxyWidget.child}
final ValueTransitionBuilder builder;
/// The animation that controls the rotation of the child.
///
/// If the current value of the turns animation is v, the child will be
/// rotated v * 2 * pi radians before being painted.
final double value;
/// The alignment of the origin of the coordinate system in which the rotation
/// takes place, relative to the size of the box.
///
/// For example, to set the origin of the rotation to bottom middle, you can use
/// an alignment of (0.0, 1.0).
final Alignment alignment;
/// The filter quality with which to apply the transform as a bitmap operation.
///
/// {@macro flutter.widgets.Transform.optional.FilterQuality}
final FilterQuality? filterQuality;
@override
ImplicitlyAnimatedWidgetState<AnimatedValue> createState() =>
_AnimatedValueState();
}
class _AnimatedValueState extends ImplicitlyAnimatedWidgetState<AnimatedValue> {
Tween<double>? _values;
late Animation<double> _valueAnimation;
@override
void forEachTween(TweenVisitor<dynamic> visitor) {
_values = visitor(_values, widget.value,
(dynamic value) => Tween<double>(begin: value as double))
as Tween<double>?;
}
@override
void didUpdateTweens() {
_valueAnimation = animation.drive(_values!);
}
@override
Widget build(BuildContext context) =>
AnimatedBuilder(animation: _valueAnimation, builder: (context, child) => widget.builder(context,child,_valueAnimation.value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment