-
Notifications
You must be signed in to change notification settings - Fork 29.3k
Text field vertical align #34355
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
Text field vertical align #34355
Conversation
a457e20
to
01ebf49
Compare
TODO:
|
9493d4c
to
b1be182
Compare
This PR adds a parameter No borderTextField(
textAlignVertical: TextAlignVertical.top, // default
) TextField(
textAlignVertical: TextAlignVertical.center,
) TextField(
textAlignVertical: TextAlignVertical.bottom,
) TextField(
textAlignVertical: TextAlignVertical(y: -0.5),
) BorderPreviously, when the TextField had a border, the text would be centered. This behavior is preserved by default, but it's also possible to position the text within a bordered TextField using TextField(
textAlignVertical: TextAlignVertical.top,
decoration: InputDecoration(
border: OutlineInputBorder(),
),
) alignLabelWithHintThe behavior of However, TextField(
textAlignVertical: TextAlignVertical.bottom,
decoration: InputDecoration(
alignLabelWithHint: true,
hintText: 'hint',
labelText: 'label',
),
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outlineBaseline is no longer needed because centering is done by textAlignVertical now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some of this comment needs to be retained above?
I confirmed that this address the problem mentioned in #18081 (comment). |
1759ff3
to
b9e01fc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good; just small potatoes.
if (_textAlignVertical == value) | ||
return; | ||
_textAlignVertical = value; | ||
markNeedsLayout(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe only do this if the "effective" value of _textAlignVertical changed. For example if it was null and _isOutlineAligned is true, then only markNeedsLayout if the old value wasn't TextAlignVerticalCenter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart.
// input and align the remaining part of the text and prefix/suffix. | ||
final double overflow = math.max(0, contentHeight - maxContainerHeight); | ||
final double baselineAdjustment = fixAboveInput - overflow; | ||
final double textAlignVerticalFactor = (textAlignVertical.y + 1.0) / 2.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not obvious (to me) what the + 1.0
and the 1 -
adjustments are for here and on the next line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some of this comment needs to be retained above?
start += centerLayout(prefixIcon, start); | ||
} | ||
if (label != null) | ||
if (label != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NICE
|
||
/// The vertical alignment of text within an input. | ||
/// | ||
/// This consists simply of a double [y] that can range from -1.0 to 1.0. -1.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This consists simply of a double [y] => A single [y] value
}) : assert(y != null), | ||
assert(y >= -1.0 && y <= 1.0); | ||
|
||
/// A value ranging from -1.0 to 1.0 that corresponds to the topmost to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that corresponds to the topmost to the => bottommost possible text position => that defines the topmost and bottommost locations of the top and bottom of the input text box.
/// bottommost possible text position. | ||
final double y; | ||
|
||
/// A TextAlignVertical that aligns to the top of the input. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these values might be easier to understand if they were defined in terms of their effect on a TextField's input text. Like: Aligns a TextField's input Text with the topmost location within the TextField.
|
||
@override | ||
String toString() { | ||
return 'TextAlignVertical(y: $y)'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you'd want $runtimeType(y: $y)
because subclassing
/// {@template flutter.widgets.inputDecorator.textAlignVertical} | ||
/// How the text should be aligned vertically. | ||
/// | ||
/// Cannot be null. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can be null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
final TextAlign textAlign; | ||
|
||
/// {@template flutter.widgets.inputDecorator.textAlignVertical} | ||
/// How the text should be aligned vertically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best to explain this a little more carefully, since what we're talking about depends on how the textfield is decorated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
/// How the text should be aligned vertically. | ||
/// | ||
/// Determines the alignment of the baseline within the available space of | ||
/// the input. For example, TextAlignVertical.top will place the baseline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space after the "."
Maybe say: input (typically a TextField).
/// the input. For example, TextAlignVertical.top will place the baseline | ||
/// such that the text, and any attached decoration like prefix and suffix, | ||
/// is as close to the top of the input as possible without overflowing. | ||
/// Likewise, prefix and suffix height are included in the calculation at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The heights of the prefix and suffix are similarly included for other alignment values.
/// | ||
/// See also: | ||
/// | ||
/// * [TextField.textAlignVertical] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Links like this are usually followed by a brief explanation. Like:
[TextField.textAlignVertical], which defines the alignment of a text field's prefix, input, and suffix, within the text field's [InputDecorator].
Adds the `textAlignVertical` param to TextField and InputDecorator, allowing arbitrary vertical positioning of text in its input.
Description
TextFields currently vertically center their text when they have an outline border. The difference between this and top alignment is almost nothing in most cases except when the input is very tall (see issue). We need to decide if we need that vertically centering functionality at all, and if so, when exactly.
Currently, this PR is just an exploration of how we could force top alignment, since according to #29927 (comment), we may not need this vertical centering at all.
Related Issues
Closes #29927
Reopened from #31079