Skip to content

A way to distinguish TextEditingController event types. #79982

Open

Description

@dpedrinha

Use case

When using the TextEditingController.addListener(myListener) I get events from text changes but also from focus changes.
If I want to change my state, let's say showing an alert at some point when the user is typing, myListener is called 2 more times.
Meaning that I need to keep track of the previous text and check every time if the text did actually change or if it was just a focus event.
Same happens when the TextField get focused or unfocused. I get calls to myListener.

Proposal

Having two listener types for TextEditingController. One for focus events and another one just for text changes.

Activity

  1. added
    in triagePresently being triaged by the triage team
    c: proposalA detailed proposal for a change to Flutter
    and removed
    c: proposalA detailed proposal for a change to Flutter
    on Apr 8, 2021
  2. pedromassangocode commented on Apr 8, 2021

    @pedromassangocode

    Related to #64143 and #79982.

    @justinmc I'm wondering whether this this is should be considered as an issue rather than a proposal to distinguish event types. From my understanding controller is expected to be notified when the text changes only and not on focus change (in the user perspective).

    I'm considering this a bug for now, feel free to update the labels as required.

    Cc @LongCatIsLooong @chunhtai

    The code bellow prints every time the text field gains/loses focus.

    reproducible code sample
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      late final TextEditingController _controller = TextEditingController()..addListener(() {
        print(_controller.text);
      });
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          home: Scaffold(
            body: Center(
              child: TextField(controller: _controller),
            ),
          ),
        );
      }
    }
    
  3. added
    a: text inputEntering text in a text field or keyboard related problems
    p: material_uimaterial_ui package in flutter/packages
    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 Apr 8, 2021
  4. dpedrinha commented on Apr 8, 2021

    @dpedrinha
    Author

    I believe that having two events is better than just removing the focus event because it would give us more control over the textfield. But I'm used to event driven platforms, and flutter isn't that event driven, so the dev team will know what's best.

  5. chunhtai commented on Apr 8, 2021

    @chunhtai
    Contributor

    I think one of the event is triggered by selection changes? The text editing value includes both selection and text (as well as composing). When a textfield gets focused, the selection changes from TextSelection.collapsed(offset: -1) to TextSelection.collapsed(offset: text.length)

  6. justinmc commented on Apr 8, 2021

    @justinmc
    Contributor

    Yeah I think the behavior of calling the listener for both selection and text changes is working as intended, so this is a proposal and not a bug. TextEditingValue includes both selection and text as @chunhtai said, and since TextEditingController is just a ValueNotifier, it will call the listener whenever the TextEditingValue changes at all.

    The workaround mentioned in the description above: https://dartpad.dartlang.org/eec5ab37140ce52c8eb530b861960907

    I wonder if it would be possible to write a subclass of TextEditingController that has addTextListener and addSelectionListener?

  7. chunhtai commented on Apr 8, 2021

    @chunhtai
    Contributor

    Just a FYI, there is a onChange parameter for textfield that is specifically for text changes.

  8. LongCatIsLooong commented on Apr 9, 2021

    @LongCatIsLooong
    Contributor

    IIRC when EditableText gets unfocused it sets the selection to (-1, -1). It's probably not needed anymore. Let me see if I can remove it.

  9. 17 remaining items

  10. flutter-triage-bot commented on Sep 17, 2023

    @flutter-triage-bot

    This issue is missing a priority label. Please set a priority label when adding the triaged-framework label.

  11. added
    P2Important issues not at the top of the work list
    on Sep 19, 2023
  12. added
    packageflutter/packages repository. See also p: labels.
    and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 17, 2026
  13. added
    team-designOwned by Design Languages team
    triaged-designTriaged by Design Languages team
    and removed on Aug 26, 2026
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 problemsfound in release: 2.1Found to occur in 2.1has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.team-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions