Skip to content

[web] TextField duplicates KeyEvents (handles event, AND passes event on to parent) #70507

Closed

Description

@allComputableThings

Steps to Reproduce

Its seems that TextField passes all of its keys events to its parent widget, even if those events were handled by the textview as text input. This is very non-standard behavior in UI and causes input to TextFields to be also interpreted a second time as, say, short-cut keys to parent widgets. (This happens for web and Android, at least).

A cleaner design would be to:

  • not pass key events (generally!) to parents if the event is interpreted by a widget
  • supply a flag (bool eatInterprettedKeyEvent = true) to TextView to enable/disable the behavior to ensure back compatibility

Example: Enter text into the TextView, the key inputs are also received by the containing FocusScope. event2 is printed indicating that event handled by the textview (including arrow keys which affect scrolling) are unhelpfully passed to parent widgets.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
                child: Padding(padding: EdgeInsets.all(20), child: test()))));
  }
}

Widget test() {
  return FocusScope(
      onKey: (FocusNode node, RawKeyEvent event) {
        print("event1: $event");    /// In not called
        return true;
      },
      node: FocusScopeNode(),
      child: FocusScope(
          onKey: (FocusNode node, RawKeyEvent event) {
            print("event2: $event");   /// Should not be called
            return true;   /// Don't pass to parent
          },
          node: FocusScopeNode(),
          child: TextField(decoration: InputDecoration(hintText: "type..."))));
}

/home/sir/development/flutter/bin/flutter doctor --verbose
[✓] Flutter (Channel unknown, 1.22.0-12.2.pre, on Linux, locale en_US.UTF-8)
    • Flutter version 1.22.0-12.2.pre at /home/sir/development/flutter
    • Framework revision 2bafdc8226 (7 weeks ago), 2020-09-25 12:48:22 -0700
    • Engine revision f763b5b9b9
    • Dart version 2.10.0 (build 2.10.0-110.5.beta)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /home/sir/Android/Sdk
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /home/sir/soft/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Android Studio (version 4.0)
    • Android Studio at /home/sir/soft/android-studio
    • Flutter plugin version 49.0.2
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 10 (API 29) (emulator)
    • Web Server (web)                   • web-server    • web-javascript • Flutter Tools
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 86.0.4240.193

• No issues found!
Process finished with exit code 0

Activity

  1. pedromassangocode commented on Nov 16, 2020

    @pedromassangocode

    Hi @stuz5000
    Can you please upgrade to the latest stable and try again
    flutter channel stable
    flutter upgrade --force
    flutter doctor -v

    If the issue persists then please provide your flutter doctor -v.
    Thank you

  2. added
    in triagePresently being triaged by the triage team
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 16, 2020
  3. allComputableThings commented on Nov 16, 2020

    @allComputableThings
    Author

    Following your instructions, it works for Android. But still fails for web (where I have to switch to beta).

    Android:

    [✓] Flutter (Channel stable, 1.22.4, on Linux, locale en_US.UTF-8)
        • Flutter version 1.22.4 at /home/sir/development/flutter
        • Framework revision 1aafb3a8b9 (3 days ago), 2020-11-13 09:59:28 -0800
        • Engine revision 2c956a31c0
        • Dart version 2.10.4
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
        • Android SDK at /home/sir/Android/Sdk
        • Platform android-29, build-tools 29.0.2
        • ANDROID_HOME = /home/sir/Android/Sdk
        • Java binary at: /home/sir/soft/android-studio/jre/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
        • All Android licenses accepted.
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /home/sir/soft/android-studio
        • Flutter plugin version 49.0.2
        • Dart plugin version 193.7547
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    
    [✓] Connected device (1 available)
        • Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
    
    • No issues found!
    

    Web:

    %  flutter channel beta
    %  flutter upgrade
    %  flutter config --enable-web
    % flutter doctor -v
    
    [✓] Flutter (Channel beta, 1.23.0-18.1.pre, on Linux, locale en_US.UTF-8)
        • Flutter version 1.23.0-18.1.pre at /home/sir/development/flutter
        • Framework revision 198df796aa (5 weeks ago), 2020-10-15 12:04:33 -0700
        • Engine revision 1d12d82d9c
        • Dart version 2.11.0 (build 2.11.0-213.1.beta)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
        • Android SDK at /home/sir/Android/Sdk
        • Platform android-29, build-tools 29.0.2
        • ANDROID_HOME = /home/sir/Android/Sdk
        • Java binary at: /home/sir/soft/android-studio/jre/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
        • All Android licenses accepted.
    
    [✓] Chrome - develop for the web
        • Chrome at google-chrome
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /home/sir/soft/android-studio
        • Flutter plugin version 49.0.2
        • Dart plugin version 193.7547
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2020.2)
        • IntelliJ at /snap/intellij-idea-ultimate/253
        • Flutter plugin installed
        • Dart plugin version 202.7319.5
    
    [✓] Connected device (3 available)
        • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 10 (API 29) (emulator)
        • Web Server (web)                   • web-server    • web-javascript • Flutter Tools
        • Chrome (web)                       • chrome        • web-javascript • Google Chrome 86.0.4240.193
    
    • No issues found!
    
  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 16, 2020
  5. changed the title [-]TextView duplicates KeyEvents (handles event, AND passes event on to parent)[/-] [+][web] TextView duplicates KeyEvents (handles event, AND passes event on to parent)[/+] on Nov 16, 2020
  6. pedromassangocode commented on Nov 17, 2020

    @pedromassangocode

    Hi @stuz5000 isn't this the expected behavior?
    If you see the docs of FocusScope it says:

    Screen Shot 2020-11-17 at 11 04 39

    This means that it is not passing the event to its parent, but, the FocusScope widget is capturing the vents itself, does that makes sense to you?

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 17, 2020
  8. allComputableThings commented on Nov 18, 2020

    @allComputableThings
    Author

    The issue is not that FocusScope catches the event (and allows me to correctly block it from being passed to parents), its that key inputs to the TextField makes it to the FocusScope in the first place. I would think that if that someone types into a TextField and the TextField receives that input, updated its TextController and displays it, then the key event (by default) is handled and should not be passed to the parent (FocusScope or otherwise). If it is not so, the key input will be potentially interpreted again by parent widgets (e.g. generating spurious shortcuts key event for the current page, or scrolling a containing listview on cursor input in the TextView, etc...)

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 18, 2020
  10. pedromassangocode commented on Nov 18, 2020

    @pedromassangocode

    Hi @stuz5000
    Do you mean TextField instead of TextView in the issue's title? As far as I know there is no such widget in Flutter.

  11. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 18, 2020
  12. allComputableThings commented on Dec 1, 2020

    @allComputableThings
    Author

    Yes -- that is correct. TextField (as in the sample code I provided)

  13. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 1, 2020
  14. pedromassangocode commented on Dec 1, 2020

    @pedromassangocode

    Hi @stuz5000
    I just tested again and I cannot reproduce the issue on stable and beta channel.

    In the logs bellow, I pressed the following digits one at time sequentially (on Flutter Web): 235

    For a more detailed help message, press "h". To quit, press "q".
    event2: RawKeyDownEvent#2fa27(logicalKey: LogicalKeyboardKey#00032(keyId: "0x00000032", keyLabel: "2", debugName:
    "Digit 2"), physicalKey: PhysicalKeyboardKey#7001f(usbHidUsage: "0x0007001f", debugName: "Digit 2"))
    event2: RawKeyUpEvent#05183(logicalKey: LogicalKeyboardKey#00032(keyId: "0x00000032", keyLabel: "2", debugName: "Digit
    2"), physicalKey: PhysicalKeyboardKey#7001f(usbHidUsage: "0x0007001f", debugName: "Digit 2"))
    event2: RawKeyDownEvent#f5877(logicalKey: LogicalKeyboardKey#00033(keyId: "0x00000033", keyLabel: "3", debugName:
    "Digit 3"), physicalKey: PhysicalKeyboardKey#70020(usbHidUsage: "0x00070020", debugName: "Digit 3"))
    event2: RawKeyUpEvent#9f2f4(logicalKey: LogicalKeyboardKey#00033(keyId: "0x00000033", keyLabel: "3", debugName: "Digit
    3"), physicalKey: PhysicalKeyboardKey#70020(usbHidUsage: "0x00070020", debugName: "Digit 3"))
    event2: RawKeyDownEvent#9bb2c(logicalKey: LogicalKeyboardKey#00035(keyId: "0x00000035", keyLabel: "5", debugName:
    "Digit 5"), physicalKey: PhysicalKeyboardKey#70022(usbHidUsage: "0x00070022", debugName: "Digit 5"))
    event2: RawKeyUpEvent#d671d(logicalKey: LogicalKeyboardKey#00035(keyId: "0x00000035", keyLabel: "5", debugName: "Digit
    5"), physicalKey: PhysicalKeyboardKey#70022(usbHidUsage: "0x00070022", debugName: "Digit 5"))
    

    Note that only the vent2 is being printed.

    flutter doctor -v
    [✓] Flutter (Channel beta, 1.24.0-10.2.pre, on Mac OS X 10.15.7 19H2 darwin-x64, locale en)
        • Flutter version 1.24.0-10.2.pre at /Users/pedromassango/dev/SDKs/flutter_beta
        • Framework revision 022b333a08 (13 days ago), 2020-11-18 11:35:09 -0800
        • Engine revision 07c1eed46b
        • Dart version 2.12.0 (build 2.12.0-29.10.beta)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
        • Android SDK at /Users/pedromassango/Library/Android/sdk
        • Platform android-30, build-tools 30.0.2
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 12.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.1, Build version 12A7403
        • CocoaPods version 1.9.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    
    [✓] IntelliJ IDEA Community Edition (version 2020.2.3)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 51.0.3
        • Dart plugin version 202.8070
    
    [✓] VS Code (version 1.51.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.16.0
    
    [✓] Connected device (3 available)
        • Redmi 5 Plus (mobile) • 0258ff700005 • android-arm64  • Android 8.1.0 (API 27)
        • Web Server (web)      • web-server   • web-javascript • Flutter Tools
        • Chrome (web)          • chrome       • web-javascript • Google Chrome 86.0.4240.198
    
    • No issues found!
    
  15. 2 remaining items

  16. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 7, 2020
  17. added
    a: text inputEntering text in a text field or keyboard related problems
    f: focusFocus traversal, gaining or losing focus
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Dec 8, 2020
  18. changed the title [-][web] TextView duplicates KeyEvents (handles event, AND passes event on to parent)[/-] [+][web] TextField duplicates KeyEvents (handles event, AND passes event on to parent)[/+] on Dec 8, 2020
  19. added
    P2Important issues not at the top of the work list
    on Jun 22, 2022
  20. exaby73 commented on Feb 1, 2023

    @exaby73
    Member

    I cannot reproduce this issue anymore on the latest stable and master. Therefore, I am closing this issue as fixed. if you still reproduce this issue, please comment and I can reopen it. Thank you

  21. added
    r: fixedIssue is closed as already fixed in a newer version
    on Feb 1, 2023
  22. github-actions commented on Mar 3, 2023

    @github-actions

    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.

  23. locked as resolved and limited conversation to collaborators on Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsf: focusFocus traversal, gaining or losing focusfound in release: 1.24Found to occur in 1.24found in release: 1.25Found to occur in 1.25frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions