focus_manager.dart 74.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6
import 'dart:ui';
7 8

import 'package:flutter/foundation.dart';
9
import 'package:flutter/gestures.dart';
10 11
import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';
12

13 14
import 'binding.dart';
import 'focus_scope.dart';
15
import 'focus_traversal.dart';
16 17
import 'framework.dart';

18 19 20 21
// Used for debugging focus code. Set to true to see highly verbose debug output
// when focus changes occur.
const bool _kDebugFocus = false;

22
bool _focusDebug(String message, [Iterable<String>? details]) {
23 24 25
  if (_kDebugFocus) {
    debugPrint('FOCUS: $message');
    if (details != null && details.isNotEmpty) {
26
      for (final String detail in details) {
27
        debugPrint('    $detail');
28 29
      }
    }
30 31
  }
  return true;
32 33
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/// An enum that describes how to handle a key event handled by a
/// [FocusOnKeyCallback].
enum KeyEventResult {
  /// The key event has been handled, and the event should not be propagated to
  /// other key event handlers.
  handled,
  /// The key event has not been handled, and the event should continue to be
  /// propagated to other key event handlers, even non-Flutter ones.
  ignored,
  /// The key event has not been handled, but the key event should not be
  /// propagated to other key event handlers.
  ///
  /// It will be returned to the platform embedding to be propagated to text
  /// fields and non-Flutter key event handlers on the platform.
  skipRemainingHandlers,
}

51 52
/// Signature of a callback used by [Focus.onKey] and [FocusScope.onKey]
/// to receive key events.
53
///
54
/// The [node] is the node that received the event.
55 56 57 58 59
///
/// Returns a [KeyEventResult] that describes how, and whether, the key event
/// was handled.
// TODO(gspencergoog): Convert this from dynamic to KeyEventResult once migration is complete.
typedef FocusOnKeyCallback = dynamic Function(FocusNode node, RawKeyEvent event);
60 61

/// An attachment point for a [FocusNode].
62
///
63 64 65
/// Using a [FocusAttachment] is rarely needed, unless you are building
/// something akin to the [Focus] or [FocusScope] widgets from scratch.
///
66 67 68 69 70 71
/// Once created, a [FocusNode] must be attached to the widget tree by its
/// _host_ [StatefulWidget] via a [FocusAttachment] object. [FocusAttachment]s
/// are owned by the [StatefulWidget] that hosts a [FocusNode] or
/// [FocusScopeNode]. There can be multiple [FocusAttachment]s for each
/// [FocusNode], but the node will only ever be attached to one of them at a
/// time.
72
///
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/// This attachment is created by calling [FocusNode.attach], usually from the
/// host widget's [State.initState] method. If the widget is updated to have a
/// different focus node, then the new node needs to be attached in
/// [State.didUpdateWidget], after calling [detach] on the previous
/// [FocusAttachment]. Once detached, the attachment is defunct and will no
/// longer make changes to the [FocusNode] through [reparent].
///
/// Without these attachment points, it would be possible for a focus node to
/// simultaneously be attached to more than one part of the widget tree during
/// the build stage.
class FocusAttachment {
  /// A private constructor, because [FocusAttachment]s are only to be created
  /// by [FocusNode.attach].
  FocusAttachment._(this._node) : assert(_node != null);

  // The focus node that this attachment manages an attachment for. The node may
  // not yet have a parent, or may have been detached from this attachment, so
  // don't count on this node being in a usable state.
  final FocusNode _node;

  /// Returns true if the associated node is attached to this attachment.
  ///
  /// It is possible to be attached to the widget tree, but not be placed in
  /// the focus tree (i.e. to not have a parent yet in the focus tree).
  bool get isAttached => _node._attachment == this;

  /// Detaches the [FocusNode] this attachment point is associated with from the
  /// focus tree, and disconnects it from this attachment point.
  ///
  /// Calling [FocusNode.dispose] will also automatically detach the node.
  void detach() {
    assert(_node != null);
105
    assert(_focusDebug('Detaching node:', <String>[_node.toString(), 'With enclosing scope ${_node.enclosingScope}']));
106
    if (isAttached) {
107
      if (_node.hasPrimaryFocus || (_node._manager != null && _node._manager!._markedForFocus == _node)) {
108
        _node.unfocus(disposition: UnfocusDisposition.previouslyFocusedChild);
109
      }
110 111
      // This node is no longer in the tree, so shouldn't send notifications anymore.
      _node._manager?._markDetached(_node);
112 113
      _node._parent?._removeChild(_node);
      _node._attachment = null;
114 115
      assert(!_node.hasPrimaryFocus);
      assert(_node._manager?._markedForFocus != _node);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    }
    assert(!isAttached);
  }

  /// Ensures that the [FocusNode] attached at this attachment point has the
  /// proper parent node, changing it if necessary.
  ///
  /// If given, ensures that the given [parent] node is the parent of the node
  /// that is attached at this attachment point, changing it if necessary.
  /// However, it is usually not necessary to supply an explicit parent, since
  /// [reparent] will use [Focus.of] to determine the correct parent node for
  /// the context given in [FocusNode.attach].
  ///
  /// If [isAttached] is false, then calling this method does nothing.
  ///
  /// Should be called whenever the associated widget is rebuilt in order to
  /// maintain the focus hierarchy.
  ///
  /// A [StatefulWidget] that hosts a [FocusNode] should call this method on the
  /// node it hosts during its [State.build] or [State.didChangeDependencies]
  /// methods in case the widget is moved from one location in the tree to
  /// another location that has a different [FocusScope] or context.
  ///
  /// The optional [parent] argument must be supplied when not using [Focus] and
  /// [FocusScope] widgets to build the focus tree, or if there is a need to
  /// supply the parent explicitly (which are both uncommon).
142
  void reparent({FocusNode? parent}) {
143 144 145
    assert(_node != null);
    if (isAttached) {
      assert(_node.context != null);
146
      parent ??= Focus.maybeOf(_node.context!, scopeOk: true);
147
      parent ??= _node.context!.owner!.focusManager.rootScope;
148 149 150 151 152 153
      assert(parent != null);
      parent._reparent(_node);
    }
  }
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/// Describe what should happen after [FocusNode.unfocus] is called.
///
/// See also:
///
///  * [FocusNode.unfocus], which takes this as its `disposition` parameter.
enum UnfocusDisposition {
  /// Focus the nearest focusable enclosing scope of this node, but do not
  /// descend to locate the leaf [FocusScopeNode.focusedChild] the way
  /// [previouslyFocusedChild] does.
  ///
  /// Focusing the scope in this way clears the [FocusScopeNode.focusedChild]
  /// history for the enclosing scope when it receives focus. Because of this,
  /// calling a traversal method like [FocusNode.nextFocus] after unfocusing
  /// will cause the [FocusTraversalPolicy] to pick the node it thinks should be
  /// first in the scope.
  ///
  /// This is the default disposition for [FocusNode.unfocus].
  scope,

  /// Focus the previously focused child of the nearest focusable enclosing
  /// scope of this node.
  ///
  /// If there is no previously focused child, then this is equivalent to
  /// using the [scope] disposition.
  ///
  /// Unfocusing with this disposition will cause [FocusNode.unfocus] to walk up
  /// the tree to the nearest focusable enclosing scope, then start to walk down
  /// the tree, looking for a focused child at its
  /// [FocusScopeNode.focusedChild].
  ///
  /// If the [FocusScopeNode.focusedChild] is a scope, then look for its
  /// [FocusScopeNode.focusedChild], and so on, finding the leaf
  /// [FocusScopeNode.focusedChild] that is not a scope, or, failing that, a
  /// leaf scope that has no focused child.
  previouslyFocusedChild,
}

191 192 193 194 195
/// An object that can be used by a stateful widget to obtain the keyboard focus
/// and to handle keyboard events.
///
/// _Please see the [Focus] and [FocusScope] widgets, which are utility widgets
/// that manage their own [FocusNode]s and [FocusScopeNode]s, respectively. If
196 197
/// they aren't appropriate, [FocusNode]s can be managed directly, but doing
/// this yourself is rare._
198 199 200 201 202 203 204 205
///
/// [FocusNode]s are persistent objects that form a _focus tree_ that is a
/// representation of the widgets in the hierarchy that are interested in focus.
/// A focus node might need to be created if it is passed in from an ancestor of
/// a [Focus] widget to control the focus of the children from the ancestor, or
/// a widget might need to host one if the widget subsystem is not being used,
/// or if the [Focus] and [FocusScope] widgets provide insufficient control.
///
206
/// [FocusNode]s are organized into _scopes_ (see [FocusScopeNode]), which form
207 208 209
/// sub-trees of nodes that restrict traversal to a group of nodes. Within a
/// scope, the most recent nodes to have focus are remembered, and if a node is
/// focused and then unfocused, the previous node receives focus again.
210 211 212 213 214 215 216 217
///
/// The focus node hierarchy can be traversed using the [parent], [children],
/// [ancestors] and [descendants] accessors.
///
/// [FocusNode]s are [ChangeNotifier]s, so a listener can be registered to
/// receive a notification when the focus changes. If the [Focus] and
/// [FocusScope] widgets are being used to manage the nodes, consider
/// establishing an [InheritedWidget] dependency on them by calling [Focus.of]
218 219 220
/// or [FocusScope.of] instead. [FocusNode.hasFocus] can also be used to
/// establish a similar dependency, especially if all that is needed is to
/// determine whether or not the widget is focused at build time.
221 222 223 224
///
/// To see the focus tree in the debug console, call [debugDumpFocusTree]. To
/// get the focus tree as a string, call [debugDescribeFocusTree].
///
225
/// {@template flutter.widgets.FocusNode.lifecycle}
226 227 228 229
/// ## Lifecycle
///
/// There are several actors involved in the lifecycle of a
/// [FocusNode]/[FocusScopeNode]. They are created and disposed by their
230
/// _owner_, attached, detached, and re-parented using a [FocusAttachment] by
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/// their _host_ (which must be owned by the [State] of a [StatefulWidget]), and
/// they are managed by the [FocusManager]. Different parts of the [FocusNode]
/// API are intended for these different actors.
///
/// [FocusNode]s (and hence [FocusScopeNode]s) are persistent objects that form
/// part of a _focus tree_ that is a sparse representation of the widgets in the
/// hierarchy that are interested in receiving keyboard events. They must be
/// managed like other persistent state, which is typically done by a
/// [StatefulWidget] that owns the node. A stateful widget that owns a focus
/// scope node must call [dispose] from its [State.dispose] method.
///
/// Once created, a [FocusNode] must be attached to the widget tree via a
/// [FocusAttachment] object. This attachment is created by calling [attach],
/// usually from the [State.initState] method. If the hosting widget is updated
/// to have a different focus node, then the updated node needs to be attached
246 247
/// in [State.didUpdateWidget], after calling [FocusAttachment.detach] on the
/// previous [FocusAttachment].
248
///
249 250 251
/// Because [FocusNode]s form a sparse representation of the widget tree, they
/// must be updated whenever the widget tree is rebuilt. This is done by calling
/// [FocusAttachment.reparent], usually from the [State.build] or
252 253 254 255 256 257 258 259 260 261 262
/// [State.didChangeDependencies] methods of the widget that represents the
/// focused region, so that the [BuildContext] assigned to the [FocusScopeNode]
/// can be tracked (the context is used to obtain the [RenderObject], from which
/// the geometry of focused regions can be determined).
///
/// Creating a [FocusNode] each time [State.build] is invoked will cause the
/// focus to be lost each time the widget is built, which is usually not desired
/// behavior (call [unfocus] if losing focus is desired).
///
/// If, as is common, the hosting [StatefulWidget] is also the owner of the
/// focus node, then it will also call [dispose] from its [State.dispose] (in
263 264 265
/// which case the [FocusAttachment.detach] may be skipped, since dispose will
/// automatically detach). If another object owns the focus node, then it must
/// call [dispose] when the node is done being used.
266 267
/// {@endtemplate}
///
268
/// {@template flutter.widgets.FocusNode.keyEvents}
269 270
/// ## Key Event Propagation
///
271 272 273 274 275 276 277
/// The [FocusManager] receives key events from [RawKeyboard] and will pass them
/// to the focused nodes. It starts with the node with the primary focus, and
/// will call the [onKey] callback for that node. If the callback returns false,
/// indicating that it did not handle the event, the [FocusManager] will move to
/// the parent of that node and call its [onKey]. If that [onKey] returns true,
/// then it will stop propagating the event. If it reaches the root
/// [FocusScopeNode], [FocusManager.rootScope], the event is discarded.
278 279
/// {@endtemplate}
///
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
/// ## Focus Traversal
///
/// The term _traversal_, sometimes called _tab traversal_, refers to moving the
/// focus from one widget to the next in a particular order (also sometimes
/// referred to as the _tab order_, since the TAB key is often bound to the
/// action to move to the next widget).
///
/// To give focus to the logical _next_ or _previous_ widget in the UI, call the
/// [nextFocus] or [previousFocus] methods. To give the focus to a widget in a
/// particular direction, call the [focusInDirection] method.
///
/// The policy for what the _next_ or _previous_ widget is, or the widget in a
/// particular direction, is determined by the [FocusTraversalPolicy] in force.
///
/// The ambient policy is determined by looking up the widget hierarchy for a
295 296 297 298
/// [FocusTraversalGroup] widget, and obtaining the focus traversal policy from
/// it. Different focus nodes can inherit difference policies, so part of the
/// app can go in a predefined order (using [OrderedTraversalPolicy]), and part
/// can go in reading order (using [ReadingOrderTraversalPolicy]), depending
299 300
/// upon the use case.
///
301
/// Predefined policies include [WidgetOrderTraversalPolicy],
302 303 304
/// [ReadingOrderTraversalPolicy], [OrderedTraversalPolicy], and
/// [DirectionalFocusTraversalPolicyMixin], but custom policies can be built
/// based upon these policies. See [FocusTraversalPolicy] for more information.
305
///
306 307 308 309
/// {@tool dartpad --template=stateless_widget_scaffold}
/// This example shows how a FocusNode should be managed if not using the
/// [Focus] or [FocusScope] widgets. See the [Focus] widget for a similar
/// example using [Focus] and [FocusScope] widgets.
310 311 312
///
/// ```dart imports
/// import 'package:flutter/services.dart';
313
/// ```
Adam Barth's avatar
Adam Barth committed
314
///
315 316
/// ```dart preamble
/// class ColorfulButton extends StatefulWidget {
317
///   ColorfulButton({Key? key}) : super(key: key);
318 319 320 321
///
///   @override
///   _ColorfulButtonState createState() => _ColorfulButtonState();
/// }
Adam Barth's avatar
Adam Barth committed
322
///
323
/// class _ColorfulButtonState extends State<ColorfulButton> {
324
///   late FocusNode _node;
325
///   bool _focused = false;
326
///   late FocusAttachment _nodeAttachment;
327
///   Color _color = Colors.white;
328
///
329 330 331 332
///   @override
///   void initState() {
///     super.initState();
///     _node = FocusNode(debugLabel: 'Button');
333
///     _node.addListener(_handleFocusChange);
334 335 336
///     _nodeAttachment = _node.attach(context, onKey: _handleKeyPress);
///   }
///
337 338 339 340 341 342 343 344
///   void _handleFocusChange() {
///     if (_node.hasFocus != _focused) {
///       setState(() {
///         _focused = _node.hasFocus;
///       });
///     }
///   }
///
345
///   KeyEventResult _handleKeyPress(FocusNode node, RawKeyEvent event) {
346 347 348 349 350 351 352
///     if (event is RawKeyDownEvent) {
///       print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
///       if (event.logicalKey == LogicalKeyboardKey.keyR) {
///         print('Changing color to red.');
///         setState(() {
///           _color = Colors.red;
///         });
353
///         return KeyEventResult.handled;
354 355 356 357 358
///       } else if (event.logicalKey == LogicalKeyboardKey.keyG) {
///         print('Changing color to green.');
///         setState(() {
///           _color = Colors.green;
///         });
359
///         return KeyEventResult.handled;
360 361 362 363 364
///       } else if (event.logicalKey == LogicalKeyboardKey.keyB) {
///         print('Changing color to blue.');
///         setState(() {
///           _color = Colors.blue;
///         });
365
///         return KeyEventResult.handled;
366 367
///       }
///     }
368
///     return KeyEventResult.ignored;
369 370 371 372
///   }
///
///   @override
///   void dispose() {
373
///     _node.removeListener(_handleFocusChange);
374 375 376 377 378 379 380 381 382 383
///     // The attachment will automatically be detached in dispose().
///     _node.dispose();
///     super.dispose();
///   }
///
///   @override
///   Widget build(BuildContext context) {
///     _nodeAttachment.reparent();
///     return GestureDetector(
///       onTap: () {
384
///         if (_focused) {
385 386
///             _node.unfocus();
///         } else {
387
///            _node.requestFocus();
388 389 390 391 392 393
///         }
///       },
///       child: Center(
///         child: Container(
///           width: 400,
///           height: 100,
394
///           color: _focused ? _color : Colors.white,
395 396
///           alignment: Alignment.center,
///           child: Text(
397
///               _focused ? "I'm in color! Press R,G,B!" : 'Press to focus'),
398 399 400 401 402 403 404 405 406 407 408
///         ),
///       ),
///     );
///   }
/// }
/// ```
///
/// ```dart
/// Widget build(BuildContext context) {
///   final TextTheme textTheme = Theme.of(context).textTheme;
///   return DefaultTextStyle(
409
///     style: textTheme.headline4!,
410 411 412 413 414
///     child: ColorfulButton(),
///   );
/// }
/// ```
/// {@end-tool}
415
///
416
/// See also:
Adam Barth's avatar
Adam Barth committed
417
///
418 419 420 421 422 423 424 425
/// * [Focus], a widget that manages a [FocusNode] and provides access to focus
///   information and actions to its descendant widgets.
/// * [FocusTraversalGroup], a widget used to group together and configure the
///   focus traversal policy for a widget subtree.
/// * [FocusManager], a singleton that manages the primary focus and distributes
///   key events to focused nodes.
/// * [FocusTraversalPolicy], a class used to determine how to move the focus to
///   other nodes.
426 427 428 429
class FocusNode with DiagnosticableTreeMixin, ChangeNotifier {
  /// Creates a focus node.
  ///
  /// The [debugLabel] is ignored on release builds.
430
  ///
431 432
  /// The [skipTraversal], [descendantsAreFocusable], and [canRequestFocus]
  /// arguments must not be null.
433
  FocusNode({
434
    String? debugLabel,
435
    this.onKey,
436 437
    bool skipTraversal = false,
    bool canRequestFocus = true,
438
    bool descendantsAreFocusable = true,
439
  })  : assert(skipTraversal != null),
440
        assert(canRequestFocus != null),
441
        assert(descendantsAreFocusable != null),
442 443
        _skipTraversal = skipTraversal,
        _canRequestFocus = canRequestFocus,
444
        _descendantsAreFocusable = descendantsAreFocusable {
445 446 447 448
    // Set it via the setter so that it does nothing on release builds.
    this.debugLabel = debugLabel;
  }

449 450 451 452 453 454
  /// If true, tells the focus traversal policy to skip over this node for
  /// purposes of the traversal algorithm.
  ///
  /// This may be used to place nodes in the focus tree that may be focused, but
  /// not traversed, allowing them to receive key events as part of the focus
  /// chain, but not be traversed to via focus traversal.
455 456 457 458 459 460 461 462 463
  ///
  /// This is different from [canRequestFocus] because it only implies that the
  /// node can't be reached via traversal, not that it can't be focused. It may
  /// still be focused explicitly.
  bool get skipTraversal => _skipTraversal;
  bool _skipTraversal;
  set skipTraversal(bool value) {
    if (value != _skipTraversal) {
      _skipTraversal = value;
464
      _manager?._markPropertiesChanged(this);
465 466 467 468 469 470
    }
  }

  /// If true, this focus node may request the primary focus.
  ///
  /// Defaults to true.  Set to false if you want this node to do nothing when
471 472 473 474 475 476 477 478 479 480
  /// [requestFocus] is called on it.
  ///
  /// If set to false on a [FocusScopeNode], will cause all of the children of
  /// the scope node to not be focusable.
  ///
  /// If set to false on a [FocusNode], it will not affect the children of the
  /// node.
  ///
  /// The [hasFocus] member can still return true if this node is the ancestor
  /// of a node with primary focus.
481 482 483 484 485 486 487 488 489 490
  ///
  /// This is different than [skipTraversal] because [skipTraversal] still
  /// allows the node to be focused, just not traversed to via the
  /// [FocusTraversalPolicy]
  ///
  /// Setting [canRequestFocus] to false implies that the node will also be
  /// skipped for traversal purposes.
  ///
  /// See also:
  ///
491 492
  ///  * [FocusTraversalGroup], a widget used to group together and configure the
  ///    focus traversal policy for a widget subtree.
493 494
  ///  * [FocusTraversalPolicy], a class that can be extended to describe a
  ///    traversal policy.
495
  bool get canRequestFocus {
496 497 498
    if (!_canRequestFocus) {
      return false;
    }
499
    final FocusScopeNode? scope = enclosingScope;
500 501 502 503 504 505 506 507 508
    if (scope != null && !scope.canRequestFocus) {
      return false;
    }
    for (final FocusNode ancestor in ancestors) {
      if (!ancestor.descendantsAreFocusable) {
        return false;
      }
    }
    return true;
509
  }
510

511
  bool _canRequestFocus;
512
  @mustCallSuper
513 514
  set canRequestFocus(bool value) {
    if (value != _canRequestFocus) {
515 516 517 518
      // Have to set this first before unfocusing, since it checks this to cull
      // unfocusable, previously-focused children.
      _canRequestFocus = value;
      if (hasFocus && !value) {
519
        unfocus(disposition: UnfocusDisposition.previouslyFocusedChild);
520
      }
521
      _manager?._markPropertiesChanged(this);
522 523
    }
  }
524

525 526 527 528 529 530 531 532 533 534 535
  /// If false, will disable focus for all of this node's descendants.
  ///
  /// Defaults to true. Does not affect focusability of this node: for that,
  /// use [canRequestFocus].
  ///
  /// If any descendants are focused when this is set to false, they will be
  /// unfocused. When `descendantsAreFocusable` is set to true again, they will
  /// not be refocused, although they will be able to accept focus again.
  ///
  /// Does not affect the value of [canRequestFocus] on the descendants.
  ///
536 537 538
  /// If a descendant node loses focus when this value is changed, the focus
  /// will move to the scope enclosing this node.
  ///
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  /// See also:
  ///
  ///  * [ExcludeFocus], a widget that uses this property to conditionally
  ///    exclude focus for a subtree.
  ///  * [Focus], a widget that exposes this setting as a parameter.
  ///  * [FocusTraversalGroup], a widget used to group together and configure
  ///    the focus traversal policy for a widget subtree that also has an
  ///    `descendantsAreFocusable` parameter that prevents its children from
  ///    being focused.
  bool get descendantsAreFocusable => _descendantsAreFocusable;
  bool _descendantsAreFocusable;
  @mustCallSuper
  set descendantsAreFocusable(bool value) {
    if (value == _descendantsAreFocusable) {
      return;
    }
555 556 557
    // Set _descendantsAreFocusable before unfocusing, so the scope won't try
    // and focus any of the children here again if it is false.
    _descendantsAreFocusable = value;
558
    if (!value && hasFocus) {
559
      unfocus(disposition: UnfocusDisposition.previouslyFocusedChild);
560 561 562 563
    }
    _manager?._markPropertiesChanged(this);
  }

564 565 566 567
  /// The context that was supplied to [attach].
  ///
  /// This is typically the context for the widget that is being focused, as it
  /// is used to determine the bounds of the widget.
568 569
  BuildContext? get context => _context;
  BuildContext? _context;
570 571 572 573

  /// Called if this focus node receives a key event while focused (i.e. when
  /// [hasFocus] returns true).
  ///
574
  /// {@macro flutter.widgets.FocusNode.keyEvents}
575
  FocusOnKeyCallback? onKey;
576

577 578 579
  FocusManager? _manager;
  List<FocusNode>? _ancestors;
  List<FocusNode>? _descendants;
580
  bool _hasKeyboardToken = false;
581

582
  /// Returns the parent node for this object.
Adam Barth's avatar
Adam Barth committed
583
  ///
584 585 586
  /// All nodes except for the root [FocusScopeNode] ([FocusManager.rootScope])
  /// will be given a parent when they are added to the focus tree, which is
  /// done using [FocusAttachment.reparent].
587 588
  FocusNode? get parent => _parent;
  FocusNode? _parent;
589 590 591 592 593

  /// An iterator over the children of this node.
  Iterable<FocusNode> get children => _children;
  final List<FocusNode> _children = <FocusNode>[];

594 595
  /// An iterator over the children that are allowed to be traversed by the
  /// [FocusTraversalPolicy].
596
  Iterable<FocusNode> get traversalChildren {
597 598 599
    if (!canRequestFocus) {
      return const <FocusNode>[];
    }
600
    return children.where(
601
      (FocusNode node) => !node.skipTraversal && node.canRequestFocus,
602 603
    );
  }
604

605
  /// A debug label that is used for diagnostic output.
Adam Barth's avatar
Adam Barth committed
606
  ///
607
  /// Will always return null in release builds.
608 609 610
  String? get debugLabel => _debugLabel;
  String? _debugLabel;
  set debugLabel(String? value) {
611 612 613 614 615 616 617
    assert(() {
      // Only set the value in debug builds.
      _debugLabel = value;
      return true;
    }());
  }

618
  FocusAttachment? _attachment;
619 620 621

  /// An [Iterable] over the hierarchy of children below this one, in
  /// depth-first order.
622 623 624
  Iterable<FocusNode> get descendants {
    if (_descendants == null) {
      final List<FocusNode> result = <FocusNode>[];
625
      for (final FocusNode child in _children) {
626 627 628 629
        result.addAll(child.descendants);
        result.add(child);
      }
      _descendants = result;
630
    }
631
    return _descendants!;
632 633
  }

634 635
  /// Returns all descendants which do not have the [skipTraversal] and do have
  /// the [canRequestFocus] flag set.
636
  Iterable<FocusNode> get traversalDescendants => descendants.where((FocusNode node) => !node.skipTraversal && node.canRequestFocus);
637

638
  /// An [Iterable] over the ancestors of this node.
Adam Barth's avatar
Adam Barth committed
639
  ///
640 641
  /// Iterates the ancestors of this node starting at the parent and iterating
  /// over successively more remote ancestors of this node, ending at the root
642
  /// [FocusScopeNode] ([FocusManager.rootScope]).
643 644 645
  Iterable<FocusNode> get ancestors {
    if (_ancestors == null) {
      final List<FocusNode> result = <FocusNode>[];
646
      FocusNode? parent = _parent;
647 648 649 650 651
      while (parent != null) {
        result.add(parent);
        parent = parent._parent;
      }
      _ancestors = result;
652
    }
653
    return _ancestors!;
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
  }

  /// Whether this node has input focus.
  ///
  /// A [FocusNode] has focus when it is an ancestor of a node that returns true
  /// from [hasPrimaryFocus], or it has the primary focus itself.
  ///
  /// The [hasFocus] accessor is different from [hasPrimaryFocus] in that
  /// [hasFocus] is true if the node is anywhere in the focus chain, but for
  /// [hasPrimaryFocus] the node must to be at the end of the chain to return
  /// true.
  ///
  /// A node that returns true for [hasFocus] will receive key events if none of
  /// its focused descendants returned true from their [onKey] handler.
  ///
  /// This object is a [ChangeNotifier], and notifies its [Listenable] listeners
  /// (registered via [addListener]) whenever this value changes.
  ///
  /// See also:
  ///
674 675
  ///  * [Focus.isAt], which is a static method that will return the focus
  ///    state of the nearest ancestor [Focus] widget's focus node.
676
  bool get hasFocus => hasPrimaryFocus || (_manager?.primaryFocus?.ancestors.contains(this) ?? false);
677 678 679 680 681 682 683 684 685 686 687 688 689

  /// Returns true if this node currently has the application-wide input focus.
  ///
  /// A [FocusNode] has the primary focus when the node is focused in its
  /// nearest ancestor [FocusScopeNode] and [hasFocus] is true for all its
  /// ancestor nodes, but none of its descendants.
  ///
  /// This is different from [hasFocus] in that [hasFocus] is true if the node
  /// is anywhere in the focus chain, but here the node has to be at the end of
  /// the chain to return true.
  ///
  /// A node that returns true for [hasPrimaryFocus] will be the first node to
  /// receive key events through its [onKey] handler.
Adam Barth's avatar
Adam Barth committed
690
  ///
691
  /// This object notifies its listeners whenever this value changes.
692
  bool get hasPrimaryFocus => _manager?.primaryFocus == this;
693

694
  /// Returns the [FocusHighlightMode] that is currently in effect for this node.
695
  FocusHighlightMode get highlightMode => FocusManager.instance.highlightMode;
696

697 698 699 700 701 702
  /// Returns the nearest enclosing scope node above this node, including
  /// this node, if it's a scope.
  ///
  /// Returns null if no scope is found.
  ///
  /// Use [enclosingScope] to look for scopes above this node.
703
  FocusScopeNode? get nearestScope => enclosingScope;
704 705 706 707 708 709 710 711

  /// Returns the nearest enclosing scope node above this node, or null if the
  /// node has not yet be added to the focus tree.
  ///
  /// If this node is itself a scope, this will only return ancestors of this
  /// scope.
  ///
  /// Use [nearestScope] to start at this node instead of above it.
712 713 714 715 716 717
  FocusScopeNode? get enclosingScope {
    for (final FocusNode node in ancestors) {
      if (node is FocusScopeNode)
        return node;
    }
    return null;
718 719 720 721
  }

  /// Returns the size of the attached widget's [RenderObject], in logical
  /// units.
722 723 724
  ///
  /// Size is the size of the transformed widget in global coordinates.
  Size get size => rect.size;
725 726 727

  /// Returns the global offset to the upper left corner of the attached
  /// widget's [RenderObject], in logical units.
728 729
  ///
  /// Offset is the offset of the transformed widget in global coordinates.
730 731 732 733
  Offset get offset {
    assert(
        context != null,
        "Tried to get the offset of a focus node that didn't have its context set yet.\n"
734 735
        'The context needs to be set before trying to evaluate traversal policies. '
        'Setting the context is typically done with the attach method.');
736
    final RenderObject object = context!.findRenderObject()!;
737 738 739 740 741
    return MatrixUtils.transformPoint(object.getTransformTo(null), object.semanticBounds.topLeft);
  }

  /// Returns the global rectangle of the attached widget's [RenderObject], in
  /// logical units.
742 743
  ///
  /// Rect is the rectangle of the transformed widget in global coordinates.
744 745 746 747
  Rect get rect {
    assert(
        context != null,
        "Tried to get the bounds of a focus node that didn't have its context set yet.\n"
748 749
        'The context needs to be set before trying to evaluate traversal policies. '
        'Setting the context is typically done with the attach method.');
750
    final RenderObject object = context!.findRenderObject()!;
751 752 753
    final Offset topLeft = MatrixUtils.transformPoint(object.getTransformTo(null), object.semanticBounds.topLeft);
    final Offset bottomRight = MatrixUtils.transformPoint(object.getTransformTo(null), object.semanticBounds.bottomRight);
    return Rect.fromLTRB(topLeft.dx, topLeft.dy, bottomRight.dx, bottomRight.dy);
754 755
  }

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
  /// Removes the focus on this node by moving the primary focus to another node.
  ///
  /// This method removes focus from a node that has the primary focus, cancels
  /// any outstanding requests to focus it, while setting the primary focus to
  /// another node according to the `disposition`.
  ///
  /// It is safe to call regardless of whether this node has ever requested
  /// focus or not. If this node doesn't have focus or primary focus, nothing
  /// happens.
  ///
  /// The `disposition` argument determines which node will receive primary
  /// focus after this one loses it.
  ///
  /// If `disposition` is set to [UnfocusDisposition.scope] (the default), then
  /// the previously focused node history of the enclosing scope will be
  /// cleared, and the primary focus will be moved to the nearest enclosing
  /// scope ancestor that is enabled for focus, ignoring the
  /// [FocusScopeNode.focusedChild] for that scope.
  ///
  /// If `disposition` is set to [UnfocusDisposition.previouslyFocusedChild],
  /// then this node will be removed from the previously focused list in the
  /// [enclosingScope], and the focus will be moved to the previously focused
  /// node of the [enclosingScope], which (if it is a scope itself), will find
  /// its focused child, etc., until a leaf focus node is found. If there is no
  /// previously focused child, then the scope itself will receive focus, as if
  /// [UnfocusDisposition.scope] were specified.
  ///
  /// If you want this node to lose focus and the focus to move to the next or
  /// previous node in the enclosing [FocusTraversalGroup], call [nextFocus] or
  /// [previousFocus] instead of calling `unfocus`.
  ///
787
  /// {@tool dartpad --template=stateful_widget_material}
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  /// This example shows the difference between the different [UnfocusDisposition]
  /// values for [unfocus].
  ///
  /// Try setting focus on the four text fields by selecting them, and then
  /// select "UNFOCUS" to see what happens when the current
  /// [FocusManager.primaryFocus] is unfocused.
  ///
  /// Try pressing the TAB key after unfocusing to see what the next widget
  /// chosen is.
  ///
  /// ```dart imports
  /// import 'package:flutter/foundation.dart';
  /// ```
  ///
  /// ```dart
  /// UnfocusDisposition disposition = UnfocusDisposition.scope;
  ///
  /// @override
  /// Widget build(BuildContext context) {
  ///   return Material(
  ///     child: Container(
  ///       color: Colors.white,
  ///       child: Column(
  ///         mainAxisAlignment: MainAxisAlignment.center,
  ///         children: <Widget>[
  ///           Wrap(
  ///             children: List<Widget>.generate(4, (int index) {
  ///               return SizedBox(
  ///                 width: 200,
  ///                 child: Padding(
  ///                   padding: const EdgeInsets.all(8.0),
  ///                   child: TextField(
  ///                     decoration: InputDecoration(border: OutlineInputBorder()),
  ///                   ),
  ///                 ),
  ///               );
  ///             }),
  ///           ),
  ///           Row(
  ///             mainAxisAlignment: MainAxisAlignment.spaceAround,
  ///             children: <Widget>[
  ///               ...List<Widget>.generate(UnfocusDisposition.values.length,
  ///                   (int index) {
  ///                 return Row(
  ///                   mainAxisSize: MainAxisSize.min,
  ///                   children: <Widget>[
  ///                     Radio<UnfocusDisposition>(
  ///                       groupValue: disposition,
836
  ///                       onChanged: (UnfocusDisposition? value) {
837
  ///                         setState(() {
838 839 840
  ///                           if (value != null) {
  ///                             disposition = value;
  ///                           }
841 842 843 844 845 846 847 848
  ///                         });
  ///                       },
  ///                       value: UnfocusDisposition.values[index],
  ///                     ),
  ///                     Text(describeEnum(UnfocusDisposition.values[index])),
  ///                   ],
  ///                 );
  ///               }),
849
  ///               OutlinedButton(
850 851 852
  ///                 child: const Text('UNFOCUS'),
  ///                 onPressed: () {
  ///                   setState(() {
853
  ///                     primaryFocus!.unfocus(disposition: disposition);
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
  ///                   });
  ///                 },
  ///               ),
  ///             ],
  ///           ),
  ///         ],
  ///       ),
  ///     ),
  ///   );
  /// }
  /// ```
  /// {@end-tool}
  void unfocus({
    UnfocusDisposition disposition = UnfocusDisposition.scope,
  }) {
    assert(disposition != null);
870
    if (!hasFocus && (_manager == null || _manager!._markedForFocus != this)) {
871 872
      return;
    }
873
    FocusScopeNode? scope = enclosingScope;
874 875 876 877
    if (scope == null) {
      // If the scope is null, then this is either the root node, or a node that
      // is not yet in the tree, neither of which do anything when unfocused.
      return;
878
    }
879 880 881 882 883 884 885 886 887 888 889
    switch (disposition) {
      case UnfocusDisposition.scope:
        // If it can't request focus, then don't modify its focused children.
        if (scope.canRequestFocus) {
          // Clearing the focused children here prevents re-focusing the node
          // that we just unfocused if we immediately hit "next" after
          // unfocusing, and also prevents choosing to refocus the next-to-last
          // focused child if unfocus is called more than once.
          scope._focusedChildren.clear();
        }

890
        while (!scope!.canRequestFocus) {
891 892
          scope = scope.enclosingScope ?? _manager?.rootScope;
        }
893
        scope._doRequestFocus(findFirstFocus: false);
894 895 896 897 898
        break;
      case UnfocusDisposition.previouslyFocusedChild:
        // Select the most recent focused child from the nearest focusable scope
        // and focus that. If there isn't one, focus the scope itself.
        if (scope.canRequestFocus) {
899
          scope._focusedChildren.remove(this);
900
        }
901 902
        while (!scope!.canRequestFocus) {
          scope.enclosingScope?._focusedChildren.remove(scope);
903 904
          scope = scope.enclosingScope ?? _manager?.rootScope;
        }
905
        scope._doRequestFocus(findFirstFocus: true);
906
        break;
907
    }
908
    assert(_focusDebug('Unfocused node:', <String>['primary focus was $this', 'next focus will be ${_manager?._markedForFocus}']));
909
  }
910

911 912 913 914 915 916 917 918 919
  /// Removes the keyboard token from this focus node if it has one.
  ///
  /// This mechanism helps distinguish between an input control gaining focus by
  /// default and gaining focus as a result of an explicit user action.
  ///
  /// When a focus node requests the focus (either via
  /// [FocusScopeNode.requestFocus] or [FocusScopeNode.autofocus]), the focus
  /// node receives a keyboard token if it does not already have one. Later,
  /// when the focus node becomes focused, the widget that manages the
920
  /// [TextInputConnection] should show the keyboard (i.e. call
921 922 923
  /// [TextInputConnection.show]) only if it successfully consumes the keyboard
  /// token from the focus node.
  ///
924
  /// Returns true if this method successfully consumes the keyboard token.
925
  bool consumeKeyboardToken() {
926
    if (!_hasKeyboardToken) {
927
      return false;
928
    }
929 930 931 932
    _hasKeyboardToken = false;
    return true;
  }

933 934 935 936 937 938
  // Marks the node as being the next to be focused, meaning that it will become
  // the primary focus and notify listeners of a focus change the next time
  // focus is resolved by the manager. If something else calls _markNextFocus
  // before then, then that node will become the next focus instead of the
  // previous one.
  void _markNextFocus(FocusNode newFocus) {
939 940
    if (_manager != null) {
      // If we have a manager, then let it handle the focus change.
941
      _manager!._markNextFocus(this);
942 943 944
      return;
    }
    // If we don't have a manager, then change the focus locally.
945 946
    newFocus._setAsFocusedChildForScope();
    newFocus._notify();
947 948
    if (newFocus != this) {
      _notify();
949
    }
950 951
  }

952 953
  // Removes the given FocusNode and its children as a child of this node.
  @mustCallSuper
954
  void _removeChild(FocusNode node, {bool removeScopeFocus = true}) {
955
    assert(node != null);
956 957 958
    assert(_children.contains(node), "Tried to remove a node that wasn't a child.");
    assert(node._parent == this);
    assert(node._manager == _manager);
959

960
    if (removeScopeFocus) {
961
      node.enclosingScope?._focusedChildren.remove(node);
962
    }
963

964 965
    node._parent = null;
    _children.remove(node);
966
    for (final FocusNode ancestor in ancestors) {
967 968 969
      ancestor._descendants = null;
    }
    _descendants = null;
970
    assert(_manager == null || !_manager!.rootScope.descendants.contains(node));
971
  }
972

973
  void _updateManager(FocusManager? manager) {
974
    _manager = manager;
975
    for (final FocusNode descendant in descendants) {
976
      descendant._manager = manager;
977
      descendant._ancestors = null;
978 979 980
    }
  }

981 982 983 984 985 986 987 988 989 990
  // Used by FocusAttachment.reparent to perform the actual parenting operation.
  @mustCallSuper
  void _reparent(FocusNode child) {
    assert(child != null);
    assert(child != this, 'Tried to make a child into a parent of itself.');
    if (child._parent == this) {
      assert(_children.contains(child), "Found a node that says it's a child, but doesn't appear in the child list.");
      // The child is already a child of this parent.
      return;
    }
991
    assert(_manager == null || child != _manager!.rootScope, "Reparenting the root node isn't allowed.");
992
    assert(!ancestors.contains(child), 'The supplied child is already an ancestor of this node. Loops are not allowed.');
993
    final FocusScopeNode? oldScope = child.enclosingScope;
994
    final bool hadFocus = child.hasFocus;
995
    child._parent?._removeChild(child, removeScopeFocus: oldScope != nearestScope);
996 997
    _children.add(child);
    child._parent = this;
998
    child._ancestors = null;
999
    child._updateManager(_manager);
1000
    for (final FocusNode ancestor in child.ancestors) {
1001 1002
      ancestor._descendants = null;
    }
1003 1004
    if (hadFocus) {
      // Update the focus chain for the current focus without changing it.
1005
      _manager?.primaryFocus?._setAsFocusedChildForScope();
1006
    }
1007
    if (oldScope != null && child.context != null && child.enclosingScope != oldScope) {
1008
      FocusTraversalGroup.maybeOf(child.context!)?.changedScope(node: child, oldScope: oldScope);
1009
    }
1010
    if (child._requestFocusWhenReparented) {
1011
      child._doRequestFocus(findFirstFocus: true);
1012 1013
      child._requestFocusWhenReparented = false;
    }
1014 1015
  }

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  /// Called by the _host_ [StatefulWidget] to attach a [FocusNode] to the
  /// widget tree.
  ///
  /// In order to attach a [FocusNode] to the widget tree, call [attach],
  /// typically from the [StatefulWidget]'s [State.initState] method.
  ///
  /// If the focus node in the host widget is swapped out, the new node will
  /// need to be attached. [FocusAttachment.detach] should be called on the old
  /// node, and then [attach] called on the new node. This typically happens in
  /// the [State.didUpdateWidget] method.
  @mustCallSuper
1027
  FocusAttachment attach(BuildContext? context, {FocusOnKeyCallback? onKey}) {
1028
    _context = context;
1029
    this.onKey = onKey ?? this.onKey;
1030
    _attachment = FocusAttachment._(this);
1031
    return _attachment!;
1032 1033
  }

1034 1035
  @override
  void dispose() {
1036
    // Detaching will also unfocus and clean up the manager's data structures.
1037 1038
    _attachment?.detach();
    super.dispose();
1039 1040
  }

1041 1042 1043 1044 1045
  @mustCallSuper
  void _notify() {
    if (_parent == null) {
      // no longer part of the tree, so don't notify.
      return;
1046
    }
1047
    if (hasPrimaryFocus) {
1048
      _setAsFocusedChildForScope();
1049
    }
1050
    notifyListeners();
1051 1052
  }

1053 1054
  /// Requests the primary focus for this node, or for a supplied [node], which
  /// will also give focus to its [ancestors].
Adam Barth's avatar
Adam Barth committed
1055
  ///
1056 1057 1058 1059
  /// If called without a node, request focus for this node. If the node hasn't
  /// been added to the focus tree yet, then defer the focus request until it
  /// is, allowing newly created widgets to request focus as soon as they are
  /// added.
1060
  ///
1061 1062
  /// If the given [node] is not yet a part of the focus tree, then this method
  /// will add the [node] as a child of this node before requesting focus.
Adam Barth's avatar
Adam Barth committed
1063
  ///
1064
  /// If the given [node] is a [FocusScopeNode] and that focus scope node has a
1065 1066 1067 1068
  /// non-null [FocusScopeNode.focusedChild], then request the focus for the
  /// focused child. This process is recursive and continues until it encounters
  /// either a focus scope node with a null focused child or an ordinary
  /// (non-scope) [FocusNode] is found.
Adam Barth's avatar
Adam Barth committed
1069
  ///
1070 1071
  /// The node is notified that it has received the primary focus in a
  /// microtask, so notification may lag the request by up to one frame.
1072
  void requestFocus([FocusNode? node]) {
1073 1074 1075 1076
    if (node != null) {
      if (node._parent == null) {
        _reparent(node);
      }
1077
      assert(node.ancestors.contains(this), 'Focus was requested for a node that is not a descendant of the scope from which it was requested.');
1078
      node._doRequestFocus(findFirstFocus: true);
1079
      return;
1080
    }
1081
    _doRequestFocus(findFirstFocus: true);
1082 1083
  }

1084
  // Note that this is overridden in FocusScopeNode.
1085
  void _doRequestFocus({required bool findFirstFocus}) {
1086
    assert(findFirstFocus != null);
1087
    if (!canRequestFocus) {
1088
      assert(_focusDebug('Node NOT requesting focus because canRequestFocus is false: $this'));
1089 1090
      return;
    }
1091 1092 1093 1094 1095 1096 1097
    // If the node isn't part of the tree, then we just defer the focus request
    // until the next time it is reparented, so that it's possible to focus
    // newly added widgets.
    if (_parent == null) {
      _requestFocusWhenReparented = true;
      return;
    }
1098
    _setAsFocusedChildForScope();
1099
    if (hasPrimaryFocus && (_manager!._markedForFocus == null || _manager!._markedForFocus == this)) {
1100
      return;
1101 1102
    }
    _hasKeyboardToken = true;
1103
    assert(_focusDebug('Node requesting focus: $this'));
1104
    _markNextFocus(this);
1105 1106
  }

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  // If set to true, the node will request focus on this node the next time
  // this node is reparented in the focus tree.
  //
  // Once requestFocus has been called at the next reparenting, this value
  // will be reset to false.
  //
  // This will only force a call to requestFocus for the node once the next time
  // the node is reparented. After that, _requestFocusWhenReparented would need
  // to be set to true again to have it be focused again on the next
  // reparenting.
  //
  // This is used when requestFocus is called and there is no parent yet.
  bool _requestFocusWhenReparented = false;

1121 1122 1123 1124 1125 1126 1127 1128 1129
  /// Sets this node as the [FocusScopeNode.focusedChild] of the enclosing
  /// scope.
  ///
  /// Sets this node as the focused child for the enclosing scope, and that
  /// scope as the focused child for the scope above it, etc., until it reaches
  /// the root node. It doesn't change the primary focus, it just changes what
  /// node would be focused if the enclosing scope receives focus, and keeps
  /// track of previously focused children in that scope, so that if the focused
  /// child in that scope is removed, the previous focus returns.
1130
  void _setAsFocusedChildForScope() {
1131
    FocusNode scopeFocus = this;
1132
    for (final FocusScopeNode ancestor in ancestors.whereType<FocusScopeNode>()) {
1133
      assert(scopeFocus != ancestor, 'Somehow made a loop by setting focusedChild to its scope.');
1134
      assert(_focusDebug('Setting $scopeFocus as focused child for scope:', <String>[ancestor.toString()]));
1135 1136 1137 1138 1139 1140 1141
      // Remove it anywhere in the focused child history.
      ancestor._focusedChildren.remove(scopeFocus);
      // Add it to the end of the list, which is also the top of the queue: The
      // end of the list represents the currently focused child.
      ancestor._focusedChildren.add(scopeFocus);
      scopeFocus = ancestor;
    }
1142 1143
  }

1144 1145 1146 1147
  /// Request to move the focus to the next focus node, by calling the
  /// [FocusTraversalPolicy.next] method.
  ///
  /// Returns true if it successfully found a node and requested focus.
1148
  bool nextFocus() => FocusTraversalGroup.of(context!).next(this);
1149 1150 1151 1152 1153

  /// Request to move the focus to the previous focus node, by calling the
  /// [FocusTraversalPolicy.previous] method.
  ///
  /// Returns true if it successfully found a node and requested focus.
1154
  bool previousFocus() => FocusTraversalGroup.of(context!).previous(this);
1155 1156 1157 1158 1159

  /// Request to move the focus to the nearest focus node in the given
  /// direction, by calling the [FocusTraversalPolicy.inDirection] method.
  ///
  /// Returns true if it successfully found a node and requested focus.
1160
  bool focusInDirection(TraversalDirection direction) => FocusTraversalGroup.of(context!).inDirection(this, direction);
1161

1162 1163 1164 1165
  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<BuildContext>('context', context, defaultValue: null));
1166
    properties.add(FlagProperty('descendantsAreFocusable', value: descendantsAreFocusable, ifFalse: 'DESCENDANTS UNFOCUSABLE', defaultValue: true));
1167
    properties.add(FlagProperty('canRequestFocus', value: canRequestFocus, ifFalse: 'NOT FOCUSABLE', defaultValue: true));
1168 1169
    properties.add(FlagProperty('hasFocus', value: hasFocus && !hasPrimaryFocus, ifTrue: 'IN FOCUS PATH', defaultValue: false));
    properties.add(FlagProperty('hasPrimaryFocus', value: hasPrimaryFocus, ifTrue: 'PRIMARY FOCUS', defaultValue: false));
1170 1171
  }

1172 1173 1174 1175 1176 1177
  @override
  List<DiagnosticsNode> debugDescribeChildren() {
    int count = 1;
    return _children.map<DiagnosticsNode>((FocusNode child) {
      return child.toDiagnosticsNode(name: 'Child ${count++}');
    }).toList();
1178
  }
1179 1180 1181

  @override
  String toStringShort() {
1182
    final bool hasDebugLabel = debugLabel != null && debugLabel!.isNotEmpty;
1183 1184 1185 1186 1187
    final String extraData = '${hasDebugLabel ? debugLabel : ''}'
        '${hasFocus && hasDebugLabel ? ' ' : ''}'
        '${hasFocus && !hasPrimaryFocus ? '[IN FOCUS PATH]' : ''}'
        '${hasPrimaryFocus ? '[PRIMARY FOCUS]' : ''}';
    return '${describeIdentity(this)}${extraData.isNotEmpty ? '($extraData)' : ''}';
1188
  }
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
}

/// A subclass of [FocusNode] that acts as a scope for its descendants,
/// maintaining information about which descendant is currently or was last
/// focused.
///
/// _Please see the [FocusScope] and [Focus] widgets, which are utility widgets
/// that manage their own [FocusScopeNode]s and [FocusNode]s, respectively. If
/// they aren't appropriate, [FocusScopeNode]s can be managed directly._
///
1199
/// [FocusScopeNode] organizes [FocusNode]s into _scopes_. Scopes form sub-trees
1200 1201 1202 1203 1204 1205 1206 1207
/// of nodes that can be traversed as a group. Within a scope, the most recent
/// nodes to have focus are remembered, and if a node is focused and then
/// removed, the original node receives focus again.
///
/// From a [FocusScopeNode], calling [setFirstFocus], sets the given focus scope
/// as the [focusedChild] of this node, adopting if it isn't already part of the
/// focus tree.
///
1208 1209
/// {@macro flutter.widgets.FocusNode.lifecycle}
/// {@macro flutter.widgets.FocusNode.keyEvents}
1210 1211 1212
///
/// See also:
///
1213 1214
///  * [Focus], a widget that manages a [FocusNode] and provides access to focus
///    information and actions to its descendant widgets.
1215 1216
///  * [FocusManager], a singleton that manages the primary focus and
///    distributes key events to focused nodes.
1217
class FocusScopeNode extends FocusNode {
1218
  /// Creates a [FocusScopeNode].
1219 1220 1221
  ///
  /// All parameters are optional.
  FocusScopeNode({
1222 1223
    String? debugLabel,
    FocusOnKeyCallback? onKey,
1224 1225 1226 1227 1228 1229 1230 1231
    bool skipTraversal = false,
    bool canRequestFocus = true,
  })  : assert(skipTraversal != null),
        assert(canRequestFocus != null),
        super(
          debugLabel: debugLabel,
          onKey: onKey,
          canRequestFocus: canRequestFocus,
1232
          descendantsAreFocusable: true,
1233 1234
          skipTraversal: skipTraversal,
        );
1235 1236 1237

  @override
  FocusScopeNode get nearestScope => this;
1238

1239
  /// Returns true if this scope is the focused child of its parent scope.
1240
  bool get isFirstFocus => enclosingScope!.focusedChild == this;
1241 1242 1243

  /// Returns the child of this node that should receive focus if this scope
  /// node receives focus.
1244
  ///
1245 1246
  /// If [hasFocus] is true, then this points to the child of this node that is
  /// currently focused.
1247
  ///
1248
  /// Returns null if there is no currently focused child.
1249
  FocusNode? get focusedChild {
1250
    assert(_focusedChildren.isEmpty || _focusedChildren.last.enclosingScope == this, 'Focused child does not have the same idea of its enclosing scope as the scope does.');
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    return _focusedChildren.isNotEmpty ? _focusedChildren.last : null;
  }

  // A stack of the children that have been set as the focusedChild, most recent
  // last (which is the top of the stack).
  final List<FocusNode> _focusedChildren = <FocusNode>[];

  /// Make the given [scope] the active child scope for this scope.
  ///
  /// If the given [scope] is not yet a part of the focus tree, then add it to
  /// the tree as a child of this scope. If it is already part of the focus
  /// tree, the given scope must be a descendant of this scope.
  void setFirstFocus(FocusScopeNode scope) {
    assert(scope != null);
1265
    assert(scope != this, 'Unexpected self-reference in setFirstFocus.');
1266
    assert(_focusDebug('Setting scope as first focus in $this to node:', <String>[scope.toString()]));
1267 1268 1269 1270
    if (scope._parent == null) {
      _reparent(scope);
    }
    assert(scope.ancestors.contains(this), '$FocusScopeNode $scope must be a child of $this to set it as first focus.');
1271
    if (hasFocus) {
1272
      scope._doRequestFocus(findFirstFocus: true);
1273
    } else {
1274
      scope._setAsFocusedChildForScope();
1275
    }
1276 1277
  }

1278
  /// If this scope lacks a focus, request that the given node become the focus.
1279
  ///
1280 1281
  /// If the given node is not yet part of the focus tree, then add it as a
  /// child of this node.
1282
  ///
1283 1284 1285 1286 1287 1288
  /// Useful for widgets that wish to grab the focus if no other widget already
  /// has the focus.
  ///
  /// The node is notified that it has received the primary focus in a
  /// microtask, so notification may lag the request by up to one frame.
  void autofocus(FocusNode node) {
1289
    assert(_focusDebug('Node autofocusing: $node'));
1290 1291 1292 1293
    if (focusedChild == null) {
      if (node._parent == null) {
        _reparent(node);
      }
1294
      assert(node.ancestors.contains(this), 'Autofocus was requested for a node that is not a descendant of the scope from which it was requested.');
1295
      node._doRequestFocus(findFirstFocus: true);
1296
    }
1297 1298
  }

1299
  @override
1300
  void _doRequestFocus({required bool findFirstFocus}) {
1301 1302 1303
    assert(findFirstFocus != null);

    // It is possible that a previously focused child is no longer focusable.
1304
    while (focusedChild != null && !focusedChild!.canRequestFocus)
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
      _focusedChildren.removeLast();

    // If findFirstFocus is false, then the request is to make this scope the
    // focus instead of looking for the ultimate first focus for this scope and
    // its descendants.
    if (!findFirstFocus) {
      if (canRequestFocus) {
        _setAsFocusedChildForScope();
        _markNextFocus(this);
      }
      return;
    }

1318 1319 1320 1321 1322 1323 1324
    // Start with the primary focus as the focused child of this scope, if there
    // is one. Otherwise start with this node itself.
    FocusNode primaryFocus = focusedChild ?? this;
    // Keep going down through scopes until the ultimately focusable item is
    // found, a scope doesn't have a focusedChild, or a non-scope is
    // encountered.
    while (primaryFocus is FocusScopeNode && primaryFocus.focusedChild != null) {
1325
      primaryFocus = primaryFocus.focusedChild!;
1326
    }
1327 1328 1329 1330
    if (identical(primaryFocus, this)) {
      // We didn't find a FocusNode at the leaf, so we're focusing the scope, if
      // allowed.
      if (primaryFocus.canRequestFocus) {
1331 1332
        _setAsFocusedChildForScope();
        _markNextFocus(this);
1333
      }
1334
    } else {
1335 1336 1337
      // We found a FocusScopeNode at the leaf, so ask it to focus itself
      // instead of this scope. That will cause this scope to return true from
      // hasFocus, but false from hasPrimaryFocus.
1338
      primaryFocus._doRequestFocus(findFirstFocus: findFirstFocus);
1339
    }
1340 1341 1342
  }

  @override
1343 1344
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
1345 1346 1347 1348
    if (_focusedChildren.isEmpty) {
      return;
    }
    final List<String> childList = _focusedChildren.reversed.map<String>((FocusNode child) {
1349
      return child.toStringShort();
1350 1351
    }).toList();
    properties.add(IterableProperty<String>('focusedChildren', childList, defaultValue: <String>[]));
1352 1353 1354
  }
}

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
/// An enum to describe which kind of focus highlight behavior to use when
/// displaying focus information.
enum FocusHighlightMode {
  /// Touch interfaces will not show the focus highlight except for controls
  /// which bring up the soft keyboard.
  ///
  /// If a device that uses a traditional mouse and keyboard has a touch screen
  /// attached, it can also enter `touch` mode if the user is using the touch
  /// screen.
  touch,

  /// Traditional interfaces (keyboard and mouse) will show the currently
  /// focused control via a focus highlight of some sort.
  ///
  /// If a touch device (like a mobile phone) has a keyboard and/or mouse
  /// attached, it also can enter `traditional` mode if the user is using these
  /// input devices.
  traditional,
}

/// An enum to describe how the current value of [FocusManager.highlightMode] is
/// determined. The strategy is set on [FocusManager.highlightStrategy].
enum FocusHighlightStrategy {
  /// Automatic switches between the various highlight modes based on the last
  /// kind of input that was received. This is the default.
  automatic,

  /// [FocusManager.highlightMode] always returns [FocusHighlightMode.touch].
  alwaysTouch,

  /// [FocusManager.highlightMode] always returns [FocusHighlightMode.traditional].
  alwaysTraditional,
}

1389 1390
/// Manages the focus tree.
///
1391 1392 1393
/// The focus tree is a separate, sparser, tree from the widget tree that
/// maintains the hierarchical relationship between focusable widgets in the
/// widget tree.
Adam Barth's avatar
Adam Barth committed
1394
///
1395 1396 1397 1398 1399
/// The focus manager is responsible for tracking which [FocusNode] has the
/// primary input focus (the [primaryFocus]), holding the [FocusScopeNode] that
/// is the root of the focus tree (the [rootScope]), and what the current
/// [highlightMode] is. It also distributes key events from [RawKeyboard] to the
/// nodes in the focus tree.
Adam Barth's avatar
Adam Barth committed
1400
///
1401
/// The singleton [FocusManager] instance is held by the [WidgetsBinding] as
1402
/// [WidgetsBinding.focusManager], and can be conveniently accessed using the
1403
/// [FocusManager.instance] static accessor.
1404
///
1405 1406
/// To find the [FocusNode] for a given [BuildContext], use [Focus.of]. To find
/// the [FocusScopeNode] for a given [BuildContext], use [FocusScope.of].
1407
///
1408
/// If you would like notification whenever the [primaryFocus] changes, register
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
/// a listener with [addListener]. When you no longer want to receive these
/// events, as when your object is about to be disposed, you must unregister
/// with [removeListener] to avoid memory leaks. Removing listeners is typically
/// done in [State.dispose] on stateful widgets.
///
/// The [highlightMode] describes how focus highlights should be displayed on
/// components in the UI. The [highlightMode] changes are notified separately
/// via [addHighlightModeListener] and removed with
/// [removeHighlightModeListener]. The highlight mode changes when the user
/// switches from a mouse to a touch interface, or vice versa.
///
/// The widgets that are used to manage focus in the widget tree are:
1421
///
1422 1423 1424 1425 1426 1427
///  * [Focus], a widget that manages a [FocusNode] in the focus tree so that
///    the focus tree reflects changes in the widget hierarchy.
///  * [FocusScope], a widget that manages a [FocusScopeNode] in the focus tree,
///    creating a new scope for restricting focus to a set of focus nodes.
///  * [FocusTraversalGroup], a widget that groups together nodes that should be
///    traversed using an order described by a given [FocusTraversalPolicy].
1428
///
1429
/// See also:
Adam Barth's avatar
Adam Barth committed
1430
///
1431
///  * [FocusNode], which is a node in the focus tree that can receive focus.
1432
///  * [FocusScopeNode], which is a node in the focus tree used to collect
1433 1434 1435
///    subtrees into groups and restrict focus to them.
///  * The [primaryFocus] global accessor, for convenient access from anywhere
///    to the current focus manager state.
1436
class FocusManager with DiagnosticableTreeMixin, ChangeNotifier {
1437
  /// Creates an object that manages the focus tree.
Adam Barth's avatar
Adam Barth committed
1438
  ///
1439
  /// This constructor is rarely called directly. To access the [FocusManager],
1440 1441
  /// consider using the [FocusManager.instance] accessor instead (which gets it
  /// from the [WidgetsBinding] singleton).
1442 1443 1444 1445 1446
  ///
  /// This newly constructed focus manager does not have the necessary event
  /// handlers registered to allow it to manage focus. To register those event
  /// handlers, callers must call [registerGlobalHandlers]. See the
  /// documentation in that method for caveats to watch out for.
1447 1448
  FocusManager() {
    rootScope._manager = this;
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
  }

  /// Registers global input event handlers that are needed to manage focus.
  ///
  /// This sets the [RawKeyboard.keyEventHandler] for the shared instance of
  /// [RawKeyboard] and adds a route to the global entry in the gesture routing
  /// table. As such, only one [FocusManager] instance should register its
  /// global handlers.
  ///
  /// When this focus manager is no longer needed, calling [dispose] on it will
  /// unregister these handlers.
  void registerGlobalHandlers() {
    assert(RawKeyboard.instance.keyEventHandler == null);
1462
    RawKeyboard.instance.keyEventHandler = _handleRawKeyEvent;
1463
    GestureBinding.instance!.pointerRouter.addGlobalRoute(_handlePointerEvent);
1464 1465
  }

1466 1467 1468 1469 1470 1471 1472 1473 1474
  @override
  void dispose() {
    if (RawKeyboard.instance.keyEventHandler == _handleRawKeyEvent) {
      RawKeyboard.instance.keyEventHandler = null;
      GestureBinding.instance!.pointerRouter.removeGlobalRoute(_handlePointerEvent);
    }
    super.dispose();
  }

1475 1476
  /// Provides convenient access to the current [FocusManager] singleton from
  /// the [WidgetsBinding] instance.
1477
  static FocusManager get instance => WidgetsBinding.instance!.focusManager;
1478

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
  /// Sets the strategy by which [highlightMode] is determined.
  ///
  /// If set to [FocusHighlightStrategy.automatic], then the highlight mode will
  /// change depending upon the interaction mode used last. For instance, if the
  /// last interaction was a touch interaction, then [highlightMode] will return
  /// [FocusHighlightMode.touch], and focus highlights will only appear on
  /// widgets that bring up a soft keyboard. If the last interaction was a
  /// non-touch interaction (hardware keyboard press, mouse click, etc.), then
  /// [highlightMode] will return [FocusHighlightMode.traditional], and focus
  /// highlights will appear on all widgets.
  ///
  /// If set to [FocusHighlightStrategy.alwaysTouch] or
  /// [FocusHighlightStrategy.alwaysTraditional], then [highlightMode] will
  /// always return [FocusHighlightMode.touch] or
  /// [FocusHighlightMode.traditional], respectively, regardless of the last UI
  /// interaction type.
  ///
  /// The initial value of [highlightMode] depends upon the value of
1497 1498 1499
  /// [defaultTargetPlatform] and [BaseMouseTracker.mouseIsConnected] of
  /// [RendererBinding.mouseTracker], making a guess about which interaction is
  /// most appropriate for the initial interaction mode.
1500 1501 1502 1503 1504 1505 1506 1507 1508
  ///
  /// Defaults to [FocusHighlightStrategy.automatic].
  FocusHighlightStrategy get highlightStrategy => _highlightStrategy;
  FocusHighlightStrategy _highlightStrategy = FocusHighlightStrategy.automatic;
  set highlightStrategy(FocusHighlightStrategy highlightStrategy) {
    _highlightStrategy = highlightStrategy;
    _updateHighlightMode();
  }

1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
  static FocusHighlightMode get _defaultModeForPlatform {
    // Assume that if we're on one of the mobile platforms, and there's no mouse
    // connected, that the initial interaction will be touch-based, and that
    // it's traditional mouse and keyboard on all other platforms.
    //
    // This only affects the initial value: the ongoing value is updated to a
    // known correct value as soon as any pointer/keyboard events are received.
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
      case TargetPlatform.iOS:
1520
        if (WidgetsBinding.instance!.mouseTracker.mouseIsConnected) {
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
          return FocusHighlightMode.traditional;
        }
        return FocusHighlightMode.touch;
      case TargetPlatform.linux:
      case TargetPlatform.macOS:
      case TargetPlatform.windows:
        return FocusHighlightMode.traditional;
    }
  }

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
  /// Indicates the current interaction mode for focus highlights.
  ///
  /// The value returned depends upon the [highlightStrategy] used, and possibly
  /// (depending on the value of [highlightStrategy]) the most recent
  /// interaction mode that they user used.
  ///
  /// If [highlightMode] returns [FocusHighlightMode.touch], then widgets should
  /// not draw their focus highlight unless they perform text entry.
  ///
  /// If [highlightMode] returns [FocusHighlightMode.traditional], then widgets should
  /// draw their focus highlight whenever they are focused.
1542 1543 1544
  // Don't want to set _highlightMode here, since it's possible for the target
  // platform to change (especially in tests).
  FocusHighlightMode get highlightMode => _highlightMode ?? _defaultModeForPlatform;
1545
  FocusHighlightMode? _highlightMode;
1546 1547 1548

  // If set, indicates if the last interaction detected was touch or not.
  // If null, no interactions have occurred yet.
1549
  bool? _lastInteractionWasTouch;
1550 1551 1552 1553

  // Update function to be called whenever the state relating to highlightMode
  // changes.
  void _updateHighlightMode() {
1554
    final FocusHighlightMode newMode;
1555 1556
    switch (highlightStrategy) {
      case FocusHighlightStrategy.automatic:
1557 1558 1559 1560 1561 1562 1563
        if (_lastInteractionWasTouch == null) {
          // If we don't have any information about the last interaction yet,
          // then just rely on the default value for the platform, which will be
          // determined based on the target platform if _highlightMode is not
          // set.
          return;
        }
1564
        if (_lastInteractionWasTouch!) {
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
          newMode = FocusHighlightMode.touch;
        } else {
          newMode = FocusHighlightMode.traditional;
        }
        break;
      case FocusHighlightStrategy.alwaysTouch:
        newMode = FocusHighlightMode.touch;
        break;
      case FocusHighlightStrategy.alwaysTraditional:
        newMode = FocusHighlightMode.traditional;
        break;
    }
1577 1578 1579 1580 1581 1582
    // We can't just compare newMode with _highlightMode here, since
    // _highlightMode could be null, so we want to compare with the return value
    // for the getter, since that's what clients will be looking at.
    final FocusHighlightMode oldMode = highlightMode;
    _highlightMode = newMode;
    if (highlightMode != oldMode) {
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
      _notifyHighlightModeListeners();
    }
  }

  // The list of listeners for [highlightMode] state changes.
  final HashedObserverList<ValueChanged<FocusHighlightMode>> _listeners = HashedObserverList<ValueChanged<FocusHighlightMode>>();

  /// Register a closure to be called when the [FocusManager] notifies its listeners
  /// that the value of [highlightMode] has changed.
  void addHighlightModeListener(ValueChanged<FocusHighlightMode> listener) => _listeners.add(listener);

  /// Remove a previously registered closure from the list of closures that the
  /// [FocusManager] notifies.
1596
  void removeHighlightModeListener(ValueChanged<FocusHighlightMode> listener) => _listeners.remove(listener);
1597 1598 1599 1600 1601 1602

  void _notifyHighlightModeListeners() {
    if (_listeners.isEmpty) {
      return;
    }
    final List<ValueChanged<FocusHighlightMode>> localListeners = List<ValueChanged<FocusHighlightMode>>.from(_listeners);
1603
    for (final ValueChanged<FocusHighlightMode> listener in localListeners) {
1604 1605
      try {
        if (_listeners.contains(listener)) {
1606
          listener(highlightMode);
1607 1608
        }
      } catch (exception, stack) {
1609
        InformationCollector? collector;
1610 1611
        assert(() {
          collector = () sync* {
1612 1613 1614 1615 1616
            yield DiagnosticsProperty<FocusManager>(
              'The $runtimeType sending notification was',
              this,
              style: DiagnosticsTreeStyle.errorProperty,
            );
1617 1618 1619 1620 1621 1622 1623 1624 1625
          };
          return true;
        }());
        FlutterError.reportError(FlutterErrorDetails(
          exception: exception,
          stack: stack,
          library: 'widgets library',
          context: ErrorDescription('while dispatching notifications for $runtimeType'),
          informationCollector: collector,
1626 1627 1628
        ));
      }
    }
1629 1630 1631
  }

  /// The root [FocusScopeNode] in the focus tree.
Adam Barth's avatar
Adam Barth committed
1632
  ///
1633 1634 1635 1636
  /// This field is rarely used directly. To find the nearest [FocusScopeNode]
  /// for a given [FocusNode], call [FocusNode.nearestScope].
  final FocusScopeNode rootScope = FocusScopeNode(debugLabel: 'Root Focus Scope');

1637
  void _handlePointerEvent(PointerEvent event) {
1638
    final FocusHighlightMode expectedMode;
1639 1640 1641 1642
    switch (event.kind) {
      case PointerDeviceKind.touch:
      case PointerDeviceKind.stylus:
      case PointerDeviceKind.invertedStylus:
1643 1644
        _lastInteractionWasTouch = true;
        expectedMode = FocusHighlightMode.touch;
1645 1646 1647
        break;
      case PointerDeviceKind.mouse:
      case PointerDeviceKind.unknown:
1648 1649
        _lastInteractionWasTouch = false;
        expectedMode = FocusHighlightMode.traditional;
1650 1651
        break;
    }
1652
    if (expectedMode != highlightMode) {
1653 1654 1655 1656
      _updateHighlightMode();
    }
  }

1657
  bool _handleRawKeyEvent(RawKeyEvent event) {
1658 1659
    // Update highlightMode first, since things responding to the keys might
    // look at the highlight mode, and it should be accurate.
1660 1661
    _lastInteractionWasTouch = false;
    _updateHighlightMode();
1662

1663
    assert(_focusDebug('Received key event ${event.logicalKey}'));
1664
    if (_primaryFocus == null) {
1665
      assert(_focusDebug('No primary focus for key event, ignored: $event'));
1666
      return false;
1667
    }
1668 1669 1670 1671

    // Walk the current focus from the leaf to the root, calling each one's
    // onKey on the way up, and if one responds that they handled it or want to
    // stop propagation, stop.
1672
    bool handled = false;
1673
    for (final FocusNode node in <FocusNode>[_primaryFocus!, ..._primaryFocus!.ancestors]) {
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
      if (node.onKey != null) {
        // TODO(gspencergoog): Convert this from dynamic to KeyEventResult once migration is complete.
        final dynamic result = node.onKey!(node, event);
        assert(result is bool || result is KeyEventResult,
            'Value returned from onKey handler must be a non-null bool or KeyEventResult, not ${result.runtimeType}');
        if (result is KeyEventResult) {
          switch (result) {
            case KeyEventResult.handled:
              assert(_focusDebug('Node $node handled key event $event.'));
              handled = true;
              break;
            case KeyEventResult.skipRemainingHandlers:
              assert(_focusDebug('Node $node stopped key event propagation: $event.'));
              handled = false;
              break;
            case KeyEventResult.ignored:
              continue;
          }
        } else if (result is bool){
          if (result) {
            assert(_focusDebug('Node $node handled key event $event.'));
            handled = true;
            break;
          } else {
            continue;
          }
        }
1701 1702 1703
        break;
      }
    }
1704 1705 1706
    if (!handled) {
      assert(_focusDebug('Key event not handled by anyone: $event.'));
    }
1707
    return handled;
1708 1709
  }

1710
  /// The node that currently has the primary focus.
1711 1712
  FocusNode? get primaryFocus => _primaryFocus;
  FocusNode? _primaryFocus;
1713

1714 1715 1716
  // The set of nodes that need to notify their listeners of changes at the next
  // update.
  final Set<FocusNode> _dirtyNodes = <FocusNode>{};
1717

1718 1719
  // The node that has requested to have the primary focus, but hasn't been
  // given it yet.
1720
  FocusNode? _markedForFocus;
1721 1722 1723 1724 1725 1726 1727 1728

  void _markDetached(FocusNode node) {
    // The node has been removed from the tree, so it no longer needs to be
    // notified of changes.
    assert(_focusDebug('Node was detached: $node'));
    if (_primaryFocus == node) {
      _primaryFocus = null;
    }
1729
    _dirtyNodes.remove(node);
1730 1731 1732 1733 1734
  }

  void _markPropertiesChanged(FocusNode node) {
    _markNeedsUpdate();
    assert(_focusDebug('Properties changed for node $node.'));
1735
    _dirtyNodes.add(node);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
  }

  void _markNextFocus(FocusNode node) {
    if (_primaryFocus == node) {
      // The caller asked for the current focus to be the next focus, so just
      // pretend that didn't happen.
      _markedForFocus = null;
    } else {
      _markedForFocus = node;
      _markNeedsUpdate();
    }
  }

1749
  // True indicates that there is an update pending.
1750
  bool _haveScheduledUpdate = false;
1751 1752 1753

  // Request that an update be scheduled, optionally requesting focus for the
  // given newFocus node.
1754 1755
  void _markNeedsUpdate() {
    assert(_focusDebug('Scheduling update, current focus is $_primaryFocus, next focus will be $_markedForFocus'));
1756
    if (_haveScheduledUpdate) {
1757
      return;
1758
    }
1759
    _haveScheduledUpdate = true;
1760
    scheduleMicrotask(_applyFocusChange);
1761 1762
  }

1763
  void _applyFocusChange() {
1764
    _haveScheduledUpdate = false;
1765
    final FocusNode? previousFocus = _primaryFocus;
1766
    if (_primaryFocus == null && _markedForFocus == null) {
1767
      // If we don't have any current focus, and nobody has asked to focus yet,
1768
      // then revert to the root scope.
1769
      _markedForFocus = rootScope;
1770
    }
1771 1772 1773 1774
    assert(_focusDebug('Refreshing focus state. Next focus will be $_markedForFocus'));
    // A node has requested to be the next focus, and isn't already the primary
    // focus.
    if (_markedForFocus != null && _markedForFocus != _primaryFocus) {
1775 1776
      final Set<FocusNode> previousPath = previousFocus?.ancestors.toSet() ?? <FocusNode>{};
      final Set<FocusNode> nextPath = _markedForFocus!.ancestors.toSet();
1777 1778 1779 1780
      // Notify nodes that are newly focused.
      _dirtyNodes.addAll(nextPath.difference(previousPath));
      // Notify nodes that are no longer focused
      _dirtyNodes.addAll(previousPath.difference(nextPath));
1781 1782 1783

      _primaryFocus = _markedForFocus;
      _markedForFocus = null;
1784
    }
1785
    if (previousFocus != _primaryFocus) {
1786
      assert(_focusDebug('Updating focus from $previousFocus to $_primaryFocus'));
1787 1788 1789
      if (previousFocus != null) {
        _dirtyNodes.add(previousFocus);
      }
1790
      if (_primaryFocus != null) {
1791
        _dirtyNodes.add(_primaryFocus!);
1792 1793
      }
    }
1794
    assert(_focusDebug('Notifying ${_dirtyNodes.length} dirty nodes:', _dirtyNodes.toList().map<String>((FocusNode node) => node.toString())));
1795
    for (final FocusNode node in _dirtyNodes) {
1796 1797 1798
      node._notify();
    }
    _dirtyNodes.clear();
1799 1800 1801
    if (previousFocus != _primaryFocus) {
      notifyListeners();
    }
1802 1803 1804 1805 1806 1807
    assert(() {
      if (_kDebugFocus) {
        debugDumpFocusTree();
      }
      return true;
    }());
1808 1809
  }

1810 1811 1812 1813 1814 1815
  @override
  List<DiagnosticsNode> debugDescribeChildren() {
    return <DiagnosticsNode>[
      rootScope.toDiagnosticsNode(name: 'rootScope'),
    ];
  }
1816

1817
  @override
1818 1819
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    properties.add(FlagProperty('haveScheduledUpdate', value: _haveScheduledUpdate, ifTrue: 'UPDATE SCHEDULED'));
1820
    properties.add(DiagnosticsProperty<FocusNode>('primaryFocus', primaryFocus, defaultValue: null));
1821
    properties.add(DiagnosticsProperty<FocusNode>('nextFocus', _markedForFocus, defaultValue: null));
1822
    final Element? element = primaryFocus?.context as Element?;
1823 1824 1825
    if (element != null) {
      properties.add(DiagnosticsProperty<String>('primaryFocusCreator', element.debugGetCreatorChain(20)));
    }
1826 1827
  }
}
1828

1829 1830
/// Provides convenient access to the current [FocusManager.primaryFocus] from the
/// [WidgetsBinding] instance.
1831
FocusNode? get primaryFocus => WidgetsBinding.instance!.focusManager.primaryFocus;
1832

1833 1834 1835 1836 1837 1838
/// Returns a text representation of the current focus tree, along with the
/// current attributes on each node.
///
/// Will return an empty string in release builds.
String debugDescribeFocusTree() {
  assert(WidgetsBinding.instance != null);
1839
  String? result;
1840
  assert(() {
1841
    result = FocusManager.instance.toStringDeep();
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    return true;
  }());
  return result ?? '';
}

/// Prints a text representation of the current focus tree, along with the
/// current attributes on each node.
///
/// Will do nothing in release builds.
void debugDumpFocusTree() {
  assert(() {
    debugPrint(debugDescribeFocusTree());
    return true;
  }());
}