Skip to content

Toggling enabled of Textfield notifies FocusNode listeners #126149

@KammererTob

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

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