Skip to content

Instantly share code, notes, and snippets.

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 talamaska/4e98f2c83185d1ae3c3e12d005f35783 to your computer and use it in GitHub Desktop.
Save talamaska/4e98f2c83185d1ae3c3e12d005f35783 to your computer and use it in GitHub Desktop.
ImplicitlyAnimatedBuilder
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