Skip to content
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

Closed
abdulrehmananwar opened this issue Mar 11, 2022 · 7 comments
Labels
r: invalid Issue is closed as not valid

Comments

@abdulrehmananwar
Copy link

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

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Mar 11, 2022
@maheshmnj
Copy link
Member

maheshmnj commented Mar 11, 2022

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.

@darshankawar
Copy link
Member

@abdulrehmananwar
Looking at the description you provided, it may already have an existing solution for which you can ask on support channels like StackOverflow, so I am closing for now from here. Take a look at this documentation and see if it helps : https://docs.flutter.dev/cookbook/forms/focus

@darshankawar darshankawar added r: invalid Issue is closed as not valid and removed in triage Presently being triaged by the triage team labels Mar 11, 2022
@abdulrehmananwar
Copy link
Author

abdulrehmananwar commented Mar 11, 2022

please share a piece of code. cause its not working . when i close child screen why no one widget focused???
if there is any event (on parent screen) occur when i close child window there i can set focus please let me know?

@maheshmnj
Copy link
Member

maheshmnj commented Mar 11, 2022

@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 AutoFocus: true do not gain focus. This issue is being tracked here #48464

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 sample
import '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'),
      ),
    );
  }
}

@abdulrehmananwar
Copy link
Author

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

@abdulrehmananwar
Copy link
Author

do i need to create a List for my multiple textfields??
as i have created List.
if yes please let me know thanks

@github-actions
Copy link

github-actions bot commented Apr 5, 2022

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 -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

3 participants