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
When popping a route, controls should reacquire focus without opening the keyboard or scrolling to make visible. #48464
Comments
This is "as designed", since otherwise we would be losing the previously focused widget when we return to a route. If you want it to lose the focus so that this doesn't happen, then just unfocus the currently focused widget before you push the route: class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(
title: Text("First Page"),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
TextField(),
Text("Use back button to dismiss keyboard"),
Text("Scroll down to see next button"),
SizedBox(
height: height + 100,
child: Center(child: Text("Some widgets")),
),
RaisedButton(
child: Text("Click Here"),
onPressed: () {
// Tells any currently focused widget to unfocus, so that it won't scroll when
// returning to this page.
FocusManager.instance.primaryFocus.unfocus();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
);
},
)
],
),
),
);
}
} |
@gspencergoog Please run the code on device or emulator where the in-build keyboard(virtual keyboard) is enabled. By default keyboard is disable in IOS Simulator.. I am getting this issue in Android Device and Emulator. I am not sure about IOS. And yes we need to get previously focused widget when we return to a route. Showing cursor in the previously focused On going to next page, I am dismissing keyboard by pressing back button(the cursor is still blinking on If it is still "as designed", |
The scroll state is persisted, but when you go back, it restores the focus to the text field, which causes it to scroll into view. Notice that if you unfocus the text field before pushing (as in my example), then when you go back, the scroll state is unchanged. If I understand you correctly, what you want is (only when popping a route) focus the text field without bringing up a keyboard, and without scrolling it into view. Can you give a concrete example of an Android app doing this? I mean, it's probably possible to find, but I've tried a few apps now, and none of the do what you describe: they all either bring up the keyboard again when I go back to the page that had a text field focused, even if I manually put away the keyboard, or the control is unfocused when I go back to them. See the gif below: |
@gspencergoog Thank you.
I am unable to screen record in Netflix android app. You can go to sign in page, then enter username and then password and then navigate to Help page and then navigate back. you can see password field still has focus, but the keyboard not popping. I am not requesting new feature. It worked fine on older app that build with older Flutter SDK but in latest Flutter SDK(Flutter version 1.12.13+hotfix.5) looks kind of buggy. |
Demo Native Project. This is a simple native android project with two page with |
OK, we'll see what we can do to figure out a way to do this. This may be architecturally difficult with Flutter, so it may take a while, but we'll get to it. Thanks for your examples. |
FYI the cause of the regression is enabling focus upon popping a route. Before that change, popping a route didn't automatically cause controls to re-acquire the focus at all. |
I have also started experiencing this issue after using v1.9.1 for a long time and now update to 1.12. MaterialApp(
...
navigatorObservers: [ClearFocusOnPush()],
...
)
class ClearFocusOnPush extends NavigatorObserver {
@override
void didPush(Route route, Route previousRoute) {
super.didPush(route, previousRoute);
final focus = FocusManager.instance.primaryFocus;
focus?.unfocus();
}
} I would also like to see a fix for this issue implemented at some point |
@davidmartos96 Your solution is good to solve the annoying keyboard popup when popping a route. But the previous focus will be lost. This is also an unexpected behavior. I only don't want the keyboard popup, while still persisting previous focus. |
This will solve the problem. But not sure this an efficient way to do so. |
faced with the same problem. waiting for fix |
This doesn't to be code sampleimport 'package:flutter/material.dart';
void main() => runApp(MainPage());
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(
title: Text("First Page"),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
TextField(),
Text("Use back button to dismiss keyboar"),
Text("Scroll down to see next button"),
SizedBox(
height: height + 100,
child: Center(child: Text("Some widgets")),
),
RaisedButton(
child: Text("Click Here"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
);
},
)
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Page"),
),
body: Column(
children: <Widget>[
TextField(),
RaisedButton(
child: Text("Go Back"),
onPressed: () {
Navigator.pop(context);
},
)
],
),
);
}
}
flutter doctor -v[✓] Flutter (Channel master, 1.22.0-10.0.pre.358, on Mac OS X 10.15.6 19G2021
x86_64, locale en-GB)
• Flutter version 1.22.0-10.0.pre.358 at
/Users/tahatesser/Code/flutter_master
• Framework revision 4898a42b0a (2 hours ago), 2020-09-25 05:42:04 -0400
• Engine revision 62b5a53b10
• Dart version 2.11.0 (build 2.11.0-161.0.dev)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Users/tahatesser/Code/sdk
• Platform android-30, build-tools 30.0.2
• ANDROID_HOME = /Users/tahatesser/Code/sdk
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.0.1, Build version 12A7300
• CocoaPods version 1.9.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 49.0.2
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build
1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.49.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.14.1
[✓] Connected device (4 available)
• RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29)
• macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.6
19G2021 x86_64
• Web Server (web) • web-server • web-javascript • Flutter Tools
• Chrome (web) • chrome • web-javascript • Google Chrome
85.0.4183.121
• No issues found!
|
Hi, same issue on version 1.22.5. also waiting for fix |
Thanks @davidmartos96, worked perfectly. |
Hi |
My page has a lot of operations to use pushPage to the next page, and I expect that if the input field is focused when the pushPage to a new page comes back when the keyboard won't be focused if i use the code
when I back page, the keyboard can not be used How to deal with it better |
Same question |
This comment was marked as duplicate.
This comment was marked as duplicate.
I am also facing the same issue. |
Same issue, auto focus cannot working for this case :((( |
This comment was marked as spam.
This comment was marked as spam.
@davidmartos96 great solution, thank you |
This comment was marked as duplicate.
This comment was marked as duplicate.
Reproducible on the latest Code Sample
import 'package:flutter/material.dart';
void main() => runApp(MainPage());
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstPage(),
);
}
}
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(
title: Text("First Page"),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
TextField(),
Text("Use back button to dismiss keyboar"),
Text("Scroll down to see next button"),
SizedBox(
height: height + 100,
child: Center(child: Text("Some widgets")),
),
TextButton(
child: Text("Click Here"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
),
);
},
)
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Page"),
),
body: Column(
children: <Widget>[
TextField(),
TextButton(
child: Text("Go Back"),
onPressed: () {
Navigator.pop(context);
},
)
],
),
);
}
}
Screen Record
3.webmstable, master flutter doctor -v
|
I have number of pages and all of them are kind of forms with some
TextFields
.In each page, I am entering some data in
TextField
and then moving to the next page either by clicking next button directly (Key Board is Visible) or by clicking back button to dismiss keyboard then clicking next button. After the next page, When I trying to go to previous page by back button orNavigator.pop
thatTextField
getting auto focused. This behaviour is really annoying lot.I verified that in my older version app (Which uses older Flutter SDK) works fine.
Sample code to reproduce:
Flutter Doctor:
The text was updated successfully, but these errors were encountered: