How can I center text in TextFormField?
It seems to me that additional padding is being applied that I have no control over.
My code example:
@override
Widget build(BuildContext context) {
return TextFormField(
maxLines: 1,
minLines: 1,
maxLength: 13,
keyboardType: TextInputType.phone,
textAlignVertical: TextAlignVertical.center,
cursorWidth: 1.5,
decoration: const InputDecoration(
contentPadding: EdgeInsets.zero,
counterText: "",
prefixIcon: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'+254',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w400,
fontSize: 18,
),
),
],
),
),
);
}
2
Answers
Before
After
You need to set
isDense
totrue
on theInputDecoration
:However, the
prefixIcon
has a default padding of 12 px:so you can also do:
prefixIcon
is generally for icons, you should useprefix
instead.Also you can remove the
Row
if it’s only meant to wrap a child. Just use theText
widget directly as theprefix
.Result:
Notice how the prefix and the input text is actually aligned on the baseline. The difference comes from the font size. If you use the same
TextStyle
, it will look like this: