How do I remove content padding from TextField?

后端 未结 10 1693
深忆病人
深忆病人 2021-02-05 01:28

I am new to flutter and I am creating login form, for that I have used TextField and added prefix icon but I am getting some extra spaces in between textfields (user id and pin

相关标签:
10条回答
  • 2021-02-05 02:20

    For those struggling with the TextFormField that has a suffixIcon or prefixIcon not being allowed to be smaller due to the 48x padding, you can achieve so, by wrapping it with an IntrinsicHeight that will force the child (TextFormField in this case) to shrink to a more reasonable size, mostly like it will be sized to the input font size + any decoration padding.

    However, have in mind that this should only be a workaround and there should be an option to set the icon padding manually. With this solution, if you have your icon bigger than the TextFormField input text, it won't care and the Icon will overflow the box.

    0 讨论(0)
  • 2021-02-05 02:22

    I had to achieve something similar but could not find perfect solution. Came up with and work around using Stack widget.

    Widget _username(context){
      return SizedBox(
        height: 35,
        child: Stack(
          children: <Widget>[
            Align(
              alignment: Alignment.centerLeft,
              child: Icon(
                Icons.mail,
                size: 20,
                color: Theme.of(context).accentColor,
              ),
            ),
            TextField(
              style: TextStyle(
                  color: Colors.white
              ),
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.symmetric(vertical: 11.0, horizontal: 33.0),
                hasFloatingPlaceholder: false,
                labelText: 'Username',
                labelStyle: TextStyle(
                    color: Colors.white
                ),
                enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(
                      color: Theme.of(context).accentColor,
                    )
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: Theme.of(context).accentColor,
                  ),
                ),
              ),
            ),
          ]
        ),
      );
    }
    

    SizedBox is used to control the vertical padding. For horizontal padding, Icon and TextField are stacked. This results overlapped TextField's label above Icon so contentPadding property is used to move the label to the right. With this I achieved following look.

    0 讨论(0)
  • 2021-02-05 02:23

    That prefixIcon has already contained the padding of 12.0 according to documentation. So you don't need to provide extra padding.

    See this below explanation & code which you can find in input_decorator.dart.

    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:

    prefixIcon: Padding(
         padding: const EdgeInsetsDirectional.only(start: 12.0),
         child: myIcon, // icon is 48px widget.
    )
    

    I hope it will help.

    0 讨论(0)
  • 2021-02-05 02:28

    You can use:

    TextField(
       maxLines: 1,
       decoration: InputDecoration(contentPadding: EdgeInsets.only(bottom: -10.0))
     )
    
    0 讨论(0)
提交回复
热议问题