Flutter中使用表单时键盘会自动关闭。

huangapple go评论126阅读模式
英文:

Keyboard automatically closes in flutter while using form

问题

在我的代码中,当使用text formfield时,我的键盘会自动关闭,我发现当我使用Form和它的验证键时,键盘会关闭,我正在使用带有表单的列表视图,但当我删除表单时,它可以正常工作,但我需要一个表单来验证,我该怎么办?

Form(
  key: _formKeyPlant,
  child: ListView(children: [
    SizedBox(
      width: size.width,
      child: Column(
        children: [
          CustomTextField(
            controller: name,
            title: 'Name',
            validator: (text) {
              if (text == null || text.isEmpty) {
                return 'Required';
              } else if (text.contains(RegExp(r'[0-9]')) ||
                  text.contains(RegExp(r'[#?!@$%^&*-]'))) {
                return 'Invalid Input';
              }
              return null;
            },
          ),
          15.verticalSpace,
          CustomTextField(
            controller: location,
            title: 'Location',
            validator: (text) {
              if (text == null || text.isEmpty) {
                return 'Required';
              } else if (text.contains(RegExp(r'[0-9]')) ||
                  text.contains(RegExp(r'[#?!@$%^&*-]'))) {
                return 'Invalid Input';
              }
              return null;
            },
          ),
          15.verticalSpace,
          CustomTextField(
            enabled: false,
            onTap: () async {
              final response = await showAppBottomSheet(
                context,
                const TreesInfoBottomSheet(),
              );
              if (response != null) {
                treeType.text = response['name'];
                treeTypeId = response['id'];
              }
            },
            controller: treeType,
            title: 'Tree Type',
          ),
          15.verticalSpace,
          CustomTextField(
            controller: ageOfTree,
            inputType: TextInputType.number,
            validator: (text) {
              if (text == null || text.isEmpty) {
                return 'Required';
              } else if (text.contains(RegExp(r'[a-z]')) ||
                  text.contains(RegExp(r'[A-Z]')) ||
                  text.contains(RegExp(r'[#?!@$%^&*-]'))) {
                return 'Invalid Input';
              }
              return null;
            },
            title: 'Age of Tree',
          ),
          15.verticalSpace,
          CustomTextField(
            controller: registration,
            title: 'Registration ID (Optional)',
          ),
          15.verticalSpace,
          CustomTextField(
            maxLines: 6,
            controller: comments,
            labelText: 'comments',
          ),
          40.verticalSpace,
        ],
      ).px(18),
    ),
  ]),
),

请注意,这只是提供了代码的翻译部分,不包括问题的回答。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

In my code my keyboard closes automatically when using text formfield and I realized that it closes when I use Form and its key for validation, I am using list view with form but when I remove form it works fine but I need a form to validate, what should I do ?

  Form(
key: _formKeyPlant,
child: ListView(children: [
SizedBox(
width: size.width,
child: Column(
children: [
CustomTextField(
controller: name,
title: 'Name',
validator: (text) {
if (text == null || text.isEmpty) {
return 'Required';
} else if (text.contains(RegExp(r'[0-9]')) ||
text.contains(RegExp(r'[#?!@$%^&*-]'))) {
return 'Invalid Input';
}
return null;
},
),
15.verticalSpace,
CustomTextField(
controller: location,
title: 'Location',
validator: (text) {
if (text == null || text.isEmpty) {
return 'Required';
} else if (text.contains(RegExp(r'[0-9]')) ||
text.contains(RegExp(r'[#?!@$%^&*-]'))) {
return 'Invalid Input';
}
return null;
},
),
15.verticalSpace,
CustomTextField(
enabled: false,
onTap: () async {
final response = await showAppBottomSheet(
context,
const TreesInfoBottomSheet(),
);
if (response != null) {
treeType.text = response['name'];
treeTypeId = response['id'];
}
},
controller: treeType,
title: 'Tree Type',
),
15.verticalSpace,
CustomTextField(
controller: ageOfTree,
inputType: TextInputType.number,
validator: (text) {
if (text == null || text.isEmpty) {
return 'Required';
} else if (text.contains(RegExp(r'[a-z]')) ||
text.contains(RegExp(r'[A-Z]')) ||
text.contains(RegExp(r'[#?!@$%^&*-]'))) {
return 'Invalid Input';
}
return null;
},
title: 'Age of Tree',
),
15.verticalSpace,
CustomTextField(
controller: registration,
title: 'Registration ID (Optional)',
),
15.verticalSpace,
CustomTextField(
maxLines: 6,
controller: comments,
labelText: 'comments',
),
},
),
40.verticalSpace,
],
).px(18),
),
]),
),

答案1

得分: 2

以下是您要的翻译内容:

使用表单和滚动小部件正确的格式如下,以确保在文本字段上键盘永不隐藏:

final Key _key = GlobalKey();
FocusNode f1 = FocusNode();
FocusNode f2 = FocusNode();
@override
Widget build(BuildContext context) {
  return Form(
    key: _key,
    child: SingleChildScrollView(
      child: Column(
        children: [
          TextFormField(
            focusNode: f1,
            onFieldSubmitted: (c) {
              f1.unfocus();
              f2.requestFocus();
            },
          ),
          TextFormField(
            focusNode: f2,
          ),
        ],
      ),
    ),
  );
}
英文:

Correct formate of using textform filed with form and scroll widget is

> Please try the below code your keyboard with never hide on textformfiled

final Key _key = GlobalKey();
FocusNode f1 = FocusNode();
FocusNode f2 = FocusNode();
@override
Widget build(BuildContext context) {
return Form(
key: _key,
child: SingleChildScrollView(
child: Column(
children: [
TextFormField(
focusNode: f1,
onFieldSubmitted: (c) {
f1.unfocus();
f2.requestFocus();
},
),
TextFormField(
focusNode: f2,
),
],
),
),
);
}

huangapple
  • 本文由 发表于 2023年2月8日 12:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定