From f974c295b5aa6d8e0369c61db4913d96b07e1db8 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Tue, 7 Jan 2020 11:58:01 -0800 Subject: [PATCH] SliverFadeTransition (#45950) --- .../flutter/lib/src/rendering/proxy_box.dart | 43 +-- .../lib/src/rendering/proxy_sliver.dart | 31 ++- packages/flutter/lib/src/widgets/sliver.dart | 7 +- .../flutter/lib/src/widgets/transitions.dart | 116 +++++++++ .../test/rendering/proxy_box_test.dart | 62 +---- .../test/rendering/proxy_sliver_test.dart | 163 ++++++++++++ .../test/rendering/rendering_tester.dart | 55 ++++ .../test/widgets/transitions_test.dart | 244 +++++++++++++----- 8 files changed, 569 insertions(+), 152 deletions(-) create mode 100644 packages/flutter/test/rendering/proxy_sliver_test.dart diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 0855de1014..3b834c6223 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -823,25 +823,12 @@ class RenderOpacity extends RenderProxyBox { } } -/// Makes its child partially transparent, driven from an [Animation]. +/// Implementation of [RenderAnimatedOpacity] and [RenderSliverAnimatedOpacity]. /// -/// This is a variant of [RenderOpacity] that uses an [Animation] rather -/// than a [double] to control the opacity. -class RenderAnimatedOpacity extends RenderProxyBox { - /// Creates a partially transparent render object. - /// - /// The [opacity] argument must not be null. - RenderAnimatedOpacity({ - @required Animation opacity, - bool alwaysIncludeSemantics = false, - RenderBox child, - }) : assert(opacity != null), - assert(alwaysIncludeSemantics != null), - _alwaysIncludeSemantics = alwaysIncludeSemantics, - super(child) { - this.opacity = opacity; - } - +/// Use this mixin in situations where the proxying behavior +/// of [RenderProxyBox] or [RenderProxySliver] is desired for animating opacity, +/// but would like to use the same methods for both types of render objects. +mixin RenderAnimatedOpacityMixin on RenderObjectWithChildMixin { int _alpha; @override @@ -943,6 +930,26 @@ class RenderAnimatedOpacity extends RenderProxyBox { } } +/// Makes its child partially transparent, driven from an [Animation]. +/// +/// This is a variant of [RenderOpacity] that uses an [Animation] rather +/// than a [double] to control the opacity. +class RenderAnimatedOpacity extends RenderProxyBox with RenderProxyBoxMixin, RenderAnimatedOpacityMixin { + /// Creates a partially transparent render object. + /// + /// The [opacity] argument must not be null. + RenderAnimatedOpacity({ + @required Animation opacity, + bool alwaysIncludeSemantics = false, + RenderBox child, + }) : assert(opacity != null), + assert(alwaysIncludeSemantics != null), + super(child) { + this.opacity = opacity; + this.alwaysIncludeSemantics = alwaysIncludeSemantics; + } +} + /// Signature for a function that creates a [Shader] for a given [Rect]. /// /// Used by [RenderShaderMask] and the [ShaderMask] widget. diff --git a/packages/flutter/lib/src/rendering/proxy_sliver.dart b/packages/flutter/lib/src/rendering/proxy_sliver.dart index 4c1ac249ec..c8ef50813a 100644 --- a/packages/flutter/lib/src/rendering/proxy_sliver.dart +++ b/packages/flutter/lib/src/rendering/proxy_sliver.dart @@ -4,11 +4,13 @@ import 'dart:ui' as ui show Color; +import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; import 'package:vector_math/vector_math_64.dart'; import 'layer.dart'; import 'object.dart'; +import 'proxy_box.dart'; import 'sliver.dart'; /// A base class for sliver render objects that resemble their children. @@ -51,6 +53,12 @@ abstract class RenderProxySliver extends RenderSliver with RenderObjectWithChild geometry = child.geometry; } + @override + void paint(PaintingContext context, Offset offset) { + if (child != null) + context.paintChild(child, offset); + } + @override bool hitTestChildren(SliverHitTestResult result, {double mainAxisPosition, double crossAxisPosition}) { return child != null @@ -152,7 +160,6 @@ class RenderSliverOpacity extends RenderProxySliver { @override void paint(PaintingContext context, Offset offset) { - void _paintWithOpacity(PaintingContext context, Offset offset) => context.paintChild(child, offset); if (child != null && child.geometry.visible) { if (_alpha == 0) { // No need to keep the layer. We'll create a new one if necessary. @@ -169,7 +176,7 @@ class RenderSliverOpacity extends RenderProxySliver { layer = context.pushOpacity( offset, _alpha, - _paintWithOpacity, + super.paint, oldLayer: layer as OpacityLayer, ); } @@ -370,3 +377,23 @@ class RenderSliverOffstage extends RenderProxySliver { ]; } } + +/// Makes its sliver child partially transparent, driven from an [Animation]. +/// +/// This is a variant of [RenderSliverOpacity] that uses an [Animation] +/// rather than a [double] to control the opacity. +class RenderSliverAnimatedOpacity extends RenderProxySliver with RenderAnimatedOpacityMixin{ + /// Creates a partially transparent render object. + /// + /// The [opacity] argument must not be null. + RenderSliverAnimatedOpacity({ + @required Animation opacity, + bool alwaysIncludeSemantics = false, + RenderSliver sliver, + }) : assert(opacity != null), + assert(alwaysIncludeSemantics != null) { + this.opacity = opacity; + this.alwaysIncludeSemantics = alwaysIncludeSemantics; + child = sliver; + } +} diff --git a/packages/flutter/lib/src/widgets/sliver.dart b/packages/flutter/lib/src/widgets/sliver.dart index 92fe47f9a6..0a0a7cde17 100644 --- a/packages/flutter/lib/src/widgets/sliver.dart +++ b/packages/flutter/lib/src/widgets/sliver.dart @@ -1776,10 +1776,9 @@ class SliverOpacity extends SingleChildRenderObjectWidget { @required this.opacity, this.alwaysIncludeSemantics = false, Widget sliver, - }) - : assert(opacity != null && opacity >= 0.0 && opacity <= 1.0), - assert(alwaysIncludeSemantics != null), - super(key: key, child: sliver); + }) : assert(opacity != null && opacity >= 0.0 && opacity <= 1.0), + assert(alwaysIncludeSemantics != null), + super(key: key, child: sliver); /// The fraction to scale the sliver child's alpha value. /// diff --git a/packages/flutter/lib/src/widgets/transitions.dart b/packages/flutter/lib/src/widgets/transitions.dart index eb95c02c91..524a4298fd 100644 --- a/packages/flutter/lib/src/widgets/transitions.dart +++ b/packages/flutter/lib/src/widgets/transitions.dart @@ -520,6 +520,7 @@ class SizeTransition extends AnimatedWidget { /// /// Here's an illustration of the [FadeTransition] widget, with it's [opacity] /// animated by a [CurvedAnimation] set to [Curves.fastOutSlowIn]: +/// /// {@animation 300 378 https://flutter.github.io/assets-for-api-docs/assets/widgets/fade_transition.mp4} /// /// See also: @@ -580,6 +581,121 @@ class FadeTransition extends SingleChildRenderObjectWidget { } } +/// Animates the opacity of a sliver widget. +/// +/// {@tool snippet --template=stateful_widget_scaffold_center_freeform_state} +/// Creates a [CustomScrollView] with a [SliverFixedExtentList] that uses a +/// [SliverFadeTransition] to fade the list in and out. +/// +/// ```dart +/// class _MyStatefulWidgetState extends State with SingleTickerProviderStateMixin { +/// AnimationController controller; +/// Animation animation; +/// +/// initState() { +/// super.initState(); +/// controller = AnimationController( +/// duration: const Duration(milliseconds: 1000), vsync: this); +/// animation = CurvedAnimation(parent: controller, curve: Curves.easeIn); +/// +/// animation.addStatusListener((status) { +/// if (status == AnimationStatus.completed) { +/// controller.reverse(); +/// } else if (status == AnimationStatus.dismissed) { +/// controller.forward(); +/// } +/// }); +/// controller.forward(); +/// } +/// +/// Widget build(BuildContext context) { +/// return CustomScrollView( +/// slivers: [ +/// SliverFadeTransition( +/// opacity: animation, +/// sliver: SliverFixedExtentList( +/// itemExtent: 100.0, +/// delegate: SliverChildBuilderDelegate( +/// (BuildContext context, int index) { +/// return Container( +/// color: index % 2 == 0 +/// ? Colors.indigo[200] +/// : Colors.orange[200], +/// ); +/// }, +/// childCount: 5, +/// ), +/// ), +/// ) +/// ] +/// ); +/// } +/// } +/// ``` +/// {@end-tool} +/// +/// Here's an illustration of the [FadeTransition] widget, the [RenderBox] +/// equivalent widget, with it's [opacity] animated by a [CurvedAnimation] set +/// to [Curves.fastOutSlowIn]: +/// +/// {@animation 300 378 https://flutter.github.io/assets-for-api-docs/assets/widgets/fade_transition.mp4} +/// +/// See also: +/// +/// * [SliverOpacity], which does not animate changes in opacity. +class SliverFadeTransition extends SingleChildRenderObjectWidget { + /// Creates an opacity transition. + /// + /// The [opacity] argument must not be null. + const SliverFadeTransition({ + Key key, + @required this.opacity, + this.alwaysIncludeSemantics = false, + Widget sliver, + }) : assert(opacity != null), + super(key: key, child: sliver); + + /// The animation that controls the opacity of the sliver child. + /// + /// If the current value of the opacity animation is v, the child will be + /// painted with an opacity of v. For example, if v is 0.5, the child will be + /// blended 50% with its background. Similarly, if v is 0.0, the child will be + /// completely transparent. + final Animation opacity; + + /// Whether the semantic information of the sliver child is always included. + /// + /// Defaults to false. + /// + /// When true, regardless of the opacity settings the sliver child's semantic + /// information is exposed as if the widget were fully visible. This is + /// useful in cases where labels may be hidden during animations that + /// would otherwise contribute relevant semantics. + final bool alwaysIncludeSemantics; + + @override + RenderSliverAnimatedOpacity createRenderObject(BuildContext context) { + return RenderSliverAnimatedOpacity( + opacity: opacity, + alwaysIncludeSemantics: alwaysIncludeSemantics, + ); + } + + @override + void updateRenderObject(BuildContext context, RenderSliverAnimatedOpacity renderObject) { + renderObject + ..opacity = opacity + ..alwaysIncludeSemantics = alwaysIncludeSemantics; + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty>('opacity', opacity)); + properties.add(FlagProperty('alwaysIncludeSemantics', value: alwaysIncludeSemantics, ifTrue: 'alwaysIncludeSemantics')); + } +} + /// An interpolation between two relative rects. /// /// This class specializes the interpolation of [Tween] to diff --git a/packages/flutter/test/rendering/proxy_box_test.dart b/packages/flutter/test/rendering/proxy_box_test.dart index e7aae9d5e2..e028865280 100644 --- a/packages/flutter/test/rendering/proxy_box_test.dart +++ b/packages/flutter/test/rendering/proxy_box_test.dart @@ -9,7 +9,6 @@ import 'package:flutter/animation.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/rendering.dart'; -import 'package:flutter/src/scheduler/ticker.dart'; import '../flutter_test_alternative.dart'; import 'rendering_tester.dart'; @@ -277,7 +276,7 @@ void main() { test('RenderAnimatedOpacity does not composite if it is transparent', () async { final Animation opacityAnimation = AnimationController( - vsync: _FakeTickerProvider(), + vsync: FakeTickerProvider(), )..value = 0.0; final RenderAnimatedOpacity renderAnimatedOpacity = RenderAnimatedOpacity( @@ -292,7 +291,7 @@ void main() { test('RenderAnimatedOpacity does not composite if it is opaque', () { final Animation opacityAnimation = AnimationController( - vsync: _FakeTickerProvider(), + vsync: FakeTickerProvider(), )..value = 1.0; final RenderAnimatedOpacity renderAnimatedOpacity = RenderAnimatedOpacity( @@ -307,7 +306,7 @@ void main() { test('RenderAnimatedOpacity reuses its layer', () { final Animation opacityAnimation = AnimationController( - vsync: _FakeTickerProvider(), + vsync: FakeTickerProvider(), )..value = 0.5; // must not be 0 or 1.0. Otherwise, it won't create a layer _testLayerReuse(RenderAnimatedOpacity( @@ -483,61 +482,6 @@ class _TestRRectClipper extends CustomClipper { bool shouldReclip(_TestRRectClipper oldClipper) => true; } -class _FakeTickerProvider implements TickerProvider { - @override - Ticker createTicker(TickerCallback onTick, [ bool disableAnimations = false ]) { - return _FakeTicker(); - } -} - -class _FakeTicker implements Ticker { - @override - bool muted; - - @override - void absorbTicker(Ticker originalTicker) { } - - @override - String get debugLabel => null; - - @override - bool get isActive => null; - - @override - bool get isTicking => null; - - @override - bool get scheduled => null; - - @override - bool get shouldScheduleTick => null; - - @override - void dispose() { } - - @override - void scheduleTick({ bool rescheduling = false }) { } - - @override - TickerFuture start() { - return null; - } - - @override - void stop({ bool canceled = false }) { } - - @override - void unscheduleTick() { } - - @override - String toString({ bool debugIncludeStack = false }) => super.toString(); - - @override - DiagnosticsNode describeForError(String name) { - return DiagnosticsProperty(name, this, style: DiagnosticsTreeStyle.errorProperty); - } -} - // Forces two frames and checks that: // - a layer is created on the first frame // - the layer is reused on the second frame diff --git a/packages/flutter/test/rendering/proxy_sliver_test.dart b/packages/flutter/test/rendering/proxy_sliver_test.dart new file mode 100644 index 0000000000..6285bd22e3 --- /dev/null +++ b/packages/flutter/test/rendering/proxy_sliver_test.dart @@ -0,0 +1,163 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/animation.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; +import '../flutter_test_alternative.dart'; + +import 'rendering_tester.dart'; + +void main() { + test('RenderSliverOpacity does not composite if it is transparent', () { + final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity( + opacity: 0.0, + sliver: RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter + ) + ); + + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + cacheExtent: 250.0, + children: [renderSliverOpacity], + ); + + layout(root, phase: EnginePhase.composite); + expect(renderSliverOpacity.needsCompositing, false); + }); + + test('RenderSliverOpacity does not composite if it is opaque', () { + final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity( + opacity: 1.0, + sliver: RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter + ) + ); + + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + cacheExtent: 250.0, + children: [renderSliverOpacity], + ); + + layout(root, phase: EnginePhase.composite); + expect(renderSliverOpacity.needsCompositing, false); + }); + test('RenderSliverOpacity reuses its layer', () { + final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity( + opacity: 0.5, + sliver: RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter + ) + ); + + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + cacheExtent: 250.0, + children: [renderSliverOpacity] + ); + + expect(renderSliverOpacity.debugLayer, null); + layout(root, phase: EnginePhase.paint, constraints: BoxConstraints.tight(const Size(10, 10))); + final ContainerLayer layer = renderSliverOpacity.debugLayer; + expect(layer, isNotNull); + + // Mark for repaint otherwise pumpFrame is a noop. + renderSliverOpacity.markNeedsPaint(); + expect(renderSliverOpacity.debugNeedsPaint, true); + pumpFrame(phase: EnginePhase.paint); + expect(renderSliverOpacity.debugNeedsPaint, false); + expect(renderSliverOpacity.debugLayer, same(layer)); + }); + + test('RenderSliverAnimatedOpacity does not composite if it is transparent', () async { + final Animation opacityAnimation = AnimationController( + vsync: FakeTickerProvider(), + )..value = 0.0; + + final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity( + alwaysIncludeSemantics: false, + opacity: opacityAnimation, + sliver: RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter + ) + ); + + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + cacheExtent: 250.0, + children: [renderSliverAnimatedOpacity], + ); + + layout(root, phase: EnginePhase.composite); + expect(renderSliverAnimatedOpacity.needsCompositing, false); + }); + + test('RenderSliverAnimatedOpacity does not composite if it is opaque', () { + final Animation opacityAnimation = AnimationController( + vsync: FakeTickerProvider(), + )..value = 1.0; + + final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity( + alwaysIncludeSemantics: false, + opacity: opacityAnimation, + sliver: RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter + ) + ); + + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + cacheExtent: 250.0, + children: [renderSliverAnimatedOpacity], + ); + + layout(root, phase: EnginePhase.composite); + expect(renderSliverAnimatedOpacity.needsCompositing, false); + }); + + test('RenderSliverAnimatedOpacity reuses its layer', () { + final Animation opacityAnimation = AnimationController( + vsync: FakeTickerProvider(), + )..value = 0.5; // must not be 0 or 1.0. Otherwise, it won't create a layer + + final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity( + opacity: opacityAnimation, + sliver: RenderSliverToBoxAdapter( + child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter + ) + ); + + final RenderViewport root = RenderViewport( + axisDirection: AxisDirection.down, + crossAxisDirection: AxisDirection.right, + offset: ViewportOffset.zero(), + cacheExtent: 250.0, + children: [renderSliverAnimatedOpacity] + ); + + expect(renderSliverAnimatedOpacity.debugLayer, null); + layout(root, phase: EnginePhase.paint, constraints: BoxConstraints.tight(const Size(10, 10))); + final ContainerLayer layer = renderSliverAnimatedOpacity.debugLayer; + expect(layer, isNotNull); + + // Mark for repaint otherwise pumpFrame is a noop. + renderSliverAnimatedOpacity.markNeedsPaint(); + expect(renderSliverAnimatedOpacity.debugNeedsPaint, true); + pumpFrame(phase: EnginePhase.paint); + expect(renderSliverAnimatedOpacity.debugNeedsPaint, false); + expect(renderSliverAnimatedOpacity.debugLayer, same(layer)); + }); +} diff --git a/packages/flutter/test/rendering/rendering_tester.dart b/packages/flutter/test/rendering/rendering_tester.dart index 8c22205571..dd94dfbdd2 100644 --- a/packages/flutter/test/rendering/rendering_tester.dart +++ b/packages/flutter/test/rendering/rendering_tester.dart @@ -229,3 +229,58 @@ class RenderSizedBox extends RenderBox { @override bool hitTestSelf(Offset position) => true; } + +class FakeTickerProvider implements TickerProvider { + @override + Ticker createTicker(TickerCallback onTick, [ bool disableAnimations = false ]) { + return FakeTicker(); + } +} + +class FakeTicker implements Ticker { + @override + bool muted; + + @override + void absorbTicker(Ticker originalTicker) { } + + @override + String get debugLabel => null; + + @override + bool get isActive => null; + + @override + bool get isTicking => null; + + @override + bool get scheduled => null; + + @override + bool get shouldScheduleTick => null; + + @override + void dispose() { } + + @override + void scheduleTick({ bool rescheduling = false }) { } + + @override + TickerFuture start() { + return null; + } + + @override + void stop({ bool canceled = false }) { } + + @override + void unscheduleTick() { } + + @override + String toString({ bool debugIncludeStack = false }) => super.toString(); + + @override + DiagnosticsNode describeForError(String name) { + return DiagnosticsProperty(name, this, style: DiagnosticsTreeStyle.errorProperty); + } +} diff --git a/packages/flutter/test/widgets/transitions_test.dart b/packages/flutter/test/widgets/transitions_test.dart index 94ae32047a..7ffba0dfa9 100644 --- a/packages/flutter/test/widgets/transitions_test.dart +++ b/packages/flutter/test/widgets/transitions_test.dart @@ -4,6 +4,7 @@ import 'dart:math' as math; +import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -53,72 +54,73 @@ void main() { controller = AnimationController(vsync: const TestVSync()); }); - testWidgets( - 'decoration test', - (WidgetTester tester) async { - final DecoratedBoxTransition transitionUnderTest = - DecoratedBoxTransition( - decoration: decorationTween.animate(controller), - child: const Text('Doesn\'t matter', textDirection: TextDirection.ltr), - ); + testWidgets('decoration test', (WidgetTester tester) async { + final DecoratedBoxTransition transitionUnderTest = + DecoratedBoxTransition( + decoration: decorationTween.animate(controller), + child: const Text( + 'Doesn\'t matter', + textDirection: TextDirection.ltr, + ), + ); - await tester.pumpWidget(transitionUnderTest); - RenderDecoratedBox actualBox = - tester.renderObject(find.byType(DecoratedBox)); - BoxDecoration actualDecoration = actualBox.decoration as BoxDecoration; + await tester.pumpWidget(transitionUnderTest); + RenderDecoratedBox actualBox = tester.renderObject(find.byType(DecoratedBox)); + BoxDecoration actualDecoration = actualBox.decoration as BoxDecoration; - expect(actualDecoration.color, const Color(0xFFFFFFFF)); - expect(actualDecoration.boxShadow[0].blurRadius, 10.0); - expect(actualDecoration.boxShadow[0].spreadRadius, 4.0); - expect(actualDecoration.boxShadow[0].color, const Color(0x66000000)); + expect(actualDecoration.color, const Color(0xFFFFFFFF)); + expect(actualDecoration.boxShadow[0].blurRadius, 10.0); + expect(actualDecoration.boxShadow[0].spreadRadius, 4.0); + expect(actualDecoration.boxShadow[0].color, const Color(0x66000000)); - controller.value = 0.5; + controller.value = 0.5; - await tester.pump(); - actualBox = tester.renderObject(find.byType(DecoratedBox)); - actualDecoration = actualBox.decoration as BoxDecoration; + await tester.pump(); + actualBox = tester.renderObject(find.byType(DecoratedBox)); + actualDecoration = actualBox.decoration as BoxDecoration; - expect(actualDecoration.color, const Color(0xFF7F7F7F)); - expect(actualDecoration.border, isInstanceOf()); - final Border border = actualDecoration.border as Border; - expect(border.left.width, 2.5); - expect(border.left.style, BorderStyle.solid); - expect(border.left.color, const Color(0xFF101010)); - expect(actualDecoration.borderRadius, BorderRadius.circular(5.0)); - expect(actualDecoration.shape, BoxShape.rectangle); - expect(actualDecoration.boxShadow[0].blurRadius, 5.0); - expect(actualDecoration.boxShadow[0].spreadRadius, 2.0); - // Scaling a shadow doesn't change the color. - expect(actualDecoration.boxShadow[0].color, const Color(0x66000000)); + expect(actualDecoration.color, const Color(0xFF7F7F7F)); + expect(actualDecoration.border, isInstanceOf()); + final Border border = actualDecoration.border as Border; + expect(border.left.width, 2.5); + expect(border.left.style, BorderStyle.solid); + expect(border.left.color, const Color(0xFF101010)); + expect(actualDecoration.borderRadius, BorderRadius.circular(5.0)); + expect(actualDecoration.shape, BoxShape.rectangle); + expect(actualDecoration.boxShadow[0].blurRadius, 5.0); + expect(actualDecoration.boxShadow[0].spreadRadius, 2.0); + // Scaling a shadow doesn't change the color. + expect(actualDecoration.boxShadow[0].color, const Color(0x66000000)); - controller.value = 1.0; + controller.value = 1.0; - await tester.pump(); - actualBox = tester.renderObject(find.byType(DecoratedBox)); - actualDecoration = actualBox.decoration as BoxDecoration; + await tester.pump(); + actualBox = tester.renderObject(find.byType(DecoratedBox)); + actualDecoration = actualBox.decoration as BoxDecoration; - expect(actualDecoration.color, const Color(0xFF000000)); - expect(actualDecoration.boxShadow, null); - }, - ); + expect(actualDecoration.color, const Color(0xFF000000)); + expect(actualDecoration.boxShadow, null); + }); testWidgets('animations work with curves test', (WidgetTester tester) async { final Animation curvedDecorationAnimation = - decorationTween.animate(CurvedAnimation( - parent: controller, - curve: Curves.easeOut, - )); + decorationTween.animate(CurvedAnimation( + parent: controller, + curve: Curves.easeOut, + )); - final DecoratedBoxTransition transitionUnderTest = - DecoratedBoxTransition( - decoration: curvedDecorationAnimation, - position: DecorationPosition.foreground, - child: const Text('Doesn\'t matter', textDirection: TextDirection.ltr), - ); + final DecoratedBoxTransition transitionUnderTest = DecoratedBoxTransition( + decoration: curvedDecorationAnimation, + position: DecorationPosition.foreground, + child: const Text( + 'Doesn\'t matter', + textDirection: TextDirection.ltr, + ), + ); await tester.pumpWidget(transitionUnderTest); - RenderDecoratedBox actualBox = - tester.renderObject(find.byType(DecoratedBox)); + + RenderDecoratedBox actualBox = tester.renderObject(find.byType(DecoratedBox)); BoxDecoration actualDecoration = actualBox.decoration as BoxDecoration; expect(actualDecoration.color, const Color(0xFFFFFFFF)); @@ -199,13 +201,13 @@ void main() { final Animation animation = Tween(begin: -1.0, end: 1.0).animate(controller); final Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: SizeTransition( - axis: Axis.vertical, - sizeFactor: animation, - child: const Text('Ready'), - ), - ); + textDirection: TextDirection.ltr, + child: SizeTransition( + axis: Axis.vertical, + sizeFactor: animation, + child: const Text('Ready'), + ), + ); await tester.pumpWidget(widget); @@ -230,13 +232,13 @@ void main() { final Animation animation = Tween(begin: -1.0, end: 1.0).animate(controller); final Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: SizeTransition( - axis: Axis.horizontal, - sizeFactor: animation, - child: const Text('Ready'), - ), - ); + textDirection: TextDirection.ltr, + child: SizeTransition( + axis: Axis.horizontal, + sizeFactor: animation, + child: const Text('Ready'), + ), + ); await tester.pumpWidget(widget); @@ -261,7 +263,10 @@ void main() { final Widget widget = RotationTransition( alignment: Alignment.topRight, turns: controller, - child: const Text('Rotation', textDirection: TextDirection.ltr), + child: const Text( + 'Rotation', + textDirection: TextDirection.ltr, + ), ); await tester.pumpWidget(widget); @@ -291,8 +296,7 @@ void main() { ); await tester.pumpWidget(widget); - RotationTransition actualRotatedBox = - tester.widget(find.byType(RotationTransition)); + RotationTransition actualRotatedBox = tester.widget(find.byType(RotationTransition)); Alignment actualAlignment = actualRotatedBox.alignment; expect(actualAlignment, const Alignment(1.0, -1.0)); @@ -302,4 +306,106 @@ void main() { actualAlignment = actualRotatedBox.alignment; expect(actualAlignment, const Alignment(1.0, -1.0)); }); + + group('FadeTransition', () { + double _getOpacity(WidgetTester tester, String textValue) { + final FadeTransition opacityWidget = tester.widget( + find.ancestor( + of: find.text(textValue), + matching: find.byType(FadeTransition), + ).first, + ); + return opacityWidget.opacity.value; + } + testWidgets('animates', (WidgetTester tester) async { + final AnimationController controller = AnimationController(vsync: const TestVSync()); + final Animation animation = Tween(begin: 0.0, end: 1.0).animate(controller); + final Widget widget = Directionality( + textDirection: TextDirection.ltr, + child: FadeTransition( + opacity: animation, + child: const Text('Fade In'), + ), + ); + + await tester.pumpWidget(widget); + + expect(_getOpacity(tester, 'Fade In'), 0.0); + + controller.value = 0.25; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 0.25); + + controller.value = 0.5; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 0.5); + + controller.value = 0.75; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 0.75); + + controller.value = 1.0; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 1.0); + }); + }); + + group('SliverFadeTransition', () { + double _getOpacity(WidgetTester tester, String textValue) { + final SliverFadeTransition opacityWidget = tester.widget( + find.ancestor( + of: find.text(textValue), + matching: find.byType(SliverFadeTransition), + ).first, + ); + return opacityWidget.opacity.value; + } + testWidgets('animates', (WidgetTester tester) async { + final AnimationController controller = AnimationController(vsync: const TestVSync()); + final Animation animation = Tween(begin: 0.0, end: 1.0).animate(controller); + final Widget widget = Localizations( + locale: const Locale('en', 'us'), + delegates: const >[ + DefaultWidgetsLocalizations.delegate, + DefaultMaterialLocalizations.delegate, + ], + child: Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: const MediaQueryData(), + child: CustomScrollView( + slivers: [ + SliverFadeTransition( + opacity: animation, + sliver: const SliverToBoxAdapter( + child: Text('Fade In'), + ), + ), + ] + ) + ) + ) + ); + + await tester.pumpWidget(widget); + + expect(_getOpacity(tester, 'Fade In'), 0.0); + + controller.value = 0.25; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 0.25); + + controller.value = 0.5; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 0.5); + + controller.value = 0.75; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 0.75); + + controller.value = 1.0; + await tester.pump(); + expect(_getOpacity(tester, 'Fade In'), 1.0); + }); + }); }