Skip to content

Conversation

@justinmc
Copy link
Contributor

@justinmc justinmc commented Jan 28, 2019

There have been several issues where people were unable to accomplish certain simple TextField behaviors without complicated workarounds. This PR is a work in progress attempt at coming up with a minimal API addition that will satisfy these needs for TextField.

Goals:

  1. Preserve existing maxLines behavior. We will not make any non-backwards-compatible changes.
  2. Provide a way to configure a textfield so that its height will grow as text is entered, until it reaches maxLines. The textfield would similarly shrink as text is deleted.
    growth
  1. Provide a way to start expansion at a minimum number of lines.
    screenshot_1548715149
    screenshot_1548715216
  1. Provide a way to specify that a textfield's height should expand to fill the space allocated by its parent.
    screenshot_1548715633

Proposal

In addition to maxLines, add two new parameters that affect height:

  • int minLines
  • bool expands

minLines allows the developer to specify a minimum to start from as the content causes the input to grow. expands tells the input that it can size itself based on its parent if wrapped in an Expanded or Flex.

Examples

  • Goals 2 and 3: Height growth in a range of lines, including unlimited height with maxLines: null.
TextField(
  minLines: 2,
  maxLines: 4,
)
  • Goal 4: Size based on parent.
Expanded(
  child: TextField(
    expands: true,
    maxLines: null,
  ),
)

Reference: Behavior Matrix

Current Behavior

  • None
    • Single line
  • maxLines explicitly null
    • 1 line with no content, grows infinitely
  • maxLines: 1 (default)
    • Single line
  • maxLines: n
    • n lines, then scrolls
  • Wrapping in Expanded has no effect.

Proposed Behavior

  • All current behavior above
  • minLines: 1 or null
    • Single line
  • minLines: n
    • Error because maxLines is 1 and minLines can't be greater than maxLines
  • minLines: n, maxLines: explicitly null
    • n lines, scrolls when content is longer
  • expands: true with Expanded parent
    • Error because maxLines is 1, which prevents expansion.
  • maxLines: 1, minLines: 1
    • Single line
  • maxLines: n, minLines: m
    • Grows between n and m, including infinitely if n is null
    • New behavior
  • minLines: n
    • Error because maxLines is 1 and minLines can't be greater than maxLines
  • maxLines: null, expands: true wrapped in Expanded
    • Height matches parent.
    • New behavior

@tuliomagalhaes
Copy link

tuliomagalhaes commented Jan 29, 2019

Your solution is perfect and solves a lot of open issues. One of my doubt is to close or not my PR. And if you need any kind of help, I'm open to work with you in your repo.

Thank you for your attention!

@QoLTech
Copy link

QoLTech commented Jan 29, 2019

This would be great, thanks!

@Will-W
Copy link

Will-W commented Jan 29, 2019

This proposal looks like it fixes all my Text Field layout issues, thanks.

@zoechi zoechi added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. labels Jan 29, 2019
@justinmc
Copy link
Contributor Author

Thanks for the feedback! I'll move forward with this.

@TulioH I'm going to see if I can rebase this PR off of your PR. That way I'll get a good starting point for the expands flag, and you should get credit for contributing. If that works then I'll close your PR.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

@justinmc
Copy link
Contributor Author

Alright @TulioH I was able to bring in your commits. I renamed maxLinesIncrementalHeight to expands and will continue adding the functionality according to my proposal from there.

If that sounds good to you, let me know (see the CLA bot comment above) and I'll close your old PR.

@tuliomagalhaes
Copy link

For me it's ok! Thanks again!

@justinmc justinmc added cla: yes and removed cla: no labels Jan 29, 2019
@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

@justinmc justinmc force-pushed the text-field-height branch 5 times, most recently from c41c2d3 to 6ba4dc4 Compare March 7, 2019 19:16
@justinmc justinmc force-pushed the text-field-height branch 2 times, most recently from fb1c04b to 7305839 Compare March 8, 2019 16:13
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@justinmc justinmc merged commit 9e9f48d into flutter:master Mar 8, 2019
@justinmc justinmc deleted the text-field-height branch March 8, 2019 20:22
@tvolkert
Copy link
Contributor

tvolkert commented Mar 9, 2019

Unfortunately, this was identified as the culprit of some test failures when rolling this into Google. e.g.:

breakage

I'm gonna revert, and we can re-apply with a fix.

@zaidkazi
Copy link

zaidkazi commented Apr 9, 2021

#80133 Still dont understand how to stop the textfield from scrolling vertically when min and max and the user goes over that range

@vqthanh1412489
Copy link

Expanded(
  child: TextField(
    expands: true,
    maxLines: null,
  ),
)

NOT work for password field. If turn on key obscureText: true

@justinmc
Copy link
Contributor Author

Can you file a new issue if this isn't working for you? Mention me on it and I'd be happy to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.