Skip to content

InputDecorator 'errorText' should have 'textAlign' property #11068

Description

@kostaa

When used with the TextField the InputDecorator 'errorText' uses the TextField's textAlign property (I suspect).

This is a problem when the TextField is used to show numerical values in which case the value should be right-aligned but the error message should be left-aligned.

Activity

  1. added
    a: text inputEntering text in a text field or keyboard related problems
    c: new featureNothing broken; request for a new capability
    frameworkflutter/packages/flutter repository. See also f: labels.
    p: material_uimaterial_ui package in flutter/packages
    on May 1, 2018
  2. added this to the milestone on May 1, 2018
  3. Hixie commented on May 1, 2018

    @Hixie
    Contributor
  4. HansMuller commented on Jun 13, 2018

    @HansMuller
    Contributor

    This makes sense. We could an errorAlign property or just enable providing an error widget.

  5. jonas-zebari commented on Oct 17, 2019

    @jonas-zebari

    @HansMuller Is there an update on this issue? I have also run into the need for explicitly specifying the text alignment for the field input and the error text separately.

    Also, the alignment of error text changes its default from left to center depending on the number of lines the error text takes up, which does not seem like intended behavior.

    Here's a project that makes it easy to observe the behavior I'm talking about:

    code sample
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: AdjustableTextFieldWithError(),
            ),
          ),
        );
      }
    }
    
    class AdjustableTextFieldWithError extends StatefulWidget {
      @override
      _AdjustableTextFieldWithErrorState createState() => _AdjustableTextFieldWithErrorState();
    }
    
    class _AdjustableTextFieldWithErrorState extends State<AdjustableTextFieldWithError> {
      late TextEditingController _controller;
      bool _isValid = false;
      TextAlign _alignment = TextAlign.left;
      int _lengthMultiplier = 1;
    
      @override
      void initState() {
        super.initState();
        _controller = TextEditingController();
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(12),
              child: TextField(
                controller: _controller,
                textAlign: _alignment,
                onChanged: (value) {
                  setState(() {
                    _isValid = _controller.text == 'A1';
                  });
                },
                decoration: InputDecoration(
                  hintText: 'Write the string "A1"',
                  errorText: _isValid ? null : 'The string must be "A1"' * _lengthMultiplier,
                  errorMaxLines: 3,
                ),
              ),
            ),
            ButtonBar(
              alignment: MainAxisAlignment.center,
              children: <Widget>[
                FlatButton(
                  child: const Text('Long'),
                  onPressed: () {
                    setState(() {
                      _lengthMultiplier = 7;
                    });
                  },
                ),
                FlatButton(
                  child: const Text('Medium'),
                  onPressed: () {
                    setState(() {
                      _lengthMultiplier = 4;
                    });
                  },
                ),
                FlatButton(
                  child: const Text('Short'),
                  onPressed: () {
                    setState(() {
                      _lengthMultiplier = 1;
                    });
                  },
                ),
              ],
            ),
            ButtonBar(
              alignment: MainAxisAlignment.center,
              children: <Widget>[
                FlatButton(
                  child: const Text('Left'),
                  onPressed: () {
                    setState(() {
                      _alignment = TextAlign.left;
                    });
                  },
                ),
                FlatButton(
                  child: const Text('Center'),
                  onPressed: () {
                    setState(() {
                      _alignment = TextAlign.center;
                    });
                  },
                ),
                FlatButton(
                  child: const Text('Right'),
                  onPressed: () {
                    setState(() {
                      _alignment = TextAlign.right;
                    });
                  },
                ),
              ],
            ),
          ],
        );
      }
    }
  6. esDotDev commented on Dec 28, 2019

    @esDotDev

    I think the errorWidget approach makes sense. Most of what is useful about the TextInput's built-in error handling is the way the label interacts with the border, and animates it's color and position.

    The error messages are simple widgets, we can provide those ourselves and it is much more flexible.

    For example, I'm stuck right now cause I need a 2 line error message, which is a dead end with the existing TextInput. Now I basically need to hack the widget to get the error outline to show, while providing my own error message, which is going to be pretty cringy.

  7. modified the milestones: , on Jan 7, 2020
  8. erf commented on Apr 13, 2020

    @erf

    As i use textAlign: TextAlign.center for the TextFormField, i would like the error text to also be center aligned.

    A solution could be to either return a Widget in the validator or set in errorText ( instead of a string ), or add a errorAlignment property.

    Returning an error widget would be more flexible / customisable.

  9. wilburx9 commented on Apr 25, 2020

    @wilburx9

    Please, is there any update on this?

  10. LockedThread commented on May 2, 2020

    @LockedThread

    I'm with @Wilburt on this. When will this be added? This is pretty necessary and there are multiple issues open related to this.

  11. added
    P3Issues that are less important to the Flutter project
    on May 29, 2020
  12. 31 remaining items

  13. artimmy commented on May 24, 2022

    @artimmy

    I got the solution, i'm trying to make a PR but i don't have enough time to make the tests. The solution consist in a WidgetBuilder function inside the InputDecoration class that take in input the errorText String when not Null and we can build a widget from it. If someone want to help me on how to tests it i really appreciate it.

    atta boy

  14. CordMemescape commented on Jun 5, 2022

    @CordMemescape

    For my case, I got a workaround solution using Directionality widget

    const Directionality(
      textDirection: TextDirection.rtl, // align errorText to the right
      child: TextField(
        textDirection: TextDirection.ltr, // keep the input text aligned to the left
        decoration: InputDecoration(errorText: 'Field required'),
      ),
    ),
    
    image

    NOTE: Directionality widget allows to align only to the left or to the right.

    Nice idea but it also aligns the InputDecoration's labelText to the right and there doesn't appear to be a way to align that back to the left. So, a good workaround if you don't use labelText. Not so good if you do.

    The right solution would be to simply replace the errorText string with an errorText widget and allow the user to decide.

  15. HansMuller commented on Jun 6, 2022

    @HansMuller
    Contributor

    There's a PR underway that's attempting to provide support for specifying an error widget. Looks like it could use some help: #104165

  16. added a commit that references this issue on Jan 17, 2023
    e9a64f9
  17. Julitech commented on Jun 19, 2023

    @Julitech

    Any updates on this??

  18. HansMuller commented on Jun 20, 2023

    @HansMuller
    Contributor

    A fix was started in January and seemed to be getting pretty close: #118610. Unfortunately the dev hasn't been active since early March.

    CC @Renzo-Olivares, @justinmc, @KishanBusa8

  19. justinmc commented on Jun 20, 2023

    @justinmc
    Contributor

    If @KishanBusa8 doesn't respond, then someone should probably take over the PR. I think it was pretty close, from my memory.

  20. bleroux commented on Jun 21, 2023

    @bleroux
    Contributor

    FYI, I filed #129275 which can be used by @KishanBusa8 to update #118610 or can become a non WIP PR if @KishanBusa8 does not respond or can not work on the update.

  21. justinmc commented on Jun 21, 2023

    @justinmc
    Contributor

    Great, thanks! Let's give it until next week maybe and then move forward with your PR if we haven't heard back.

  22. added
    r: fixedIssue is closed as already fixed in a newer version
    on Jun 28, 2023
  23. github-actions commented on Jul 12, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  24. locked as resolved and limited conversation to collaborators on Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: text inputEntering text in a text field or keyboard related problemsc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions