Created
January 6, 2024 16:54
-
-
Save RobertApikyan/6b62e30a284f679d79a90fd11b0da07d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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