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
Focus on remain stable after retrun back from child widget to parent widget #99943
Comments
This issue was originally filed here #99903 and has been closed for the reasons specified #99903 (comment), In case you think this is a bug in the framework please provide more info about the bug in the original issue and we will reopen it. |
@abdulrehmananwar |
please share a piece of code. cause its not working . when i close child screen why no one widget focused??? |
@abdulrehmananwar, You have autofocus enabled in TextField, It is expected that the Textfield should gain the focus when it comes into view. But as per the current implementation, On popping the route widgets with However, the linked issue is about regaining focus without opening a keyboard, changing scroll position. I tried to achieve that with the help of the below code sample but it opens up the keyboard. code sampleimport 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
navigatorObservers: [routeObserver],
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final FocusNode focusNode = FocusNode();
@override
void initState() {
// TODO: implement initState
super.initState();
focusNode.addListener(() {
if (!focusNode.hasFocus) {
push(SecondScreen(
onPop: () {
focusNode.requestFocus();
},
));
}
});
}
void push(Widget child) {
Navigator.push(context, MaterialPageRoute(builder: (context) => child));
}
@override
void dispose() {
focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
autofocus: true,
focusNode: focusNode,
)
],
),
)),
);
}
}
final RouteObserver<ModalRoute<void>> routeObserver =
RouteObserver<ModalRoute<void>>();
class SecondScreen extends StatefulWidget {
final Function? onPop;
const SecondScreen({Key? key, this.onPop}) : super(key: key);
@override
State<SecondScreen> createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> with RouteAware {
@override
void didChangeDependencies() {
super.didChangeDependencies();
routeObserver.subscribe(this, ModalRoute.of(context)!);
}
@override
void dispose() {
routeObserver.unsubscribe(this);
super.dispose();
}
@override
void didPop() {
widget.onPop!();
super.didPop();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: const Center(
child: Text('Second Screen'),
),
);
}
}
|
this is perfect for single textfield. butt i have multiple textfields using List on parent form. how to manage them with focused node. on each text field on parent form i want the same function |
do i need to create a List for my multiple textfields?? |
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 |
i am using text widget on parent window and on tap i navigate to child window . when child window close i want to see the focus in the same textfield of parent window.
Use case
Proposal
The text was updated successfully, but these errors were encountered: