Skip to content

Toggling enabled of Textfield notifies FocusNode listeners #126149

Closed

Description

@KammererTob

Is there an existing issue for this?

Steps to reproduce

  1. Attach a FocusNode to an input inside a Stateful Widget and listen to it
  2. Toggle/Rebuild the input with a different enabled state
  3. Listeners are notified, although the focus did not change

Expected results

I would not expect the listeners to be notified when the input state changed between disabled and enabled without any change in focus.

Actual results

Listeners of the FocusNode are notified although the focus did not change.

Code sample

https://dartpad.dev/?id=112cc5c8b9cf28fac38b4b37656aede5

Code
import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Container(child: MyWidget(), width: 300, height: 200)
        ),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  late final FocusNode markFocusNode;
  bool isLocked = false;

  @override
  void initState() {
    markFocusNode = FocusNode(debugLabel: "MarkFocusNode")
      ..addListener(_onChangeFocus);
    super.initState();
  }

  @override
  void dispose() {
    markFocusNode.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
          autofocus: false,
          enabled: !isLocked,
          focusNode: markFocusNode,
          onChanged: (val) => print('onChanged'),
          textInputAction: TextInputAction.next,
        ),
        ElevatedButton(child: Text('Toggle Lock'), onPressed: () => setState(() => isLocked = !isLocked))
      ]
    );
  }
  
    void _onChangeFocus() {
    if (!markFocusNode.hasFocus) {
      print('Lost focus');
    }
  }
}

Logs

No response

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.7.8, on Microsoft Windows [Version 10.0.19044.2846], locale de-DE)
    • Flutter version 3.7.8 on channel stable at C:\SDKs\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 90c64ed42b (6 weeks ago), 2023-03-21 11:27:08 -0500
    • Engine revision 9aa7816315
    • Dart version 2.19.5
    • DevTools version 2.20.1

Activity

  1. darshankawar commented on May 5, 2023

    @darshankawar
    Member

    Thanks for the detailed report @KammererTob
    Upon running your code sample using latest stable (3.7.12), I observed below:

    1. Tap on textfield to make it active. We see - D/InputConnectionAdaptor(26306): The input method toggled cursor monitoring on log.

    2. Tap on Toggle lock. We see -> Lost focus.

    3. Tap again on Toggle lock. We see -> Lost focus, but the textfield is now active.

    Is this the behavior you are reporting that even though it says lost focus, but textfield is active ? But expected behavior should be, the textfield shouldn't be active ?

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 5, 2023
  3. KammererTob commented on May 5, 2023

    @KammererTob
    ContributorAuthor

    @darshankawar Thanks for the quick response.

    No. If you don't tap on the textfield at all and only press the "Toggle lock" (so toggling between enabled and disabled) button it always calls the _onChangeFocus method (the listener of the attached FocusNode), although technically the Focus state of the input hasn't changed. We only switched between endabled and disabled states.

    EDIT: I updated the issue to better reflect that the issue is not actually with rebuilding the widget, but specifically with toggling the enabled flag of the TextField

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 5, 2023
  5. changed the title [-]Rebuild notifies FocusNode listeners[/-] [+]Toggling enabled of Textfield notifies FocusNode listeners[/+] on May 5, 2023
  6. darshankawar commented on May 5, 2023

    @darshankawar
    Member

    Thanks for the update and feedback. Upon directly tapping Toggle lock, I don't see onChanged being triggered or printed. I see Lost focus in the log as below:

    I/flutter (28083): Lost focus
    I/flutter (28083): Lost focus
    I/flutter (28083): Lost focus
    I/flutter (28083): Lost focus
    I/flutter (28083): Lost focus
    I/flutter (28083): Lost focus
    I/flutter (28083): Lost focus
    

    I am on latest stable (3.7.12).

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 5, 2023
  8. KammererTob commented on May 5, 2023

    @KammererTob
    ContributorAuthor

    "Lost focus" is the output of the _onChangeFocus method, which is the listener method attached to the focus node:

    markFocusNode = FocusNode(debugLabel: "MarkFocusNode")
          ..addListener(_onChangeFocus);
    
      void _onChangeFocus() {
        if (!markFocusNode.hasFocus) {
          print('Lost focus');
        }
      }
    
  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 5, 2023
  10. darshankawar commented on May 5, 2023

    @darshankawar
    Member

    I see, thanks.

    stable, master flutter doctor -v
    [!] Flutter (Channel stable, 3.7.12, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 3.7.12 on channel stable at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 4d9e56e694 (3 days ago), 2023-04-17 21:47:46 -0400
        • Engine revision 1a65d409c7
        • Dart version 2.19.6
        • DevTools version 2.20.1
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.11.0-1.0.pre.63, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 3.11.0-1.0.pre.63 on channel master at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 04e284a49d (27 minutes ago), 2023-05-05 00:31:05 -0400
        • Engine revision c97a0deccb
        • Dart version 3.1.0 (build 3.1.0-77.0.dev)
        • DevTools version 2.23.1
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at /Users/dhs/Library/Android/sdk
        ✗ cmdline-tools component is missing
          Run `path/to/sdkmanager --install "cmdline-tools;latest"`
          See https://developer.android.com/studio/command-line for more details.
        ✗ Android license status unknown.
          Run `flutter doctor --android-licenses` to accept the SDK licenses.
          See https://flutter.dev/docs/get-started/install/macos#android-setup for
          more details.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 13C100
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 65.1.4
        • Dart plugin version 213.7228
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.29.0
    
    [✓] Connected device (3 available)
        • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
          • iOS 15.3.1 19D52
        • macOS (desktop)           • macos                                    •
          darwin-x64     • macOS 12.2.1 21D62 darwin-x64
        • Chrome (web)              • chrome                                   •
          web-javascript • Google Chrome 109.0.5414.119
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
          
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
    
    
  11. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    f: focusFocus traversal, gaining or losing focus
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on May 5, 2023
  12. 8 remaining items

  13. moved this from To do to PR submitted in Nevercodeon May 9, 2023
  14. self-assigned this
    on May 9, 2023
  15. bleroux commented on May 9, 2023

    @bleroux
    Contributor

    @KammererTob Thanks for pointing out this sentence, you are right that it can be improved.
    I filed #126331 to add some details.

  16. KammererTob commented on May 9, 2023

    @KammererTob
    ContributorAuthor

    @bleroux Perfect. That makes it very clear. Thank you!

  17. moved this from PR submitted to Done (PR merged) in Nevercodeon May 9, 2023
  18. added
    r: fixedIssue is closed as already fixed in a newer version
    on May 9, 2023
  19. github-actions commented on May 23, 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.

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

Metadata

Metadata

Assignees

Labels

a: text inputEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/f: focusFocus traversal, gaining or losing focusfound in release: 3.11Found to occur in 3.11found in release: 3.7Found to occur in 3.7frameworkflutter/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

  • Status
    Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions