Forked from lukepighetti/implicitly_animated_builder.dart
Created
November 12, 2021 22:34
-
-
Save talamaska/4e98f2c83185d1ae3c3e12d005f35783 to your computer and use it in GitHub Desktop.
ImplicitlyAnimatedBuilder
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'; | |
/// An implicitly animated builder that tweens from 0.0 to 1.0 based on `isActive` property | |
class ImplicitlyAnimatedBuilder extends ImplicitlyAnimatedWidget { | |
ImplicitlyAnimatedBuilder({ | |
Key key, | |
@required Curve curve, | |
@required Duration duration, | |
@required this.isActive, | |
@required this.builder, | |
}) : super(key: key, curve: curve, duration: duration); | |
/// When active, tweens to 1.0. When inactive, tweens to 0.0. | |
final bool isActive; | |
final Widget Function(BuildContext, double) builder; | |
@override | |
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() => | |
_ImplicitlyAnimatedBuilderState(); | |
} | |
class _ImplicitlyAnimatedBuilderState | |
extends AnimatedWidgetBaseState<ImplicitlyAnimatedBuilder> { | |
Tween<double> _value; | |
@override | |
Widget build(BuildContext context) { | |
return widget.builder(context, _value.evaluate(animation)); | |
} | |
@override | |
void forEachTween(visitor) { | |
_value = visitor( | |
_value, | |
widget.isActive ? 1.0 : 0.0, | |
(dynamic value) => Tween<double>(begin: value), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment