Make SingleTickerProviderStateMixin more resilient (#7360)

It used to crash when the State never actually used the TickerProvider
interface.
This commit is contained in:
Ian Hickson 2017-01-05 15:11:28 -08:00 committed by GitHub
parent e08c3c3bdc
commit 207968ff79
2 changed files with 22 additions and 1 deletions

View File

@ -92,6 +92,10 @@ abstract class SingleTickerProviderStateMixin implements State<dynamic>, TickerP
);
});
_ticker = new Ticker(onTick, debugLabel: 'created by $this');
// We assume that this is called from initState, build, or some sort of
// event handler, and that thus TickerMode.of(context) would return true. We
// can't actually check that here because if we're in initState then we're
// not allowed to do inheritance checks yet.
return _ticker;
}
@ -115,7 +119,8 @@ abstract class SingleTickerProviderStateMixin implements State<dynamic>, TickerP
@override
void dependenciesChanged() {
_ticker.muted = !TickerMode.of(context);
if (_ticker != null)
_ticker.muted = !TickerMode.of(context);
super.dependenciesChanged();
}

View File

@ -50,4 +50,20 @@ void main() {
await tester.pump(const Duration(seconds: 5));
expect(tester.binding.transientCallbackCount, 1);
});
testWidgets('SingleTickerProviderStateMixin can handle not being used', (WidgetTester tester) async {
await tester.pumpWidget(new BoringTickerTest());
await tester.pumpWidget(new Container());
// the test is that this doesn't crash, like it used to...
});
}
class BoringTickerTest extends StatefulWidget {
@override
_BoringTickerTestState createState() => new _BoringTickerTestState();
}
class _BoringTickerTestState extends State<BoringTickerTest> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) => new Container();
}