skip to Main Content

Steps to reproduce

1.Build an app with the code below.

2.Open the app (Tested with Samsung A33)

3.See that the keyboard is not showing, also when clicking the button "Show Keyboard"

4.Focus on the text field manually – Now the keyboard will display.

5.Hide the keyboard

6.Press the button "Show Keyboard" again, and you will see that this time the keyboard is displayed

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class BestHome extends StatefulWidget {
const BestHome({super.key});

@OverRide
// ignore: library_private_types_in_public_api
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
late FocusNode _focusNode;

void _showKeyboard() {
FocusScope.of(context).requestFocus(_focusNode);
}

@OverRide
void initState() {
super.initState();
focusNode = FocusNode();
SchedulerBinding.instance.addPostFrameCallback(() {
Timer(const Duration(milliseconds: 1000), () {
_focusNode.requestFocus();
});
});
}

@OverRide
void dispose() {
_focusNode.dispose();
super.dispose();
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
autofocus: true,
focusNode: _focusNode,
decoration: const InputDecoration(
hintText: 'Start typing...',
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _showKeyboard,
child: const Text('Show Keyboard'),
),
],
),
),
);
}
}

2

Answers


  1. Chosen as BEST ANSWER

    The flutter team has confirmed that as of November 14, 2024, This is a known issue with Flutter (Reported mainly with Samasung)

    https://github.com/flutter/flutter/issues/122994


  2. The most likely reason is that the Textfield already have focus when you call _focusNode.requestFocus();. In this case requesting the focus again will not cause the focus to change, and will not make the keyboard visible.

    The easiest solution is it to manually open keyboard

      void _showKeyboard() {
        _focusNode.requestFocus();
        SystemChannels.textInput.invokeMethod('TextInput.show');
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search