user3697484
user3697484

Reputation:

Flutter TextFormField add padding to error text

I have a Form elements like this

enter image description here

The error message stacks up like this

Is there any way to adding small padding around the error text? enter image description here

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: "[email protected]",
                                    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;
                                },
                              ),
                            ),

Upvotes: 0

Views: 2276

Answers (1)

Nisanth Reddy
Nisanth Reddy

Reputation: 6430

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.

Upvotes: 2

Related Questions