【发布时间】:2021-11-03 12:52:12
【问题描述】:
我正在使用 Flutter 2.5.3,我想删除我的 TextFormField 下方的错误消息。但是无论我做什么,当输入无效时,它都会在输入字段下方添加空间。我试图用 SizedBox 包装它,但它只会在大小盒内调整大小。不过我需要验证器,因为我想显示红色轮廓错误边框。
Widget _buildEmailForm(BuildContext context) {
final theme = Theme.of(context);
final authRepository = context.read<AuthRepository>();
return Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
"signInPageWelcome".tr(),
style: theme.textTheme.headline1,
),
SizedBox(height: 60),
TextFormField(
controller: _emailController,
validator: (value) {
final email = Email.dirty(value ?? "");
return "";
},
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsets.all(30.0),
child: Icon(
Icons.person,
color: theme.primaryColor,
),
),
hintText: 'signInPageFormEmailFieldHint'.tr(),
hintStyle: theme.textTheme.bodyText1,
contentPadding: EdgeInsets.symmetric(vertical: 30, horizontal: 30),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: theme.primaryColorLight, width: 1.0),
borderRadius: BorderRadius.circular(5.0),
),
border: OutlineInputBorder(
borderSide: BorderSide(color: theme.primaryColorLight, width: 1.0),
borderRadius: BorderRadius.circular(5.0),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: theme.primaryColorLight, width: 1.0),
borderRadius: BorderRadius.circular(5.0),
),
errorBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red, width: 1.0),
borderRadius: BorderRadius.circular(5.0),
),
errorStyle: TextStyle(color: Colors.transparent, fontSize: 0, height: 0),
),
),
SizedBox(height: 60),
Container(
width: double.infinity,
child: TextButton(
onPressed: () async {
if (!_formKey.currentState!.validate()) return;
await authRepository.sendOneTimePassword(email: Email.dirty(_emailController.value.text));
setState(() {
isEmailSent = true;
});
},
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Text(
"signInPageFormSubmit".tr(),
style: theme.textTheme.subtitle1?.copyWith(color: theme.backgroundColor),
),
),
style: TextButton.styleFrom(
elevation: 4,
backgroundColor: theme.primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(180.0),
),
),
),
),
],
),
);
}
编辑
验证器总是有意返回一个字符串,以便我可以查看错误是否隐藏。当我知道它会被隐藏时,我当然会在它有效时返回 null。
【问题讨论】: