繁体   English   中英

Flutter - 选择 TextFormField 时键盘不显示

[英]Flutter - Keyboard not showing when TextFormField is selected

我目前遇到的问题是,当我 select Form小部件内的任何TextFormField小部件时,键盘没有出现。 这是表单的代码,它位于我的CreateAccountForm Stateful 小部件中。

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sign_up_page/constants.dart';

class CreateAccountForm extends StatefulWidget {
  @override
  _CreateAccountFormState createState() => _CreateAccountFormState();
}

class _CreateAccountFormState extends State<CreateAccountForm> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          CustomTextFormField(
            labelText: "Email",
            keyboardType: TextInputType.emailAddress,
          ),
          Spacer(),
          CustomTextFormField(labelText: "Full name"),
          Spacer(),
          CustomTextFormField(
            labelText: "Password",
            isPassword: true,
          ),
          Spacer(),
          RaisedButton(
            onPressed: () {
              print("Get started button pressed");
            },
            padding: EdgeInsets.all(20.0),
            color: blueMaterialColor.shade100,
            shape: defaultRectangularButtonShape,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  "Get started",
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
                Icon(
                  Icons.arrow_forward,
                  color: Colors.white,
                ),
              ],
            ),
          ),
          Spacer(),
        ],
      ),
    );
  }
}

class CustomTextFormField extends StatefulWidget {
  CustomTextFormField({
    @required this.labelText,
    this.isPassword = false,
    this.keyboardType = TextInputType.text,
  });

  final String labelText;
  final bool isPassword;
  final TextInputType keyboardType;

  @override
  _CustomTextFormFieldState createState() => _CustomTextFormFieldState();
}

class _CustomTextFormFieldState extends State<CustomTextFormField> {
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      decoration: InputDecoration(
          labelText: widget.labelText, labelStyle: inputLabelStyleUnselected),
      style: inputTextStyle,
      obscureText: widget.isPassword,
      keyboardType: widget.keyboardType,
    );
  }
}

这是一个屏幕截图,它显示了 email TextFormWidget内部的 cursor ,但没有显示键盘:

在 iPhone 11 上运行的应用程序的屏幕截图。即使选择了 TextFormWidget,键盘也不显示。

您可以在我的 Github 分支上查看该项目的所有代码: https://github.com/Jordan-Gillard/sign_up_page/tree/bug/fixing_keyboard_not_showing

如果您在Simulator上运行,您的Simulator肯定启用了Connect Hardware Keyboard 您可以通过禁用该功能来解决此问题。

我提供了图片来指导如何在下面执行此操作:

  1. Select 您的模拟器,然后单击Hardware选项卡。
  2. Hardware选项卡上,select 是Keyboard选项。
  3. 取消选中Connect Keyboard Hardware选项以启用在Simulator上切换实际键盘

检查下面提供的图像以获得更多视觉描述:)

在此处输入图像描述

在此处输入图像描述

如果您使用 ios 模拟器,菜单选项卡是 I/O。 在此处输入图像描述

就我而言,我在 main.dart 中调用了enableFlutterDriverExtension() 删除这个解决了这个问题。

解决方案很简单

  1. 将您的文本字段和按钮包装在单独的列小部件中
  2. 然后用 Form() 包装该列并给出 formKey

例如。

 Form(
            key: _formKey,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 21),
                  child: TextFormField(
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return '';
                      }
                      return null;
                    },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM