From c08bac8335654efdb5350932c76f7277f7c22c63 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 7 Feb 2017 14:49:15 -0800 Subject: [PATCH] Form needs to clean up its scopedWillPopCallback (#7936) --- packages/flutter/lib/src/widgets/form.dart | 27 +++++--- packages/flutter/lib/src/widgets/routes.dart | 19 +++++- .../flutter/test/material/will_pop_test.dart | 68 +++++++++++++++++++ 3 files changed, 103 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/widgets/form.dart b/packages/flutter/lib/src/widgets/form.dart index b7f92c845c..0e889a1ab2 100644 --- a/packages/flutter/lib/src/widgets/form.dart +++ b/packages/flutter/lib/src/widgets/form.dart @@ -64,29 +64,36 @@ class Form extends StatefulWidget { class FormState extends State
{ int _generation = 0; Set> _fields = new Set>(); + ModalRoute _route; @override void dependenciesChanged() { super.dependenciesChanged(); - final ModalRoute route = ModalRoute.of(context); - if (route != null && config.onWillPop != null) { - // Avoid adding our callback twice by removing it first. - route.removeScopedWillPopCallback(config.onWillPop); - route.addScopedWillPopCallback(config.onWillPop); - } + if (_route != null && config.onWillPop != null) + _route.removeScopedWillPopCallback(config.onWillPop); + _route = ModalRoute.of(context); + if (_route != null && config.onWillPop != null) + _route.addScopedWillPopCallback(config.onWillPop); } @override void didUpdateConfig(Form oldConfig) { - final ModalRoute route = ModalRoute.of(context); - if (config.onWillPop != oldConfig.onWillPop && route != null) { + assert(_route == ModalRoute.of(context)); + if (config.onWillPop != oldConfig.onWillPop && _route != null) { if (oldConfig.onWillPop != null) - route.removeScopedWillPopCallback(oldConfig.onWillPop); + _route.removeScopedWillPopCallback(oldConfig.onWillPop); if (config.onWillPop != null) - route.addScopedWillPopCallback(config.onWillPop); + _route.addScopedWillPopCallback(config.onWillPop); } } + @override + void dispose() { + if (_route != null && config.onWillPop != null) + _route.removeScopedWillPopCallback(config.onWillPop); + super.dispose(); + } + // Called when a form field has changed. This will cause all form fields // to rebuild, useful if form fields have interdependencies. void _fieldDidChange() { diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index d79272ae98..a236194bfa 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -665,10 +665,14 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute _route; + /// /// @override /// void dependenciesChanged() { /// super.dependenciesChanged(); - /// ModalRoute.of(context).addScopedWillPopCallback(askTheUserIfTheyAreSure); + /// _route?.removeScopedWillPopCallback(askTheUserIfTheyAreSure); + /// _route = ModalRoute.of(context); + /// _route?.addScopedWillPopCallback(askTheUserIfTheyAreSure); /// } /// ``` /// @@ -679,6 +683,19 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends MaterialPageRoute { + TestPageRoute({ WidgetBuilder builder }) + : super(builder: builder, maintainState: true, settings: const RouteSettings()); + + bool get hasCallback => super.hasScopedWillPopCallback; +} + + void main() { testWidgets('ModalRoute scopedWillPopupCallback can inhibit back button', (WidgetTester tester) async { await tester.pumpWidget( @@ -243,4 +252,63 @@ void main() { expect(find.text('Sample Form'), findsNothing); }); + testWidgets('Route.scopedWillPop callbacks do not accumulate', (WidgetTester tester) async { + StateSetter contentsSetState; // call this to rebuild the route's SampleForm contents + bool contentsEmpty = false; // when true, don't include the SampleForm in the route + + TestPageRoute route = new TestPageRoute( + builder: (BuildContext context) { + return new StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + contentsSetState = setState; + return contentsEmpty ? new Container() : new SampleForm(key: new UniqueKey()); + } + ); + }, + ); + + Widget buildFrame() { + return new MaterialApp( + home: new Scaffold( + appBar: new AppBar(title: new Text('Home')), + body: new Builder( + builder: (BuildContext context) { + return new Center( + child: new FlatButton( + child: new Text('X'), + onPressed: () { + Navigator.of(context).push(route); + }, + ), + ); + }, + ), + ), + ); + } + + await tester.pumpWidget(buildFrame()); + + await tester.tap(find.text('X')); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + + expect(find.text('Sample Form'), findsOneWidget); + expect(route.hasCallback, isTrue); + + // Rebuild the route's SampleForm child an additional 3x for good measure. + contentsSetState(() { }); + await tester.pump(); + contentsSetState(() { }); + await tester.pump(); + contentsSetState(() { }); + await tester.pump(); + + // Now build the route's contents without the sample form. + contentsEmpty = true; + contentsSetState(() { }); + await tester.pump(); + + expect(route.hasCallback, isFalse); + }); }