简体   繁体   中英

Flutter TextFormField add padding to error text

I have a Form elements like this

在此处输入图像描述

The error message stacks up like this

Is there any way to adding small padding around the error text? 在此处输入图像描述

This is the widget

 Card(
                              color: Colors.white.withOpacity(0.9),
                              shadowColor: Colors.grey.withOpacity(0.2),
                              elevation: 15,
                              child: TextFormField(
                                style: TextStyle(
                                  fontSize: _screenFontSize,
                                ),
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    prefixIcon: Icon(
                                      Icons.email_outlined,
                                      color: Colors.grey,
                                    ),
                                    hintText: "example@website.com",
                                    labelText: "EMAIL",
                                    labelStyle: TextStyle(color: Colors.grey)),
                                keyboardType: TextInputType.emailAddress,
                                // The validator receives the text that the user has entered.
                                validator: (value) {
                                  if (value == null ||
                                      value.isEmpty ||
                                      !value.contains('@') ||
                                      !value.contains('.')) {
                                    return 'Please enter a valid Email';
                                  }
                                  return null;
                                },
                              ),
                            ),

UPDATE:

If you are fine with hacky approaches, use your error string like this,

return '    Please enter a valid Email\n';

Actual Explanation:

As per the current implementation, this is not possible in Flutter .

Explanation:

If you check the source code for TextFormField and find out the Widget responsible for rendering the error string, you will come to the following file.

flutter/lib/src/material/input_decorator.dart

Now, if you check how it renders the error message,

if (widget.errorText != null) {
  return Stack(
    children: <Widget>[
      Opacity(
        opacity: 1.0 - _controller.value,
        child: _helper,
      ),
      _buildError(),
    ],
  );
}

Here, _buildError is as such,

Widget _buildError() {
  assert(widget.errorText != null);
  return Semantics(
    container: true,
    liveRegion: true,
    child: Opacity(
      opacity: _controller.value,
      child: FractionalTranslation(
        translation: Tween<Offset>(
          begin: const Offset(0.0, -0.25),
          end: Offset.zero,
        ).evaluate(_controller.view),
        child: Text(
          widget.errorText!,
          style: widget.errorStyle,
          textAlign: widget.textAlign,
          overflow: TextOverflow.ellipsis,
          maxLines: widget.errorMaxLines,
        ),
      ),
    ),
  );
}

Now it you notice the actual Text widget being built, you will find that it is just wrapped inside an Opacity and FractionalTranslation .

Now, FractionalTranslation could've possibly allowed us to slightly alter the position of the Text widget, but if you notice, the translation is hardcoded to animate between a Tween and as such is not open to changes through any properties.

So, unfortunately, not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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