Repository navigation
Toggling enabled of Textfield notifies FocusNode listeners #126149
Copy link
Copy link
Closed
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/f: focusFocus traversal, gaining or losing focusFocus traversal, gaining or losing focusfound in release: 3.11Found to occur in 3.11Found to occur in 3.11found in release: 3.7Found to occur in 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
Description
Is there an existing issue for this?
- I have searched the existing issues
- I have read the guide to filing a bug
Steps to reproduce
- Attach a FocusNode to an input inside a Stateful Widget and listen to it
- Toggle/Rebuild the input with a different
enabledstate - 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.1Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsd: api docsIssues with https://api.flutter.dev/Issues with https://api.flutter.dev/f: focusFocus traversal, gaining or losing focusFocus traversal, gaining or losing focusfound in release: 3.11Found to occur in 3.11Found to occur in 3.11found in release: 3.7Found to occur in 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
Type
Projects
- StatusShow more project fieldsDone (PR merged)
Activity
darshankawar commentedon May 5, 2023
Thanks for the detailed report @KammererTob
Upon running your code sample using latest stable (3.7.12), I observed below:
Tap on textfield to make it active. We see -
D/InputConnectionAdaptor(26306): The input method toggled cursor monitoring onlog.Tap on
Toggle lock. We see ->Lost focus.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 ?KammererTob commentedon May 5, 2023
@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
_onChangeFocusmethod (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
enabledflag of theTextField[-]Rebuild notifies FocusNode listeners[/-][+]Toggling enabled of Textfield notifies FocusNode listeners[/+]darshankawar commentedon May 5, 2023
Thanks for the update and feedback. Upon directly tapping
Toggle lock, I don't seeonChangedbeing triggered or printed. I seeLost focusin the log as below:I am on latest stable (3.7.12).
KammererTob commentedon May 5, 2023
"Lost focus" is the output of the
_onChangeFocusmethod, which is the listener method attached to the focus node:darshankawar commentedon May 5, 2023
I see, thanks.
stable, master flutter doctor -v
8 remaining items
bleroux commentedon May 9, 2023
@KammererTob Thanks for pointing out this sentence, you are right that it can be improved.
I filed #126331 to add some details.
KammererTob commentedon May 9, 2023
@bleroux Perfect. That makes it very clear. Thank you!
Update FocusNode documentation (#126331)
github-actions commentedon May 23, 2023
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 -vand a minimal reproduction of the issue.