Turn off AnimationControllers when not in use (#5902)

This requires all AnimationController objects to be given a
TickerProvider, a class that can create the Ticker.

It also provides some nice mixins for people who want to have their
State provide a TickerProvider. And a schedulerTickerProvider for those
cases where you just want to see your battery burn.

Also, we now enforce destruction order for elements.
This commit is contained in:
Ian Hickson
2016-09-26 10:57:10 -07:00
committed by GitHub
parent c825237a38
commit 9e673853e5
63 changed files with 1358 additions and 492 deletions

View File

@@ -393,11 +393,14 @@ class _RectangleDemoState extends State<_RectangleDemo> {
typedef Widget _DemoBuilder(_ArcDemo demo);
class _ArcDemo {
_ArcDemo(String _title, this.builder) : title = _title, key = new GlobalKey(debugLabel: _title);
_ArcDemo(String _title, this.builder, TickerProvider vsync)
: title = _title,
controller = new AnimationController(duration: const Duration(milliseconds: 500), vsync: vsync),
key = new GlobalKey(debugLabel: _title);
final AnimationController controller = new AnimationController(duration: const Duration(milliseconds: 500));
final String title;
final _DemoBuilder builder;
final AnimationController controller;
final GlobalKey key;
}
@@ -410,23 +413,29 @@ class AnimationDemo extends StatefulWidget {
_AnimationDemoState createState() => new _AnimationDemoState();
}
class _AnimationDemoState extends State<AnimationDemo> {
class _AnimationDemoState extends State<AnimationDemo> with TickerProviderStateMixin {
static final GlobalKey<TabBarSelectionState<_ArcDemo>> _tabsKey = new GlobalKey<TabBarSelectionState<_ArcDemo>>();
static final List<_ArcDemo> _allDemos = <_ArcDemo>[
new _ArcDemo('POINT', (_ArcDemo demo) {
return new _PointDemo(
key: demo.key,
controller: demo.controller
);
}),
new _ArcDemo('RECTANGLE', (_ArcDemo demo) {
return new _RectangleDemo(
key: demo.key,
controller: demo.controller
);
})
];
List<_ArcDemo> _allDemos;
@override
void initState() {
super.initState();
_allDemos = <_ArcDemo>[
new _ArcDemo('POINT', (_ArcDemo demo) {
return new _PointDemo(
key: demo.key,
controller: demo.controller
);
}, this),
new _ArcDemo('RECTANGLE', (_ArcDemo demo) {
return new _RectangleDemo(
key: demo.key,
controller: demo.controller
);
}, this),
];
}
Future<Null> _play() async {
_ArcDemo demo = _tabsKey.currentState.value;

View File

@@ -8,7 +8,8 @@ class NavigationIconView {
NavigationIconView({
Icon icon,
Widget title,
Color color
Color color,
TickerProvider vsync,
}) : _icon = icon,
_color = color,
destinationLabel = new DestinationLabel(
@@ -17,13 +18,14 @@ class NavigationIconView {
backgroundColor: color
),
controller = new AnimationController(
duration: kThemeAnimationDuration
duration: kThemeAnimationDuration,
vsync: vsync,
) {
_animation = new CurvedAnimation(
parent: controller,
curve: new Interval(0.5, 1.0, curve: Curves.fastOutSlowIn)
);
}
_animation = new CurvedAnimation(
parent: controller,
curve: new Interval(0.5, 1.0, curve: Curves.fastOutSlowIn)
);
}
final Icon _icon;
final Color _color;
@@ -61,7 +63,7 @@ class BottomNavigationDemo extends StatefulWidget {
_BottomNavigationDemoState createState() => new _BottomNavigationDemoState();
}
class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
class _BottomNavigationDemoState extends State<BottomNavigationDemo> with TickerProviderStateMixin {
int _currentIndex = 0;
BottomNavigationBarType _type = BottomNavigationBarType.shifting;
List<NavigationIconView> _navigationViews;
@@ -73,22 +75,26 @@ class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
new NavigationIconView(
icon: new Icon(Icons.access_alarm),
title: new Text('Alarm'),
color: Colors.deepPurple[500]
color: Colors.deepPurple[500],
vsync: this,
),
new NavigationIconView(
icon: new Icon(Icons.cloud),
title: new Text('Cloud'),
color: Colors.teal[500]
color: Colors.teal[500],
vsync: this,
),
new NavigationIconView(
icon: new Icon(Icons.favorite),
title: new Text('Favorites'),
color: Colors.indigo[500]
color: Colors.indigo[500],
vsync: this,
),
new NavigationIconView(
icon: new Icon(Icons.event_available),
title: new Text('Event'),
color: Colors.pink[500]
color: Colors.pink[500],
vsync: this,
)
];
@@ -124,14 +130,12 @@ class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
return aValue.compareTo(bValue);
});
return new Stack(
children: transitions
);
return new Stack(children: transitions);
}
@override
Widget build(BuildContext context) {
BottomNavigationBar botNavBar = new BottomNavigationBar(
final BottomNavigationBar botNavBar = new BottomNavigationBar(
labels: _navigationViews.map(
(NavigationIconView navigationView) => navigationView.destinationLabel
).toList(),
@@ -159,18 +163,18 @@ class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
itemBuilder: (BuildContext context) => <PopupMenuItem<BottomNavigationBarType>>[
new PopupMenuItem<BottomNavigationBarType>(
value: BottomNavigationBarType.fixed,
child: new Text('Fixed')
child: new Text('Fixed'),
),
new PopupMenuItem<BottomNavigationBarType>(
value: BottomNavigationBarType.shifting,
child: new Text('Shifting')
child: new Text('Shifting'),
)
]
)
]
),
body: _buildBody(),
bottomNavigationBar: botNavBar
bottomNavigationBar: botNavBar,
);
}
}

View File

@@ -11,7 +11,7 @@ class ProgressIndicatorDemo extends StatefulWidget {
_ProgressIndicatorDemoState createState() => new _ProgressIndicatorDemoState();
}
class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {
class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<double> _animation;
@@ -19,7 +19,8 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {
void initState() {
super.initState();
_controller = new AnimationController(
duration: const Duration(milliseconds: 1500)
duration: const Duration(milliseconds: 1500),
vsync: this,
)..forward();
_animation = new CurvedAnimation(

View File

@@ -95,19 +95,20 @@ class GalleryHome extends StatefulWidget {
GalleryHomeState createState() => new GalleryHomeState();
}
class GalleryHomeState extends State<GalleryHome> {
static final Key _homeKey = new ValueKey<String>("Gallery Home");
class GalleryHomeState extends State<GalleryHome> with SingleTickerProviderStateMixin {
static final Key _homeKey = new ValueKey<String>('Gallery Home');
static final GlobalKey<ScrollableState> _scrollableKey = new GlobalKey<ScrollableState>();
final AnimationController _controller = new AnimationController(
duration: const Duration(milliseconds: 600),
debugLabel: "preview banner"
);
AnimationController _controller;
@override
void initState() {
super.initState();
_controller.forward();
_controller = new AnimationController(
duration: const Duration(milliseconds: 600),
debugLabel: 'preview banner',
vsync: this,
)..forward();
}
@override

View File

@@ -9,6 +9,13 @@ import 'dart:math' as math;
import 'package:flutter/animation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
class NonStopVSync implements TickerProvider {
const NonStopVSync();
@override
Ticker createTicker(TickerCallback onTick) => new Ticker(onTick);
}
void main() {
// We first create a render object that represents a green box.
@@ -41,7 +48,8 @@ void main() {
// To make the square spin, we use an animation that repeats every 1800
// milliseconds.
AnimationController animation = new AnimationController(
duration: const Duration(milliseconds: 1800)
duration: const Duration(milliseconds: 1800),
vsync: const NonStopVSync(),
)..repeat();
// The animation will produce a value between 0.0 and 1.0 each frame, but we
// want to rotate the square using a value between 0.0 and math.PI. To change

View File

@@ -206,7 +206,7 @@ class IsolateExampleWidget extends StatefulWidget {
}
// Main application state.
class IsolateExampleState extends State<StatefulWidget> {
class IsolateExampleState extends State<StatefulWidget> with SingleTickerProviderStateMixin {
String _status = 'Idle';
String _label = 'Start';
@@ -219,7 +219,8 @@ class IsolateExampleState extends State<StatefulWidget> {
void initState() {
super.initState();
_animation = new AnimationController(
duration: const Duration(milliseconds: 3600)
duration: const Duration(milliseconds: 3600),
vsync: this,
)..repeat();
_calculationManager = new CalculationManager(
onProgressListener: _handleProgressUpdate,

View File

@@ -9,7 +9,7 @@ class SpinningSquare extends StatefulWidget {
_SpinningSquareState createState() => new _SpinningSquareState();
}
class _SpinningSquareState extends State<SpinningSquare> {
class _SpinningSquareState extends State<SpinningSquare> with SingleTickerProviderStateMixin {
AnimationController _animation;
@override
@@ -19,7 +19,8 @@ class _SpinningSquareState extends State<SpinningSquare> {
// represents an entire turn of the square whereas in the other examples
// we used 0.0 -> math.PI, which is only half a turn.
_animation = new AnimationController(
duration: const Duration(milliseconds: 3600)
duration: const Duration(milliseconds: 3600),
vsync: this,
)..repeat();
}