getx icon indicating copy to clipboard operation
getx copied to clipboard

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

Open VolkanSecin opened this issue 1 year ago • 3 comments

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 avatar May 30 '24 19:05 VolkanSecin

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

VolkanSecin avatar May 30 '24 20:05 VolkanSecin

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

Gaurav-CareMonitor avatar Jun 03 '24 09:06 Gaurav-CareMonitor

@Gaurav-CareMonitor see https://github.com/flutter/flutter/issues/148867

VolkanSecin avatar Jun 03 '24 11:06 VolkanSecin