From 295d3749bd175340ee0d292c0d9dd8e85a65788e Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 8 Apr 2016 13:05:47 -0700 Subject: [PATCH] FlexibleSpaceBar fails to animate (#3222) We need to listen to the Scaffold's animation so that we rebuild every time it ticks so that we can drive our animations. --- .../lib/src/material/flexible_space_bar.dart | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/flexible_space_bar.dart b/packages/flutter/lib/src/material/flexible_space_bar.dart index cf0298beef..a8a55ac7eb 100644 --- a/packages/flutter/lib/src/material/flexible_space_bar.dart +++ b/packages/flutter/lib/src/material/flexible_space_bar.dart @@ -22,12 +22,27 @@ class FlexibleSpaceBar extends StatefulWidget { } class _FlexibleSpaceBarState extends State { + Animation _scaffoldAnimation; + + void _handleTick() { + setState(() { + // The animation's state is our build state, and it changed already. + }); + } + + @override + void deactivate() { + _scaffoldAnimation?.removeListener(_handleTick); + _scaffoldAnimation = null; + } + @override Widget build(BuildContext context) { assert(debugCheckHasScaffold(context)); final double statusBarHeight = MediaQuery.of(context).padding.top; - final Animation animation = Scaffold.of(context).appBarAnimation; - final double appBarHeight = Scaffold.of(context).appBarHeight + statusBarHeight; + final ScaffoldState scaffold = Scaffold.of(context); + _scaffoldAnimation ??= scaffold.appBarAnimation..addListener(_handleTick); + final double appBarHeight = scaffold.appBarHeight + statusBarHeight; final double toolBarHeight = kToolBarHeight + statusBarHeight; final List children = []; @@ -36,10 +51,10 @@ class _FlexibleSpaceBarState extends State { final double fadeStart = (appBarHeight - toolBarHeight * 2.0) / appBarHeight; final double fadeEnd = (appBarHeight - toolBarHeight) / appBarHeight; final CurvedAnimation opacityCurve = new CurvedAnimation( - parent: animation, + parent: _scaffoldAnimation, curve: new Interval(math.max(0.0, fadeStart), math.min(fadeEnd, 1.0)) ); - final double parallax = new Tween(begin: 0.0, end: appBarHeight / 4.0).evaluate(animation); + final double parallax = new Tween(begin: 0.0, end: appBarHeight / 4.0).evaluate(_scaffoldAnimation); children.add(new Positioned( top: -parallax, left: 0.0, @@ -59,7 +74,7 @@ class _FlexibleSpaceBarState extends State { final double fadeStart = (appBarHeight - toolBarHeight) / appBarHeight; final double fadeEnd = (appBarHeight - toolBarHeight / 2.0) / appBarHeight; final CurvedAnimation opacityCurve = new CurvedAnimation( - parent: animation, + parent: _scaffoldAnimation, curve: new Interval(fadeStart, fadeEnd) ); TextStyle titleStyle = Theme.of(context).primaryTextTheme.title; @@ -70,7 +85,7 @@ class _FlexibleSpaceBarState extends State { final double yAlignEnd = (statusBarHeight + kToolBarHeight / 2.0) / toolBarHeight; final double scaleAndAlignEnd = (appBarHeight - toolBarHeight) / appBarHeight; final CurvedAnimation scaleAndAlignCurve = new CurvedAnimation( - parent: animation, + parent: _scaffoldAnimation, curve: new Interval(0.0, scaleAndAlignEnd) ); children.add(new Padding(