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:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user