-
Notifications
You must be signed in to change notification settings - Fork 28.6k
ScaffoldState.openDrawer brings the keyboard up #54277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @agordeev or edit this
I can't reproduce with it |
Hi @agordeev |
Yes, it's still reproducible on 1.17.0-3.3.pre. It's not reproducible with the code snippet above though. |
@agordeev |
@VladyslavBondarenko this issue is not my priority at this moment, so I'm going to close it for now. |
@agordeev I am facing similar issue. |
The same issue happened to me. |
Reproduces with current stable Code sample from #54277 (comment) |
I bisected this and traced the cause to this PR, which makes sense. I'll try and track down the exact cause of the regression. |
Actually, it looks like the example code has a bug in it, and the PR (which corrected some buggy behavior in focus handling) has tickled that bug: The corrected code should look like this: import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
final FocusScopeNode currentScope = FocusScope.of(context);
if (!currentScope.hasPrimaryFocus && currentScope.hasFocus) {
FocusManager.instance.primaryFocus.unfocus();
}
},
child: Scaffold(
appBar: AppBar(title: const Text('title')),
body: const Center(child: TextField()),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
const DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: const Text('Item 1'),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: const Text('Item 2'),
onTap: () {
Navigator.pop(context);
},
),
],
),
),
),
);
}
} Where instead of: onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
}, The code should be: onTap: () {
final FocusScopeNode currentScope = FocusScope.of(context);
if (!currentScope.hasPrimaryFocus && currentScope.hasFocus) {
FocusManager.instance.primaryFocus.unfocus();
}
}, The original version gets the scope of the current context (which is the modal scope for the navigator's overlay), and then tells the scope to unfocus itself only if it does not have primary focus. Which, if the text field doesn't have focus, the scope does have focus, so it doesn't call unfocus at all then. If the text field does have focus, then the code does call unfocus on the scope, which hands the focus to its parent scope, which is the navigator scope. Then, when the navigator pushes the drawer route, it tries to focus its own scope, but since we've just focused the navigator's scope, and the "first focus" of that scope is still the child scope, and its first focus is the editable text, the editable text gets focus again, raising the keyboard. My recommendation above will instead only unfocus the primary focus if it is on a descendant of the current scope, but won't unfocus the scope itself, so it doesn't hand focus to the navigator's scope. The bug in the example is that it calls |
I'm going to close this issue, since it appears that #54277 (comment) explains how to use correctly use the unfocus API and avoid the problem. |
This issue still happens
I Tried adding the code from #54277 (comment) before opening the Drawer but it doesnt work @HansMuller any advice? |
@basilmariano, as @gspencergoog explained, this worked for me:
|
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 |
As soon as I press Drawer button, it shows me keyboard back.
Please see this gif that demonstrates the issue
My code is quite simple:
flutter doctor -v
The text was updated successfully, but these errors were encountered: