How do I only apply padding to the text in a TextField in Flutter?

DartFlutter

Dart Problem Overview


Without padding I get this result:

no padding

With something like this

Padding(padding: EdgeInsets.all(20.0), child: TextField())

I get the following result:

with padding

It might be a bit hard to see, but you only have to look at the red badge across the edge to realise what I mean.

I would like to only move the text with my padding, but in reality the whole TextField has the padding applied, i.e. that the underline moves with the padding, but I would like to have the underline still go across the whole view, that only the text inside the TextField is affected by the padding.

Dart Solutions


Solution 1 - Dart

To apply the padding to the content of the TextField. You can apply the

> contentPadding property of ItemDecoration at decoration property of TextField.

Like this:

TextField(
  textAlign: TextAlign.left,
  decoration: InputDecoration(
    hintText: 'Enter Something',
    contentPadding: EdgeInsets.all(20.0),
  ),
)

Solution 2 - Dart

Works for me:

TextFormField(
  decoration: InputDecoration(
    border: InputBorder.none,
    contentPadding: EdgeInsets.symmetric(vertical: 10), //Change this value to custom as you like
    isDense: true, // and add this line
    hintText: 'User Name',
    hintStyle: TextStyle(
        color: Color(0xFFF00),
  )),
  keyboardType: TextInputType.text,
  style: TextStyle(
      color: Color(0xFFF00),
      fontSize: 14),
  maxLines: 1,
)

Solution 3 - Dart

If you want to apply uneven, vertical padding to each side of the text inside the TextField, you'll have to first set textAlignVertical to TextAlignVertical.top.

If you don't do this, the largest top/bottom padding will apply to both the top and bottom of the text. I'm guessing this is because the TextField text is always centered horizontally.

TextField(
        controller: model.captionController,
        maxLines: null,
        minLines: 8,
        maxLength: 200,
        textAlignVertical: TextAlignVertical.top,
        decoration: PostFilledField('Add a caption...').copyWith(
          contentPadding: EdgeInsets.fromLTRB( 8, 0,  8, 90.0),
          isDense: true
        ),
        style: PostSmallText(),
      ),

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncreativecreatorormaybenotView Question on Stackoverflow
Solution 1 - DartDhrumil Shah - dhuma1981View Answer on Stackoverflow
Solution 2 - DartDoan BuiView Answer on Stackoverflow
Solution 3 - DartJoe MullerView Answer on Stackoverflow