Skip to content

[web] onSubmitted of TextField invoked if another TextField is clicked #49785

Closed

Description

@a-marenkov

In browsers onSubmitted of TextField invoked on Enter and if another TextField is clicked.

Expected:

  • on Enter clicked - onSubmitted invocation;
  • on another TextField clicked - only focus change (without onSubmitted invocation).

Steps to Reproduce

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.all(24.0),
      child: Material(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            TextField(
              decoration: InputDecoration(hintText: 'TextField #1'),
              onSubmitted: (text) {
                print('TextField #1 onSubmitted: $text');
              },
            ),
            TextField(
              decoration: InputDecoration(hintText: 'TextField #2'),
              onSubmitted: (text) {
                print('TextField #2 onSubmitted: $text');
              },
            ),
          ],
        ),
      ),
    );
  }
}

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v1.13.6, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Community Edition (version 2019.2)
[✓] Connected device (3 available)

• No issues found!

Activity

  1. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    platform-webWeb applications specifically
    on Jan 30, 2020
  2. yjbanov commented on Jan 30, 2020

    @yjbanov
    Contributor

    @a-marenkov The current behavior was implemented this way on purpose. Can you explain why you'd like a different behavior? Is it preventing you from implementing something?

    /cc @nturgut

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 30, 2020
  4. kunit1 commented on Jan 31, 2020

    @kunit1

    I tested the supplied sample code and this is what I observed on master:

    1. Click inside of text-field to focus
      a. text-field is now focused (good)
    2. click outside of text-field on a blank space.
      a. text-field is now unfocused (good)
      b. onSubmitted fires automatically (?)

    I think the onSubmitted firing automatically makes sense to me.

  5. a-marenkov commented on Jan 31, 2020

    @a-marenkov
    Author

    @yjbanov @kunit1

    onSubmitted firing automatically makes sense to me as well and i would not have filed the issue, but:

    1. It has different behavior on mobile - onSubmitted is not firing automatically if you tap another TextField.
    2. It has different behavior on web if opened with mobile (tested on android in chrome app) - onSubmitted is not firing automatically if you tap another TextField.
    3. Docs aren't clear about when onEditingComplete and onSubmitted are called. It says Called when the user submits editable content (e.g., user presses the "done" button on the keyboard) - but what are the other ways to "submit editable"?

    @yjbanov Here are couple of cases in which this behavior is bad:

    1. I have 2 TextFields. One for email, the other for password.

    I want on enter pressed

    • in email move focus to password;
    • in password perform some request
      (obviously, there would be also some kind of a login button on a screen to perform the request, but that's another story)

    So i put this logic either onEditingComplete or onSubmitted

    -> user enters email
    -> preses enter and enters password
    -> then decides to correct email (user mistyped email or wants to enter different email)
    -> user clicks email TextField and here is the problem, it fires onSubmitted and thus the request

    1. I have 3 TextFields.

    I want on enter pressed move focus to next TextField, so i put this logic either onEditingComplete or onSubmitted.

    -> user enters text in first TextField
    -> preses enter and enters text in second TextField
    -> then decides to correct text in first TextField
    -> user clicks on first TextField and here is the problem - for half a second the third TextField gets focused and then first TextField gets focused (it looks buggy for user)

    Maybe, there's another way to implement cases above that i don't know.
    On mobile i don't face this problems with this implementation.

    P.S. I'm sorry, i think this issue should not really be about onSubmitted alone, but also about onEditingComplete (since it seems it's more appropriate to use for focus logic).

  6. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 31, 2020
  7. a-marenkov commented on Jan 31, 2020

    @a-marenkov
    Author

    Update In the second case above to reproduce the bug it might take more than one click on another TextField. Try switching between TextFields back and forth (requires at least 3 TextFields).

  8. a-marenkov commented on Feb 3, 2020

    @a-marenkov
    Author

    @yjbanov Another case would be:

    I have at least 2 TextFields in a ListView (somewhere at the end of it).

    I want on enter pressed move focus to next TextField, so i put this logic either onEditingComplete or onSubmitted.

    -> user scrolls downwards and enters text in first TextField
    -> then decides to scoll upwards by dragging
    -> drags the ListView, and here is the problem - ListView would scroll upwards by dragging and also would autoscroll downwards to second TextField (because it gets focused as user taps blanc space).
    It looks buggy.

  9. added this to the February 2020 milestone on Feb 6, 2020
  10. 20 remaining items

  11. nturgut commented on Jun 4, 2020

    @nturgut
    Contributor

    @jlubeck I understand the confusion. You are right the documentation is not descriptive and leads to confusion. We should also have the behavior described per platform if there is a difference between them.

    I'll discuss this with other member worked on text editing this week and update this issue (and the documentations).

  12. removed this from the milestone on Jun 6, 2020
  13. kunit1 commented on Jun 8, 2020

    @kunit1

    @nturgut It is fine that now the textfield will not submit its content when click on other parts of the page. But shouldn't the text-field lose focus just like any other standard web text-field? The change in this PR caused a regression in my opinion as now the text field will not lose focus when clicking away.

  14. NicolasDionB commented on Jun 15, 2020

    @NicolasDionB

    Maybe it's a noob question but: where can I find in which version of Flutter will this update be found? I know it's been merged but I have a hard time finding when it's available in Flutter :) Thanks!

  15. added
    P2Important issues not at the top of the work list
    and removed
    P1High-priority issues at the top of the work list
    on Jun 16, 2020
  16. nturgut commented on Jun 26, 2020

    @nturgut
    Contributor

    Sorry for the late reply. I checked with developers who frequently worked on text editing.

    The current API:
    (1) Both onEditingComplete and onSubmitted are called at the same time.
    (2) onSubmitted (which has the value) is indicates user the text editing is complete.
    (3) onEditingComplete is for overriding the default behavior of focus changes.

    PR(flutter-team-archive/engine#18743) is added to put web in sync with the current API documentation.

    As also suggested by @kunit1: If TextFields do not lose focus when other areas are clicked (mouse click or touch) by the user so web scenarios will be hard to implement. We need either the existing onEditingComplete callback to act differently than the onSubmit callback, and notify these blur event. Or we need to add a new callback to support development.

    I am working on a document discussing these options. I'll add a link here.

  17. NicolasDionB commented on Jun 28, 2020

    @NicolasDionB

    That's good to hear. Though the onSubmited beeing called when we click outside the editing field is kinda strange. We should at least receive a value saying what caused the "submission". In my opinion, changing focus out of the field is not a mark of submission but I understand someone might see it differently. For me, hitting the "enter" or "send" button would be submission, nothing else. In any case, if we can receive the cause of the said submission, that would be fine so just adding an argument to the handler would work for me.

  18. ironpotter commented on Jul 15, 2020

    @ironpotter

    Found some workaround, that emulates onSubmit action and losing focus doesn't trigger onSubmit.

    TextFormField(
            controller: controller,
            autovalidate: true,
            maxLines: null,
            validator: (value) {
    
              //detecting enter in input string
              if(value.contains('\n')){
                // removing enter from input string
                String strWithoutEnter = controller.text.substring(0,controller.text.length - 1);
                controller.text = strWithoutEnter;
    
                // moving cursor to the end of input string
                controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));
                _makeSomeLogic();
              }
            },
            textInputAction: TextInputAction.search,
            decoration: InputDecoration(
              hintText: description,
              border: InputBorder.none,
              focusedBorder: InputBorder.none,
            ),
          ),
    
  19. sayurikunugi commented on Jul 16, 2020

    @sayurikunugi

    @ironpotter
    Thank you, your solution works for me!
    Just 1 thing, if the user inputs enter in the middle of text, it's not working right, so, I changed

                String strWithoutEnter = controller.text.substring(0,controller.text.length - 1);
                controller.text = strWithoutEnter;
    

    to

                                final strWithoutEnter = searchController.text
                                    .replaceAll('\n', '');
    
  20. ironpotter commented on Jul 19, 2020

    @ironpotter

    @sayurikunugi Thank you. You're right.

  21. pedromassangocode commented on Sep 21, 2020

    @pedromassangocode

    Hi @a-marenkov
    I cannot reproduce this on latest master channel.

    sample code
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(
        MaterialApp(
          title: 'Flutter Demo',
          home: Scaffold(
            body: Padding(
              padding: EdgeInsets.all(50),
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.all(24.0),
                child: Material(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      TextField(
                        decoration: InputDecoration(hintText: 'TextField #1'),
                        onSubmitted: (text) {
                          print('TextField #1 onSubmitted: $text');
                        },
                      ),
                      TextField(
                        decoration: InputDecoration(hintText: 'TextField #2'),
                        onSubmitted: (text) {
                          print('TextField #2 onSubmitted: $text');
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      );
    }
    
    
    flutter doctor -v
    [✓] Flutter (Channel master, 1.22.0-10.0.pre.269, on Mac OS X 10.15.6 19G2021 x86_64, locale en)
        • Flutter version 1.22.0-10.0.pre.269 at /Users/pedromassango/dev/SDKs/flutter_master
        • Framework revision 51fa704642 (2 days ago), 2020-09-19 11:27:04 -0700
        • Engine revision 4b8477d115
        • Dart version 2.10.0 (build 2.10.0-142.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
        • Android SDK at /Users/pedromassango/Library/Android/sdk
        • Platform android-30, build-tools 30.0.1
        • 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-6222593)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 12.0)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.0, Build version 12A7209
        • CocoaPods version 1.9.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [!] Android Studio (version 4.0)
        • Android Studio at /Applications/Android Studio.app/Contents
        ✗ Flutter plugin not installed; this adds Flutter specific functionality.
        ✗ Dart plugin not installed; this adds Dart specific functionality.
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    
    [✓] IntelliJ IDEA Community Edition (version 2020.2.2)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 49.0.4
        • Dart plugin version 202.7319.5
    
    [✓] VS Code (version 1.48.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.14.1
    
    [✓] Connected device (4 available)
        • sdk gphone x86 arm (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
        • macOS (desktop)             • macos         • darwin-x64     • Mac OS X 10.15.6 19G2021 x86_64
        • Web Server (web)            • web-server    • web-javascript • Flutter Tools
        • Chrome (web)                • chrome        • web-javascript • Google Chrome 85.0.4183.102
    
    ! Doctor found issues in 1 category.
    Process finished with exit code 0
    

    Closing this as probably fixed. Could everyone who still has this problem please file a new issue with the exact description of what happens, logs, and the output of flutter doctor -v.

    All system setups can be slightly different, so it's always better to open new issues and reference related issues.

  22. github-actions commented on Aug 12, 2021

    @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 Aug 12, 2021
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 problemsframeworkflutter/packages/flutter repository. See also f: labels.platform-webWeb applications specifically

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions