diff --git a/packages/flutter/lib/src/widgets/form.dart b/packages/flutter/lib/src/widgets/form.dart index 0e889a1ab2..cb0fbe3950 100644 --- a/packages/flutter/lib/src/widgets/form.dart +++ b/packages/flutter/lib/src/widgets/form.dart @@ -6,6 +6,7 @@ import 'package:flutter/foundation.dart'; import 'framework.dart'; import 'routes.dart'; +import 'will_pop_scope.dart'; /// An optional container for grouping together multiple form field widgets /// (e.g. [Input] widgets). @@ -64,35 +65,6 @@ class Form extends StatefulWidget { class FormState extends State
{ int _generation = 0; Set> _fields = new Set>(); - ModalRoute _route; - - @override - void dependenciesChanged() { - super.dependenciesChanged(); - 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) { - assert(_route == ModalRoute.of(context)); - if (config.onWillPop != oldConfig.onWillPop && _route != null) { - if (oldConfig.onWillPop != null) - _route.removeScopedWillPopCallback(oldConfig.onWillPop); - if (config.onWillPop != null) - _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. @@ -114,10 +86,13 @@ class FormState extends State { Widget build(BuildContext context) { if (config.autovalidate) _validate(); - return new _FormScope( - formState: this, - generation: _generation, - child: config.child + return new WillPopScope( + onWillPop: config.onWillPop, + child: new _FormScope( + formState: this, + generation: _generation, + child: config.child, + ), ); } diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 29c3754723..d672b46fff 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -688,10 +688,29 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute _route; @@ -705,32 +724,28 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute new _WillPopScopeState(); +} + +class _WillPopScopeState extends State { + ModalRoute _route; + + @override + void dependenciesChanged() { + super.dependenciesChanged(); + if (config.onWillPop != null) + _route?.removeScopedWillPopCallback(config.onWillPop); + _route = ModalRoute.of(context); + if (config.onWillPop != null) + _route?.addScopedWillPopCallback(config.onWillPop); + } + + @override + void didUpdateConfig(WillPopScope oldConfig) { + assert(_route == ModalRoute.of(context)); + if (config.onWillPop != oldConfig.onWillPop && _route != null) { + if (oldConfig.onWillPop != null) + _route.removeScopedWillPopCallback(oldConfig.onWillPop); + if (config.onWillPop != null) + _route.addScopedWillPopCallback(config.onWillPop); + } + } + + @override + void dispose() { + if (config.onWillPop != null) + _route?.removeScopedWillPopCallback(config.onWillPop); + super.dispose(); + } + + @override + Widget build(BuildContext context) => config.child; +} diff --git a/packages/flutter/lib/widgets.dart b/packages/flutter/lib/widgets.dart index ed5debe306..b799cab6db 100644 --- a/packages/flutter/lib/widgets.dart +++ b/packages/flutter/lib/widgets.dart @@ -66,5 +66,6 @@ export 'src/widgets/title.dart'; export 'src/widgets/transitions.dart'; export 'src/widgets/unique_widget.dart'; export 'src/widgets/viewport.dart'; +export 'src/widgets/will_pop_scope.dart'; export 'package:vector_math/vector_math_64.dart' show Matrix4;