focus_scope.dart 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';

import 'basic.dart';
import 'focus_manager.dart';
import 'framework.dart';

class _FocusScopeMarker extends InheritedWidget {
12
  const _FocusScopeMarker({
13 14 15
    Key key,
    @required this.node,
    Widget child,
16 17
  }) : assert(node != null),
       super(key: key, child: child);
18 19 20 21 22 23 24 25 26 27

  final FocusScopeNode node;

  @override
  bool updateShouldNotify(_FocusScopeMarker oldWidget) {
    return node != oldWidget.node;
  }
}

/// Establishes a scope in which widgets can receive focus.
Adam Barth's avatar
Adam Barth committed
28
///
29 30
/// The focus tree keeps track of which widget is the user's current focus. The
/// focused widget often listens for keyboard events.
Adam Barth's avatar
Adam Barth committed
31
///
32 33 34 35 36
/// The a focus scope does not itself receive focus but instead helps remember
/// previous focus states. A scope is currently active when its [node] is the
/// first focus of its parent scope. To activate a [FocusScope], either use the
/// [autofocus] property or explicitly make the [node] the first focus in the
/// parent scope:
Adam Barth's avatar
Adam Barth committed
37
///
38 39 40
/// ```dart
/// FocusScope.of(context).setFirstFocus(node);
/// ```
Adam Barth's avatar
Adam Barth committed
41
///
42 43
/// When a [FocusScope] is removed from the tree, the previously active
/// [FocusScope] becomes active again.
Adam Barth's avatar
Adam Barth committed
44
///
45
/// See also:
Adam Barth's avatar
Adam Barth committed
46
///
47 48 49 50 51
///  * [FocusScopeNode], which is the associated node in the focus tree.
///  * [FocusNode], which is a leaf node in the focus tree that can receive
///    focus.
class FocusScope extends StatefulWidget {
  /// Creates a scope in which widgets can receive focus.
Adam Barth's avatar
Adam Barth committed
52
  ///
53
  /// The [node] argument must not be null.
54
  const FocusScope({
55 56 57 58
    Key key,
    @required this.node,
    this.autofocus: false,
    this.child,
59 60 61
  }) : assert(node != null),
       assert(autofocus != null),
       super(key: key);
62 63 64 65 66 67 68 69 70

  /// Controls whether this scope is currently active.
  final FocusScopeNode node;

  /// Whether this scope should attempt to become active when first added to
  /// the tree.
  final bool autofocus;

  /// The widget below this widget in the tree.
71 72
  ///
  /// {@macro flutter.widgets.child}
73 74
  final Widget child;

Adam Barth's avatar
Adam Barth committed
75 76
  /// Returns the [node] of the [FocusScope] that most tightly encloses the
  /// given [BuildContext].
77 78
  static FocusScopeNode of(BuildContext context) {
    final _FocusScopeMarker scope = context.inheritFromWidgetOfExactType(_FocusScopeMarker);
79
    return scope?.node ?? context.owner.focusManager.rootScope;
80 81 82 83 84 85 86 87 88 89 90 91
  }

  @override
  _FocusScopeState createState() => new _FocusScopeState();
}

class _FocusScopeState extends State<FocusScope> {
  bool _didAutofocus = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
92 93
    if (!_didAutofocus && widget.autofocus) {
      FocusScope.of(context).setFirstFocus(widget.node);
94 95 96 97 98 99
      _didAutofocus = true;
    }
  }

  @override
  void dispose() {
100
    widget.node.detach();
101 102 103 104 105
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
106
    FocusScope.of(context).reparentScopeIfNeeded(widget.node);
107
    return new Semantics(
108
      explicitChildNodes: true,
109
      child: new _FocusScopeMarker(
110 111
        node: widget.node,
        child: widget.child,
112 113 114 115
      ),
    );
  }
}