From 77b41ba844609568f2cc1651ca1b03dea574bda5 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Mon, 22 Aug 2022 10:27:32 -0700 Subject: [PATCH] Remove the FocusScopeNode in the navigator (#109702) --- packages/flutter/lib/src/widgets/app.dart | 31 ++++---- .../flutter/lib/src/widgets/navigator.dart | 48 +++++++----- packages/flutter/lib/src/widgets/routes.dart | 10 +-- .../test/material/app_builder_test.dart | 2 +- .../flutter/test/material/debug_test.dart | 10 ++- .../flutter/test/material/dropdown_test.dart | 75 +++++++++---------- 6 files changed, 97 insertions(+), 79 deletions(-) diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 93f8c455f4..2445d32f1b 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -13,6 +13,7 @@ import 'banner.dart'; import 'basic.dart'; import 'binding.dart'; import 'default_text_editing_shortcuts.dart'; +import 'focus_scope.dart'; import 'focus_traversal.dart'; import 'framework.dart'; import 'localizations.dart'; @@ -1625,19 +1626,23 @@ class _WidgetsAppState extends State with WidgetsBindingObserver { ); } else if (_usesNavigator) { assert(_navigator != null); - routing = Navigator( - restorationScopeId: 'nav', - key: _navigator, - initialRoute: _initialRouteName, - onGenerateRoute: _onGenerateRoute, - onGenerateInitialRoutes: widget.onGenerateInitialRoutes == null - ? Navigator.defaultGenerateInitialRoutes - : (NavigatorState navigator, String initialRouteName) { - return widget.onGenerateInitialRoutes!(initialRouteName); - }, - onUnknownRoute: _onUnknownRoute, - observers: widget.navigatorObservers!, - reportsRouteUpdateToEngine: true, + routing = FocusScope( + debugLabel: 'Navigator Scope', + autofocus: true, + child: Navigator( + restorationScopeId: 'nav', + key: _navigator, + initialRoute: _initialRouteName, + onGenerateRoute: _onGenerateRoute, + onGenerateInitialRoutes: widget.onGenerateInitialRoutes == null + ? Navigator.defaultGenerateInitialRoutes + : (NavigatorState navigator, String initialRouteName) { + return widget.onGenerateInitialRoutes!(initialRouteName); + }, + onUnknownRoute: _onUnknownRoute, + observers: widget.navigatorObservers!, + reportsRouteUpdateToEngine: true, + ), ); } else if (_usesRouterWithConfig) { routing = Router.withConfig( diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index d5b22b51c0..cae9ddbad9 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -17,6 +17,7 @@ import 'basic.dart'; import 'binding.dart'; import 'focus_manager.dart'; import 'focus_scope.dart'; +import 'focus_traversal.dart'; import 'framework.dart'; import 'heroes.dart'; import 'overlay.dart'; @@ -217,7 +218,7 @@ abstract class Route { TickerFuture didPush() { return TickerFuture.complete()..then((void _) { if (navigator?.widget.requestFocus ?? false) { - navigator!.focusScopeNode.requestFocus(); + navigator!.focusNode.enclosingScope?.requestFocus(); } }); } @@ -233,11 +234,11 @@ abstract class Route { @mustCallSuper void didAdd() { if (navigator?.widget.requestFocus ?? false) { - // This TickerFuture serves two purposes. First, we want to make sure - // that animations triggered by other operations will finish before focusing the - // navigator. Second, navigator.focusScopeNode might acquire more focused - // children in Route.install asynchronously. This TickerFuture will wait for - // it to finish first. + // This TickerFuture serves two purposes. First, we want to make sure that + // animations triggered by other operations will finish before focusing + // the navigator. Second, navigator.focusNode might acquire more focused + // children in Route.install asynchronously. This TickerFuture will wait + // for it to finish first. // // The later case can be found when subclasses manage their own focus scopes. // For example, ModalRoute creates a focus scope in its overlay entries. The @@ -255,7 +256,7 @@ abstract class Route { // Since the reference to the navigator will be set to null after it is // disposed, we have to do a null-safe operation in case that happens // within the same frame when it is added. - navigator?.focusScopeNode.requestFocus(); + navigator?.focusNode.enclosingScope?.requestFocus(); }); } } @@ -3207,8 +3208,15 @@ class NavigatorState extends State with TickerProviderStateMixin, Res final Queue<_NavigatorObservation> _observedRouteAdditions = Queue<_NavigatorObservation>(); final Queue<_NavigatorObservation> _observedRouteDeletions = Queue<_NavigatorObservation>(); - /// The [FocusScopeNode] for the [FocusScope] that encloses the routes. - final FocusScopeNode focusScopeNode = FocusScopeNode(debugLabel: 'Navigator Scope'); + /// The [FocusScopeNode] for the [FocusScope] that encloses the topmost navigator. + @Deprecated( + 'Use focusNode.enclosingScope! instead. ' + 'This feature was deprecated after v3.1.0-0.0.pre.' + ) + FocusScopeNode get focusScopeNode => focusNode.enclosingScope!; + + /// The [FocusNode] for the [Focus] that encloses the routes. + final FocusNode focusNode = FocusNode(debugLabel: 'Navigator'); bool _debugLocked = false; // used to prevent re-entrant calls to push, pop, and friends @@ -3531,7 +3539,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res return true; }()); _updateHeroController(null); - focusScopeNode.dispose(); + focusNode.dispose(); for (final _RouteEntry entry in _history) { entry.dispose(); } @@ -5226,14 +5234,18 @@ class NavigatorState extends State with TickerProviderStateMixin, Res onPointerCancel: _handlePointerUpOrCancel, child: AbsorbPointer( absorbing: false, // it's mutated directly by _cancelActivePointers above - child: FocusScope( - node: focusScopeNode, - autofocus: true, - child: UnmanagedRestorationScope( - bucket: bucket, - child: Overlay( - key: _overlayKey, - initialEntries: overlay == null ? _allRouteOverlayEntries.toList(growable: false) : const [], + child: FocusTraversalGroup( + child: Focus( + focusNode: focusNode, + autofocus: true, + skipTraversal: true, + includeSemantics: false, + child: UnmanagedRestorationScope( + bucket: bucket, + child: Overlay( + key: _overlayKey, + initialEntries: overlay == null ? _allRouteOverlayEntries.toList(growable: false) : const [], + ), ), ), ), diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 5dbeadebbb..6ceb72fdfe 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -819,7 +819,7 @@ class _ModalScopeState extends State<_ModalScope> { ]; _listenable = Listenable.merge(animations); if (widget.route.isCurrent && _shouldRequestFocus) { - widget.route.navigator!.focusScopeNode.setFirstFocus(focusScopeNode); + widget.route.navigator!.focusNode.enclosingScope?.setFirstFocus(focusScopeNode); } } @@ -828,7 +828,7 @@ class _ModalScopeState extends State<_ModalScope> { super.didUpdateWidget(oldWidget); assert(widget.route == oldWidget.route); if (widget.route.isCurrent && _shouldRequestFocus) { - widget.route.navigator!.focusScopeNode.setFirstFocus(focusScopeNode); + widget.route.navigator!.focusNode.enclosingScope?.setFirstFocus(focusScopeNode); } } @@ -863,7 +863,7 @@ class _ModalScopeState extends State<_ModalScope> { // and route.offstage. void _routeSetState(VoidCallback fn) { if (widget.route.isCurrent && !_shouldIgnoreFocusRequest && _shouldRequestFocus) { - widget.route.navigator!.focusScopeNode.setFirstFocus(focusScopeNode); + widget.route.navigator!.focusNode.enclosingScope?.setFirstFocus(focusScopeNode); } setState(fn); } @@ -1213,7 +1213,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute()); + expect(child, isA()); return const Placeholder(); }, ); diff --git a/packages/flutter/test/material/debug_test.dart b/packages/flutter/test/material/debug_test.dart index 5d659fbed0..055298b909 100644 --- a/packages/flutter/test/material/debug_test.dart +++ b/packages/flutter/test/material/debug_test.dart @@ -156,12 +156,18 @@ void main() { ' Overlay-[LabeledGlobalKey#00000]\n' ' UnmanagedRestorationScope\n' ' _FocusMarker\n' - ' Semantics\n' - ' FocusScope\n' + ' Focus\n' + ' _FocusMarker\n' + ' Focus\n' + ' _FocusTraversalGroupMarker\n' + ' FocusTraversalGroup\n' ' AbsorbPointer\n' ' Listener\n' ' HeroControllerScope\n' ' Navigator-[GlobalObjectKey _WidgetsAppState#00000]\n' + ' _FocusMarker\n' + ' Semantics\n' + ' FocusScope\n' ' DefaultSelectionStyle\n' ' IconTheme\n' ' IconTheme\n' diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index 2f07f0cd45..ec1cb682d6 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -1326,53 +1326,49 @@ void main() { TestSemantics.rootChild( children: [ TestSemantics( + flags: [ + SemanticsFlag.scopesRoute, + SemanticsFlag.namesRoute, + ], + label: 'Popup menu', children: [ TestSemantics( - flags: [ - SemanticsFlag.scopesRoute, - SemanticsFlag.namesRoute, - ], - label: 'Popup menu', children: [ TestSemantics( + flags: [ + SemanticsFlag.hasImplicitScrolling, + ], children: [ TestSemantics( + label: 'one', + textDirection: TextDirection.ltr, flags: [ - SemanticsFlag.hasImplicitScrolling, - ], - children: [ - TestSemantics( - label: 'one', - textDirection: TextDirection.ltr, - flags: [ - SemanticsFlag.isFocused, - SemanticsFlag.isFocusable, - ], - tags: [const SemanticsTag('RenderViewport.twoPane')], - actions: [SemanticsAction.tap], - ), - TestSemantics( - label: 'two', - textDirection: TextDirection.ltr, - flags: [SemanticsFlag.isFocusable], - tags: [const SemanticsTag('RenderViewport.twoPane')], - actions: [SemanticsAction.tap], - ), - TestSemantics( - label: 'three', - textDirection: TextDirection.ltr, - flags: [SemanticsFlag.isFocusable], - tags: [const SemanticsTag('RenderViewport.twoPane')], - actions: [SemanticsAction.tap], - ), - TestSemantics( - label: 'four', - textDirection: TextDirection.ltr, - flags: [SemanticsFlag.isFocusable], - tags: [const SemanticsTag('RenderViewport.twoPane')], - actions: [SemanticsAction.tap], - ), + SemanticsFlag.isFocused, + SemanticsFlag.isFocusable, ], + tags: [const SemanticsTag('RenderViewport.twoPane')], + actions: [SemanticsAction.tap], + ), + TestSemantics( + label: 'two', + textDirection: TextDirection.ltr, + flags: [SemanticsFlag.isFocusable], + tags: [const SemanticsTag('RenderViewport.twoPane')], + actions: [SemanticsAction.tap], + ), + TestSemantics( + label: 'three', + textDirection: TextDirection.ltr, + flags: [SemanticsFlag.isFocusable], + tags: [const SemanticsTag('RenderViewport.twoPane')], + actions: [SemanticsAction.tap], + ), + TestSemantics( + label: 'four', + textDirection: TextDirection.ltr, + flags: [SemanticsFlag.isFocusable], + tags: [const SemanticsTag('RenderViewport.twoPane')], + actions: [SemanticsAction.tap], ), ], ), @@ -1380,7 +1376,6 @@ void main() { ), ], ), - TestSemantics(), ], ), ],