focus_scope.dart 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 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 'binding.dart';
import 'focus_manager.dart';
import 'framework.dart';

class _FocusScopeMarker extends InheritedWidget {
  _FocusScopeMarker({
    Key key,
    @required this.node,
    Widget child,
  }) : super(key: key, child: child) {
    assert(node != null);
  }

  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
30
///
31 32
/// 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
33
///
34 35 36 37 38
/// 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
39
///
40 41 42
/// ```dart
/// FocusScope.of(context).setFirstFocus(node);
/// ```
Adam Barth's avatar
Adam Barth committed
43
///
44 45
/// When a [FocusScope] is removed from the tree, the previously active
/// [FocusScope] becomes active again.
Adam Barth's avatar
Adam Barth committed
46
///
47
/// See also:
Adam Barth's avatar
Adam Barth committed
48
///
49 50 51 52 53
///  * [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
54
  ///
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  /// The [node] argument must not be null.
  FocusScope({
    Key key,
    @required this.node,
    this.autofocus: false,
    this.child,
  }) : super(key: key) {
    assert(node != null);
    assert(autofocus != null);
  }

  /// 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.
  final Widget child;

Adam Barth's avatar
Adam Barth committed
76 77
  /// Returns the [node] of the [FocusScope] that most tightly encloses the
  /// given [BuildContext].
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 105 106 107 108 109 110 111 112 113 114 115 116
  static FocusScopeNode of(BuildContext context) {
    final _FocusScopeMarker scope = context.inheritFromWidgetOfExactType(_FocusScopeMarker);
    return scope?.node ?? WidgetsBinding.instance.focusManager.rootScope;
  }

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

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

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!_didAutofocus && config.autofocus) {
      FocusScope.of(context).setFirstFocus(config.node);
      _didAutofocus = true;
    }
  }

  @override
  void dispose() {
    config.node.detach();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).reparentScopeIfNeeded(config.node);
    return new Semantics(
      container: true,
      child: new _FocusScopeMarker(
        node: config.node,
        child: config.child,
      ),
    );
  }
}