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

Keyboard not appearing after swipe-back gesture when returning to a screen with TextField using GetX #3116

Open
VolkanSecin opened this issue May 30, 2024 · 3 comments
Assignees

Comments

@VolkanSecin
Copy link

I'm developing a Flutter application using GetX for state management, and I'm facing an issue where the keyboard does not appear when returning to a screen with a TextField after navigating away and then using the swipe-back gesture to return.

Steps to Reproduce: Open a screen with a TextField (e.g., a chat screen). Tap on the TextField to ensure the keyboard appears. Navigate to another screen (e.g., a profile screen) by tapping an element such as a profile image. Use the swipe-back gesture to navigate back to the previous screen (e.g., the chat screen). Tap on the TextField again. Expected Behavior: The keyboard should appear when tapping on the TextField after returning to the screen.

Actual Behavior: The keyboard does not appear when tapping on the TextField after using the swipe-back gesture to return to the screen.

Code Example: Here is a simplified version of the chat and profile screen implementation using GetX:

main.dart:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'chat_screen.dart';
import 'profile_screen.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      home: ChatScreen(),
    );
  }
}

chat_screen.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'profile_screen.dart';

class ChatScreen extends StatefulWidget {
  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> with WidgetsBindingObserver {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      _focusNode.requestFocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Chat"),
        actions: [
          IconButton(
            icon: Icon(Icons.account_circle),
            onPressed: () {
              Get.to(() => ProfileScreen());
            },
          ),
        ],
      ),
      body: Center(
        child: TextField(
          controller: _controller,
          focusNode: _focusNode,
          decoration: InputDecoration(
            hintText: "Type a message",
          ),
        ),
      ),
    );
  }
}

This issue occurs consistently when using the swipe-back gesture to navigate back to any screen with a TextField. The keyboard functions normally if I use the back button instead of the swipe-back gesture. I'm using GetX with GetMaterialApp.

How can I ensure the keyboard reliably appears for input fields after navigating back to a screen using the swipe-back gesture with GetX?

What I've Tried:

Using WidgetsBindingObserver to listen to app lifecycle events and refocus the FocusNode.
Using didChangeDependencies to refocus the FocusNode when dependencies change.
Manually calling FocusScope.of(context).requestFocus(_focusNode) in various lifecycle methods.

@VolkanSecin
Copy link
Author

Downgraded to flutter v3.19.6 and it works now :)

@Gaurav-CareMonitor
Copy link

Hi @VolkanSecin , We are also facing the same issue, did you find any workaround for this? other than downgrading

@VolkanSecin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants