TextInput in TextFormField Get Bottom Padded If suffixIcon or prefixIcon Property of InputDecoration is Used

眉间皱痕 提交于 2020-03-05 03:11:27

问题


Top picture: Normal good looking TextFormField without supplying any Widget for suffixIcon

Bottom picture: Suppyling Icon Widget for suffixIcon makes the Text input floating unnecessarily

Any idea what's causing this?

Code: It's a plain TextFormField with suffixIcon

   TextFormField(
      decoration: InputDecoration(
        suffixIcon: Icon(Icons.search),
      )
    )


回答1:


This may not the best approach, but it works. You can wrap TextFormField with a Container. This can make the text and icon in same row.

  Padding(
       padding: EdgeInsets.all(15),
          child: Container(
              height: 25,
              child: TextFormField(
                  decoration: InputDecoration(
                suffixIcon: Icon(Icons.search),
              ))))

Output




回答2:


Current solution:

TextFormField(
   textAlignVertical: TextAlignVertical.bottom,
   decoration: InputDecoration(
      prefixIcon: Padding(
         padding: const EdgeInsets.only(top: 12.0, right: 12.0),
         child: Icon(Icons.search)
      ),
   ),
)

The documentation:

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

Because apparently according to the documentation, the prefixIcon and suffixIcon are wrapped in Padding Widget with value of 12. So to make it back to the original position, we can just wrap it in opposite direction Padding, in this case top and right because I'm using prefixIcon. If you use suffixIcon, wrap it with top and left. And the last thing, to make the Text input not floating (get bottom padded), I add this textAlignVertical: TextAlignVertical.bottom



来源:https://stackoverflow.com/questions/60017356/textinput-in-textformfield-get-bottom-padded-if-suffixicon-or-prefixicon-propert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!