Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce padding on prefixIcon on TextField?

I can't figure out how to get past the 48px Material library default. I did a quick scan through the SDK and couldn't find anything. I know it's something to do with the prefixIcon parameter itself because it will always be 48px or whatever it is no matter what is placed inside.

I have a custom SDK so if anyone knows where it is I'd like to reduce it because it always just gets in the way.

enter image description here

  // : Searchbar Decoration
  static InputDecoration searchbarDecoration = InputDecoration(
    prefixIcon: Icon(
      Icons.search,
      color: DocumentColours.colour10,
    ),
    contentPadding: EdgeInsets.symmetric(
      vertical: 0.0,
      horizontal: 0.0,
    ),
    fillColor: DocumentColours.colour8,
    filled: true,
    labelText: 'Search',
    labelStyle: FontStyles.searchbarLabel,
    hasFloatingPlaceholder: false,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(5.0),
      borderSide: BorderSide(width: 0.0, style: BorderStyle.none),
    ),
  );

like image 999
tyirvine Avatar asked May 07 '20 02:05

tyirvine


People also ask

How do I remove content padding from TextField?

As of flutter 1.17. 5 (and still the same in 2. X) to completely remove or manipulate the padding manually, first you must set isDense: true and then you can adjust the contentPadding as you wanted or apply padding on the parent widget instead.

How do you change the padding in TextField in Flutter?

You can set the inner padding of a TextField by using the contentPadding property of the InputDecoration class.

How do you remove padding in Flutter?

removePadding, which uses this method to remove padding from the ambient MediaQuery. SafeArea, which both removes the padding from the MediaQuery and adds a Padding widget. removeViewInsets, the same thing but for viewInsets. removeViewPadding, the same thing but for viewPadding.

How do I add padding to a textfield in Java?

You can set the inner padding of a TextField by using the contentPadding property of the InputDecoration class. The height of a TextField depends on its inner padding, font size, and line height. The font size can be set by manipulating the fontSize property of the TextStyle class.

How much padding do I need for the prefix icon?

Anything larger than 24px will require additional padding to ensure it matches the material spec of 12px padding between the left edge of the input and leading edge of the prefix icon. To pad the leading edge of the prefix icon:

Which textfield has no padding around it?

{ In the output, you can see two TextFields. We have entered “hello geek” in both the TextFields. The first one is the Regular TextField which has padding all around it. In the second one, you will see no padding at all.

How do I expand the size of the prefix icon?

The prefix icon is constrained with a minimum size of 48px by 48px, but can be expanded beyond that. Anything larger than 24px will require additional padding to ensure it matches the material spec of 12px padding between the left edge of the input and leading edge of the prefix icon. To pad the leading edge of the prefix icon:


1 Answers

NOTE: To solve this, use the prefixIconConstraints property of the InputDecoration as of SDK version 1.17.0

Below is what the documentation says (1.17.0) input_decorater.dart:

BoxConstraints can be used to modify the surrounding of the prefixIcon. This property is particularly useful for getting the decoration's height less than 48px. This can be achieved by setting [isDense] to true and setting the constraints' minimum height and width to a value lower than 48px.

Check the code below: It works perfectly:

TextField(
    decoration: InputDecoration(
      // choose any icon of your choice here
      prefixIcon: Icon(Icons.person),
      // set the prefix icon constraints
      prefixIconConstraints: BoxConstraints(
        minWidth: 25,
        minHeight: 25,
      ),
    ),
  ),

I hope this helps.

like image 63
V.N Avatar answered Oct 25 '22 22:10

V.N