Reputation: 2861
I want to have a clear button for my textfield. I found an example of how to add it using decoration, but its position is completely off. How can I adjust the position of the icon?
Also, I cannot get the input text to be centered in the input field. I didn't find anything how to vertically center the text in the input box.
Thanks!
...
TextEditingController _textFieldController = TextEditingController();
...
Widget searchField(BuildContext context) {
return Container(
width: 200,
height: 30,
color: Colors.grey[250],
child: TextField(
controller: _textFieldController,
decoration: InputDecoration(
suffix: IconButton(
padding: EdgeInsets.all(0),
alignment: Alignment.bottomLeft,
icon: Icon(Icons.cancel),
onPressed: _onClear,
),
border: OutlineInputBorder(),
hintText: 'Blablabla',
),
)
);
}
Upvotes: 3
Views: 6948
Reputation: 1263
There are three changes needed to reach the desired result:
suffixIcon
instead of suffix
contentPadding: EdgeInsets.only(left:5)
to vertically align the text when it is vertically constrained (e.g. the container height of 30).IconButton
: padding: EdgeInsets.zero
to align the icon.The complete code is:
return Container(
width: 200,
height: 30,
color: Colors.grey[250],
child: TextField(
controller: _textFieldController,
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 5),
suffixIcon: IconButton(
padding: EdgeInsets.zero,
icon: Icon(Icons.cancel),
onPressed: () => print("clear"),
),
border: OutlineInputBorder(),
hintText: 'Blablabla',
),
)
);
Final result:
Upvotes: 4
Reputation: 103431
Reduce the padding of the InputDecoration
to zero.
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.zero,
Upvotes: 1