AnimatedOpacity
This provides an easy way to do implicit opacity transitions.
This commit is contained in:
@@ -55,18 +55,26 @@ export 'package:flutter/rendering.dart' show
|
||||
/// This class paints its child into an intermediate buffer and then blends the
|
||||
/// child back into the scene partially transparent.
|
||||
///
|
||||
/// This class is relatively expensive because it requires painting the child
|
||||
/// into an intermediate buffer.
|
||||
/// For values of opacity other than 0.0 and 1.0, this class is relatively
|
||||
/// expensive because it requires painting the child into an intermediate
|
||||
/// buffer. For the value 0.0, the child is simply not painted at all. For the
|
||||
/// value 1.0, the child is painted immediately without an intermediate buffer.
|
||||
class Opacity extends SingleChildRenderObjectWidget {
|
||||
Opacity({ Key key, this.opacity, Widget child })
|
||||
: super(key: key, child: child) {
|
||||
assert(opacity >= 0.0 && opacity <= 1.0);
|
||||
assert(opacity != null && opacity >= 0.0 && opacity <= 1.0);
|
||||
}
|
||||
|
||||
/// The fraction to scale the child's alpha value.
|
||||
///
|
||||
/// An opacity of 1.0 is fully opaque. An opacity of 0.0 is fully transparent
|
||||
/// (i.e., invisible).
|
||||
///
|
||||
/// The opacity must not be null.
|
||||
///
|
||||
/// Values 1.0 and 0.0 are painted with a fast path. Other values
|
||||
/// require painting the child into an intermediate buffer, which is
|
||||
/// expensive.
|
||||
final double opacity;
|
||||
|
||||
@override
|
||||
|
||||
@@ -382,6 +382,23 @@ class AnimatedPositioned extends ImplicitlyAnimatedWidget {
|
||||
|
||||
@override
|
||||
_AnimatedPositionedState createState() => new _AnimatedPositionedState();
|
||||
|
||||
@override
|
||||
void debugFillDescription(List<String> description) {
|
||||
super.debugFillDescription(description);
|
||||
if (left != null)
|
||||
description.add('left: $left');
|
||||
if (top != null)
|
||||
description.add('top: $top');
|
||||
if (right != null)
|
||||
description.add('right: $right');
|
||||
if (bottom != null)
|
||||
description.add('bottom: $bottom');
|
||||
if (width != null)
|
||||
description.add('width: $width');
|
||||
if (height != null)
|
||||
description.add('height: $height');
|
||||
}
|
||||
}
|
||||
|
||||
class _AnimatedPositionedState extends AnimatedWidgetBaseState<AnimatedPositioned> {
|
||||
@@ -433,3 +450,57 @@ class _AnimatedPositionedState extends AnimatedWidgetBaseState<AnimatedPositione
|
||||
description.add('has height');
|
||||
}
|
||||
}
|
||||
|
||||
/// Animated version of [Opacity] which automatically transitions the child's
|
||||
/// opacity over a given duration whenever the given opacity changes.
|
||||
///
|
||||
/// Animating an opacity is relatively expensive.
|
||||
class AnimatedOpacity extends ImplicitlyAnimatedWidget {
|
||||
AnimatedOpacity({
|
||||
Key key,
|
||||
this.child,
|
||||
this.opacity,
|
||||
Curve curve: Curves.linear,
|
||||
Duration duration
|
||||
}) : super(key: key, curve: curve, duration: duration) {
|
||||
assert(opacity != null && opacity >= 0.0 && opacity <= 1.0);
|
||||
}
|
||||
|
||||
/// The widget below this widget in the tree.
|
||||
final Widget child;
|
||||
|
||||
/// The target opacity.
|
||||
///
|
||||
/// An opacity of 1.0 is fully opaque. An opacity of 0.0 is fully transparent
|
||||
/// (i.e., invisible).
|
||||
///
|
||||
/// The opacity must not be null.
|
||||
final double opacity;
|
||||
|
||||
@override
|
||||
_AnimatedOpacityState createState() => new _AnimatedOpacityState();
|
||||
|
||||
@override
|
||||
void debugFillDescription(List<String> description) {
|
||||
super.debugFillDescription(description);
|
||||
description.add('opacity: $opacity');
|
||||
}
|
||||
}
|
||||
|
||||
class _AnimatedOpacityState extends AnimatedWidgetBaseState<AnimatedOpacity> {
|
||||
Tween<double> _opacity;
|
||||
|
||||
@override
|
||||
void forEachTween(TweenVisitor<dynamic> visitor) {
|
||||
// TODO(ianh): Use constructor tear-offs when it becomes possible
|
||||
_opacity = visitor(_opacity, config.opacity, (dynamic value) => new Tween<double>(begin: value));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Opacity(
|
||||
opacity: _opacity.evaluate(animation),
|
||||
child: config.child
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user