Skip to content

TextField is missing a real maxLines. #19105

@marcglasberg

Description

@marcglasberg

A common UI pattern is having a text field which grows vertically, adding more lines, as you type, up to a certain number of lines, after which the content scrolls.

Presently, we can have a growing TextField by making maxLines=null. However, then the TextField never stops growing. Otherwise, if we make, say, maxLines=6, then it will have 6 fixed lines, and will not grow.

Unfortunatelly, maxLines was a bad name. That should have been called simply "lines", since it fixes the number of lines, and is not a maximum. However, that cannot be changed anymore.

I will now suggest adding another parameter, called maxLineLimit, which lets the number of lines grow until that number and then no more. And while we're at that, I would also add the less important minLineLimit.

So, for example, if we make maxLines=null, minLineLimit=2, maxLineLimit=6, then the TextField starts with 2 lines, and, if necessary to fit the text, grows until 6 lines. After that it scrolls.

This change is very easy to do, in file editable.dart, the method _preferredHeight in class RenderEditable has these last 2 lines:

    _layoutText(width);
    return math.max(preferredLineHeight, _textPainter.height);

These could be simply changed to:

      _layoutText(width);
      return math.min(preferredLineHeight * maxLineLimit, math.max(preferredLineHeight * minLineLimit, _textPainter.height));

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsc: new featureNothing broken; request for a new capabilitycustomer: crowdAffects or could affect many people, though not necessarily a specific customer.f: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.waiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions