Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextField height changes if the errorText shows #15400

Closed
Hixie opened this issue Mar 11, 2018 · 14 comments · Fixed by #29811
Closed

TextField height changes if the errorText shows #15400

Hixie opened this issue Mar 11, 2018 · 14 comments · Fixed by #29811
Assignees
Labels
c: regression It was better in the past than it is now f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Comments

@Hixie
Copy link
Contributor

Hixie commented Mar 11, 2018

Put this in a dialog, and as you type in the text field, the dialog changes height.

        new Form(
          autovalidate: true,
          child: new ListBody(
            children: <Widget>[
              new TextFormField(
                decoration: const InputDecoration(
                  labelText: 'User name',
                ),
                validator: (String name) {
                  if (name.length < 2)
                    return 'User names must be alphabetic and at least three characters long.';
                  },
                ),
              ),
            ],
          ),
        ),

cc @HansMuller

@Hixie Hixie added c: regression It was better in the past than it is now framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Mar 11, 2018
@Hixie Hixie added this to the 4: Next milestone milestone Mar 11, 2018
@alidawud
Copy link

alidawud commented Oct 8, 2018

this might help you
https://stackoverflow.com/questions/50523006/flutter-increase-height-of-textformfield

@SinaSeirafi
Copy link

Put the TextFormField inside a Container, and give it a certain height.
That should prevent vertical moving.

@babenkovladimir
Copy link

I used SizedBox. I set a specific height, which was enough for the height not to jump.

@justinmc
Copy link
Contributor

Here are some screenshots showing my understanding of the problem:

flutter_02
flutter_01

When the input has an error message, it is considered to be taller than without it, so the parent widget increases its height to accommodate it. That can definitely be pretty jarring for the developer in cases like this dialog or maybe even with a column of text fields.

I'm going over some solutions and I'll post back.

@justinmc
Copy link
Contributor

The two workarounds for this problem are:

  1. Wrap the TextFormField in a fixed height parent.
SizedBox(
  height: 80,
  child: TextFormField(
    validator: myValidator,
  ),
),
  1. Give a helperText of a single space.
TextFormField(
  decoration: const InputDecoration(
    helperText: ' ',
  ),
  validator: myValidator,
),

The referenced PR just adds an explanation of these workarounds to the documentation. I think this is better than changing any behavior. We want to avoid breaking changes. Also, I can see a situation where users want the existing behavior so that they can tightly space their inputs.

@AyushmanChatterjee
Copy link

Thank you so much! This was driving me insane as I am using two textformfields side by side in a row and the validation errorText kept breaking up the alignment of the form fields. Using an empty helperText was a simple solution to the problem.

@ksankumar
Copy link

ksankumar commented Oct 6, 2019

The two workarounds for this problem are:

  1. Wrap the TextFormField in a fixed height parent.
SizedBox(
  height: 80,
  child: TextFormField(
    validator: myValidator,
  ),
),
  1. Give a helperText of a single space.
TextFormField(
  decoration: const InputDecoration(
    helperText: ' ',
  ),
  validator: myValidator,
),

The referenced PR just adds an explanation of these workarounds to the documentation. I think this is better than changing any behavior. We want to avoid breaking changes. Also, I can see a situation where users want the existing behavior so that they can tightly space their inputs.

With helperText makes awesome solution

@jacobokoenig
Copy link

jacobokoenig commented Nov 16, 2019

This is still an issue for me, even with a sizedbox or container, and a helper text! Can't prevent the text field from becoming smaller!

Container(
          height: 32,
          child: TextFormField(
            decoration: InputDecoration(
              helperText: '  ',
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(16),
                borderSide: BorderSide(
                  color: Colors.white,
                ),
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(16),
                borderSide: BorderSide(
                  color: Colors.white,
                ),
              ),
            ),
            validator: validator,
          ),
        ),

@justinmc
Copy link
Contributor

@jacobokoenig I don't think I see the TextFormField becoming smaller in your example. I'm on the master channel. I changed your code by adding a color to the Container so that I could see how tall it is, and I also added autovalidate to your TextFormField so that the error message shows immediately.

flutter_24
flutter_25

Neither the border of the TextFormField or the Container in the background change size after the error appears.

@camsethsum
Copy link

This is still an issue for me, even with a sizedbox or container, and a helper text! Can't prevent the text field from becoming smaller!

Container(
          height: 32,
          child: TextFormField(
            decoration: InputDecoration(
              helperText: '  ',
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(16),
                borderSide: BorderSide(
                  color: Colors.white,
                ),
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(16),
                borderSide: BorderSide(
                  color: Colors.white,
                ),
              ),
            ),
            validator: validator,
          ),
        ),

I also encountered the same problem, the workaround that I made was make the fontsize of your errorText and helperText the same so that they will take the space.

@psygo
Copy link

psygo commented Mar 15, 2020

Taken from this SO answer and the new issue #52634:


There's another alternative to adding extra, permanent padding to cover the errorText — which would probably mess up many designer's original project.

You could create a modified version of the source's TextFormField.

To achieve just that, you can:

  1. Copy-paste all of the content in the source's TextFormField.
  2. Rename your custom TextFormField just so you avoid naming conflicts with the original.
    • You should probably also rename the internal state class.
    • In VS Code, you can use Ctrl + H to replace TextFormField for, say, TextFormFieldWithErrorTextOption.
  3. Add another parameter to the TextFormField's constructor (below this line), say, errorTextPresent:
    // `true` is the current default, i.e., showing the `errorText`
    bool errorTextPresent = true 
  4. Change the decoration's initialization for the internal TextField:
    1. From:
      decoration: effectiveDecoration.copyWith(field.errorText)
    2. To:
      decoration: effectiveDecoration.copyWith(
          errorText: errorTextPresent ? field.errorText : null)
  5. Then, you can use your new TextFormField similarly to how you use any TextFormField:
    TextFormFieldWithErrorTextOption(
      errorTextPresent: false, // `false` will disable the `errorText`
      ...
    ),

@ooanishoo
Copy link

https://stackoverflow.com/a/60208333/5392889

try this answer. Worked really well for me

@TheAlphamerc
Copy link

https://stackoverflow.com/a/60208333/5392889

try this answer. Worked really well for me

@ooanishoo It will probably help if we don't want to show the field validation message.
But if we want to show error message then #15400 (comment) really helpful .

@lock
Copy link

lock bot commented Apr 19, 2020

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.

@lock lock bot locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: regression It was better in the past than it is now f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging a pull request may close this issue.