I’m having a readonly TextFormField
widget like this:
TextFormField(
/// field must neither be focusable, nor must value be editable by input
canRequestFocus: false,
readOnly: true,
controller: _textEditingController,
keyboardType: TextInputType.none,
style: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 20,
letterSpacing: 1,
fontFamily: 'Arial',
),
minLines: 1,
maxLines: 1,
validator: null,
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(
vertical: 15.0,
horizontal: 15.0,
),
errorStyle: TextStyle(
fontWeight: FontWeight.w400,
fontSize: 20,
letterSpacing: 1,
fontFamily: 'Arial',
color: Colors.red,
),
errorMaxLines: 3,
labelText: 'test dskjhfkjshf kdjdfhkjshf ksdjfhkjshf ksjdhfkjshdf kjhfkjshkf skjdfhkjsdjhf kjdhfkhsf',
labelStyle: MaterialStateTextStyle.resolveWith(
(Set<MaterialState> states) =>
TextStyle(
fontWeight: FontWeight.w400,
fontSize: 20,
letterSpacing: 1,
fontFamily: 'Arial',
),
),
floatingLabelStyle: MaterialStateTextStyle.resolveWith(
(Set<MaterialState> states) {
return TextStyle(
fontWeight: FontWeight.w400,
fontSize: 20,
letterSpacing: 1,
fontFamily: 'Arial',
);
},
),
counterText: '',
),
)
I’ve tried to set the overflow: TextOverflow.visible
of the floatingLabelStyle
and the labelStyle
properties, but this does not do anything, the example keeps overflowing in an ellipsis.
There is exactly the same issue discussed with regards to the error message overflowing, where the only way is to set the errorMaxLines
of the InputDecoration
to an integer bigger than 1. But there is no floatingLabelMaxLines
or such, so I’m wondering if it’s even possible?
I’m trying this on the iPhone 15 Simulator on a MacBook Pro, iOS 17.
2
Answers
labelText
is a string property that will use default styling from the framework. If a more elaborate label is required, consider using [label] instead. Which is aWidget
propertyresult:
Since its
Text
widget, you can customize any styling on it.TL;DR use
InputDecoration.label
and pass aText
yourself instead of usingInputDecoration.labelText
that accepts a string that’s getting ellipsized.Let’s take a look at the
TextFormField
widget source code:https://github.com/flutter/flutter/blob/78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9/packages/flutter/lib/src/material/text_form_field.dart#L200C31-L200C31
It uses
TextField
widget. Okay. Let’s take a look inside it then:https://github.com/flutter/flutter/blob/78666c8dc57e9f7548ca9f8dd0740fbf0c658dc9/packages/flutter/lib/src/material/text_field.dart#L1448C33-L1448C33
It uses
InputDecorator
for displaying the decoration (which holds your label). Let’s look at that one then:https://github.com/flutter/flutter/blob/4bc7a44eb69056a6744f576c2cea656d65b656e2/packages/flutter/lib/src/material/input_decorator.dart#L2245-L2249
Yes. If the
InputDecoration.label
was null then it indeed passes yourlabelText
to aText
widget that has anoverflow
explicitly set toellipsis
. But what if we’d put thelabel
property instead then?We get a multiline label! 🙂
A small lesson from this comes up: dig into Flutter’s source code! It’s really a source of knowledge, insider info and a way to get even more insight into how Flutter works and does things, and – in case of this problem – how to solve something.