Keyboard closes immediately after opening in CustomTextField

82 views Asked by At

I'm facing a persistent issue when using Flutter in my app. When clicking on a CustomTextField, the keyboard appears, but immediately closes, making typing impossible. I've tried several solutions, including the following:

  1. Dependency updates:

    • Checked and updated all dependencies in the pubspec.yaml file by running flutter pub get to ensure all dependencies are up to date.
  2. Cache clearing:

    • I cleared the Flutter cache using flutter clean, flutter pub get and rebuilt the application with flutter run.
  3. Emulator/device configuration:

    • I made sure the emulator settings were correct.
  4. Test on another emulator: I tested on another emulator and the error persisted.

Code related to CustomTextField:

Form(
 key: GlobalKey<FormState>(),
 child: Column(
   children: [
     CustomTextField(
       label: 'E-mail',
       prefixIcon: Icons.email,
       keyboardType: TextInputType.emailAddress,
       controller: controller.emailController,
       validator: controller.validateEmail,
     ),
     CustomTextField(
       label: 'Senha',
       prefixIcon: Icons.password,
       isPassword: true,
       controller: controller.passwordController,
       validator: controller.validatePassword,
       keyboardType: TextInputType.text,
     ),
   ],
 ),
),

Controller code:

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

class SigninController extends GetxController {
  final RxString snackBarMessage = ''.obs;

  late TextEditingController emailController;
  late TextEditingController passwordController;

  @override
  void onInit() {
    super.onInit();
    emailController = TextEditingController();
    passwordController = TextEditingController();
  }

  @override
  void onClose() {
    emailController.dispose();
    passwordController.dispose();
    super.onClose();
  }

  String?  validateEmail(String? value) {
    if (value == null || value.isEmpty) {
      return 'Required field';
    } else if (!RegExp(r'^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7 }$')
        .hasMatch(value)) {
      return 'Invalid email';
    }
    return null;
  }

  String?  validatePassword(String? value) {
    if (value == null || value.isEmpty) {
      return 'Required field';
    } else if (value.length < 6) {
      return 'The password must be at least 6 characters long';
    }
    return null;
  }
}

Other observations:

  • The problem persists even with a basic TextField and without customizations.
  • The Debug Console log shows the message showSoftInput() when the problem occurs.
1

There are 1 answers

0
Anderson André On

Solution:

Remove the key associated with the form widget because it was causing unexpected behavior where the keyboard closed automatically after opening. Apparently, the presence of the key was interfering with Flutter's default keyboard behavior.

Form(
  // key: GlobalKey<FormState>(),  // <- Removed key to fix the issue
  child: Column(
    children: [
      // ... other widgets
      CustomTextField(
        label: 'E-mail',
        keyboardType: TextInputType.emailAddress,
        controller: controller.emailController,
        validator: controller.validateEmail,
      ),
      // ... other widgets
    ],
  ),
)