简体   繁体   English

如何在 Flutter 中将 TextFormField 的错误文本居中?

[英]How can I center the error text of a TextFormField in Flutter?

I have a TextFormField that displays an error message when invalid.我有一个 TextFormField 在无效时显示错误消息。 It works great, but I'd like the error text to be centered.它工作得很好,但我希望错误文本居中。

显示错误消息的 FormTextField

Container(
    child: TextFormField(
    validator: (val) => val.isEmpty ? 'Enter a Username' : null,
    onChanged: (val) {setState(() => username = val);},
    textAlign: TextAlign.center,
    decoration: InputDecoration(
        hintText: 'Pick Your Username',
        errorStyle: // is it possible to center the errorText?
        ),
    ),
),

Using the errorStyle property, I can change the color, weight, size, etc. However, I haven't been able to figure out how to get the little red text that appears when the TextFormField is invalid to center.使用 errorStyle 属性,我可以更改颜色、重量、大小等。但是,我无法弄清楚如何让 TextFormField 无效时出现的红色小文本居中。

Thanks in advance for any advice!提前感谢您的任何建议!

The default style of the error for Material TextField is displayed on the lower left portion of the Widget. Material TextField 的默认错误样式显示在 Widget 的左下部分。 If you'd like to modify how the error is displayed, you can create a Widget to display the error message.如果您想修改错误的显示方式,可以创建一个 Widget 来显示错误消息。

Here's a sample.这是一个示例。

import 'package:flutter/material.dart';

class SampleTextField extends StatefulWidget {
  const SampleTextField({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<SampleTextField> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<SampleTextField> {
  final _formKey = GlobalKey<FormState>();
  bool textFieldError = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: StatefulBuilder(
        builder:
            (BuildContext context, void Function(void Function()) setState) {
          return Center(
            child: Form(
              key: _formKey,
              child: Column(
                children: [
                  TextFormField(
                    onFieldSubmitted: (String text) {
                      _formKey.currentState!.validate();
                    },
                    validator: (String? text) {
                      // Update textFieldError to determine if an error should be displayed
                      setState(() {
                        textFieldError = text == null || text.isEmpty;
                        debugPrint('Error: $textFieldError');
                      });
                      // Return a null on validator to not display the default error from the TextField
                      return null;
                    },
                    decoration: const InputDecoration(
                      hintText: 'Hint Text',
                      border: OutlineInputBorder(),
                    ),
                  ),
                  textFieldError
                      ? const Text('Error Text',
                          style: TextStyle(color: Colors.red),
                        )
                      : const SizedBox()
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

Return null on TextFormField.validator() so the default error style won't be displayed.TextFormField.validator()上返回null ,因此不会显示默认错误样式。 You can then use a StatefulBuilder so the entire Screen won't be rebuilt when setState() was called.然后,您可以使用 StatefulBuilder 以便在调用setState()时不会重建整个屏幕。

演示

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

相关问题 Flutter Textformfield 如何在 Textformfield 中居中文本 - Flutter Textformfield How to center text in a Textformfield 如何在 Flutter 中将 TextFormField 的文本对齐到中心、垂直? - How to align text of TextFormField to center, vertical in Flutter? 如何更改 Textformfield 验证文本的 position? Flutter - How can I change the position of the Textformfield validation text ? Flutter 如何在 flutter 的 TextFormField 中的图标和文本之间添加分隔线 - How can i add a divider between icon and text in TextFormField in flutter 在 Flutter 中,如何更改 TextFormField 验证错误消息样式? - In Flutter, How can I change TextFormField validation error message style? flutter - 如何删除 TextFormField 中错误的内容填充? - flutter - How can I remove the content padding for error in TextFormField? 在 Flutter 中,如何更改 TextFormField 验证错误行样式? - In Flutter, How can I change TextFormField validation error line style? 如何在 Flutter 中的 TextFormField 中显示错误文本,而不是在其中 - How to show error text in TextFormField in Flutter below it and not in it 如何将数据传递到 Flutter 中的 textformfield? - How can I pass data into textformfield in Flutter? 如何在 TextFormField (Flutter) 中使用 AppLocalization - How can I use AppLocalization in TextFormField (Flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM