mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Make SingleTickerProviderStateMixin more resilient (#7360)
It used to crash when the State never actually used the TickerProvider interface.
This commit is contained in:
parent
e08c3c3bdc
commit
207968ff79
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user