Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to remove keyboard focus from the entire tree #14409

Closed
ride4sun opened this issue Feb 1, 2018 · 2 comments
Closed

Ability to remove keyboard focus from the entire tree #14409

ride4sun opened this issue Feb 1, 2018 · 2 comments
Labels
c: new feature Nothing broken; request for a new capability f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Comments

@ride4sun
Copy link

ride4sun commented Feb 1, 2018

Steps to Reproduce

I would like to show the hintText of a textField until the user touches somewhere else.
The touchpoint could be deep in the widget tree and I dont know how to obtain the current focused node somewhere in the widget tree to call unfocused, to show the textField inactive and hide the keyboard. This might be just an issue with my understanding of flutter or the documentation which lacks example and does not explain how to use the patterns.

The code example shows the GestureDetector which is calling unfocus on the node. This is simple here because the focusNode is easily accessible in the same widget. The question here is how to do the same in a nested widget tree. What is the recommended pattern to do so?

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Stack(children: <Widget>[
          new GestureDetector(
              excludeFromSemantics: true,
              onTapDown: (_) {
                _focusNode.unfocus();
              }),
          new Column(children: <Widget>[
            new TextField(
                focusNode: _focusNode,
                decoration: new InputDecoration(
                    border: null,
                    hintText: _focusNode.hasFocus ? 'FOCUS' : 'no focus')),
            new RaisedButton(
                child: new Text('Remove Focus'),
                onPressed: () => _focusNode.unfocus())
          ])
        ]));
  }

  final FocusNode _focusNode = new FocusNode();

  @override
  void initState() {
    super.initState();
    _focusNode.addListener(() {
      setState(() {});
      print('Has focus: $_focusNode.hasFocus');
    });
  }

Flutter Doctor

[√] Flutter (on Microsoft Windows [Version 10.0.16299.192], locale en-US, channel dev)
    • Flutter version 0.0.21 at c:\sdks\flutter
    • Framework revision 2e449f06f0 (2 days ago), 2018-01-29 14:26:51 -0800
    • Engine revision 6921873c71
    • Tools Dart version 2.0.0-dev.16.0
    • Engine Dart version 2.0.0-edge.da1f52592ef73fe3afa485385cb995b9aec0181a

[√] Android toolchain - develop for Android devices (Android SDK 27.0.2)
    • Android SDK at C:\Users\ride4\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)

[√] Android Studio (version 3.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-915-b01)

[√] IntelliJ IDEA Community Edition (version 2017.2)
    • Flutter plugin version 19.1
    • Dart plugin version 172.4343.25

[√] Connected devices
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 7.1.1 (API 25) (emulator)
@Hixie Hixie changed the title How can I obtain the focused node somewhere in the widget tree to call unfocus etc. Ability to remove keyboard focus from the entire tree May 29, 2018
@Hixie Hixie added c: new feature Nothing broken; request for a new capability framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. customer: look labels May 29, 2018
@Hixie Hixie added this to the Goals milestone May 29, 2018
@zoechi
Copy link
Contributor

zoechi commented Jan 22, 2019

I think this boils down to

FocusScope.of(context).requestFocus(new FocusNode());

Closing as duplicate of #7247
perhaps also #20227

@zoechi zoechi closed this as completed Jan 22, 2019
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: new feature Nothing broken; request for a new capability f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

No branches or pull requests

3 participants