TextField placeholder and text vertical alignment is not right - flutter

I am developing a mobile application using Flutter. I am having an issue with aligning the text field's placeholder text and its value vertically centered.
This is my code for TextField.
return Container(
color: Colors.black,
child: Padding(
padding: EdgeInsets.all(10),
child: TextField(
onChanged: (text) {
this.filter = text.toLowerCase();
this._refreshListItems();
},
style: TextStyle(
height: 0.5
),
cursorColor: Colors.black12,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
prefixIcon: Icon(Icons.search, color: Colors.black26,),
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
hintText: "Search",
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
),
),
),
)
);
However, when it is rendered, the placeholder and its value are getting a little bit to the top vertically as in the screenshots below.
What's wrong with my code and how can I fix it?

Using text style height to 0.5 is causing the text span to be half of the size of the font size. Remove it as don't think that will help you.
style: TextStyle(
height: 0.5
),
In order to handle content size you can play with contentPadding
decoration: InputDecoration(
contentPadding: EdgeInsets.all(1),
....
),
I used the following code and it is working
contentPadding: EdgeInsets.fromLTRB(0, 8, 0, 0),

if you set a custom height, try this.
Container(
height: 36,
child: TextField(
maxLines: 1,
style: TextStyle(fontSize: 17),
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
filled: true,
prefixIcon:
Icon(Icons.search, color: Theme.of(context).iconTheme.color),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(30))),
fillColor: Theme.of(context).inputDecorationTheme.fillColor,
contentPadding: EdgeInsets.zero,
hintText: 'Search',
),
),
)

Related

flutter padding to text in textfield

I have given the textfield a custom height with a container around it. The icons are stlii in the middle but the text is not in the center of the textfield. Do somebody know a way to fix this problem?
Container(
height: 45,
child: TextFormField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[100],
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
hintText: 'Hint Text',
),
style: TextStyle(
fontSize: 18,
),
),
),
For horizontally centered hint text: The hint text aligns according to the TextFormField's textAlign, so adding textAlign: TextAlign.center to the TextFormField will center the hint text horizontally.
For vertically centered hint text: Add a contentPadding, e.g., contentPadding: EdgeInsets.symmetric(vertical: 2) to the TextField.
Container(
height: 45,
child: TextFormField(
textAlign: TextAlign.center, // this is new
decoration: InputDecoration(
filled: true,
contentPadding: EdgeInsets.symmetric(vertical: 2), // this is new
fillColor: Colors.grey[100],
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
hintText: 'Hint Text',
),
style: TextStyle(
fontSize: 18,
),
),
),

Align hint text in multiline TextField in Flutter

I have a multiline TextField with a prefixIcon, so now the Icon is in the center vertically and hint text is at top left. I want both of them to be aligned, either at the top or in the center vertically.
The code used is
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
maxLines: 3,
decoration: InputDecoration(
hintText: 'Bio',
prefixIcon: Icon(Icons.chrome_reader_mode),
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(),
),
),
),
),
Although in your particular case problem can be solved by adding a newline character (hintText: '\nBio'), a better solution is to use labelText property instead of a hintText. By default label is aligned with the center of the TextField.
TextField(
maxLines: 3,
decoration: InputDecoration(
labelText: 'Bio',
prefixIcon: Icon(Icons.chrome_reader_mode),
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(),
),
),
),
This is an ugly solution, but it works. Other solutions (e.g., setting the textAlignVertical to TextAlignVertical.center) don't work since the maxLines is not null.
Add a TextStyle to the hintText and set the height of the TextStyle to 2.8. You need to reduce this when the fontSize is bigger because the height will be multiplied by the fontSize to make the line height. I added a contentPadding to make sure the text doesn't overflow(because now the text begins from the center of the TextField).
TextField(
maxLines: 3,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 30),
hintText: 'Bio',
hintStyle: TextStyle(
height: 2.8,
),
prefixIcon: Icon(Icons.chrome_reader_mode),
fillColor: Colors.grey[200],
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide(),
),
),
),
Result:
Widget customTextView(String hint, Icon iconName) {
return Container(
decoration: BoxDecoration(
color: _DarkWhiteColor.withOpacity(0.5),
borderRadius: BorderRadius.circular(5),
),
child: Padding(
padding: const EdgeInsets.only(left: 0, right: 0),
child: TextField(
style: TextStyle(color: Colors.grey),
cursorColor: Colors.grey,
decoration: InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
fillColor: _DarkWhiteColor.withOpacity(0),
filled: true,
hintText: "Enter Your Message",
labelStyle: TextStyle(color: Colors.white),
// suffixIcon: iconName,
prefixIcon: Container(
transform: Matrix4.translationValues(0, -45, 0),
child: iconName,
),
),
maxLines: 6,
),
),
);
}

Textfield text alignment is not aligned vertically

I am trying to build a form with custom textfield height, i would like to make the textfield text and hit text to be center vertically. Here is my code
SizedBox(
height: 40,
child:
TextField(
style: TextStyle(
fontSize: 14,
),
textAlignVertical: TextAlignVertical.center,
textAlign: TextAlign.left,
maxLines: 1,
decoration: InputDecoration(
filled: true,
fillColor: Color(0xff5a9fd6).withOpacity(0.15),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff5a9fd6).withOpacity(1.0),
),
borderRadius: BorderRadius.circular(2.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(1.0),
)
),
)
),
When i decrease the font size then the text is aligned vertically but i would like to use the font size as 14 and above.
Do not wrap the TextField from SizeBox to manage the height of the TextField. You can do this by using vertical contentPadding as follows. It keeps your text in the center.
TextField(
style: TextStyle(
fontSize: 14,
),
textAlignVertical: TextAlignVertical.center,
textAlign: TextAlign.left,
maxLines: 1,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 50, horizontal: 20),
filled: true,
fillColor: Color(0xff5a9fd6).withOpacity(0.15),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff5a9fd6).withOpacity(1.0),
),
borderRadius: BorderRadius.circular(2.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(1.0),
)
),
),
You can set the contentPadding of the TextField as 0 or according to your requirement.
SizedBox(
height: 40,
child:
TextField(
style: TextStyle(
fontSize: 14,
),
textAlignVertical: TextAlignVertical.center,
textAlign: TextAlign.left,
maxLines: 1,
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(0),
filled: true,
fillColor: Color(0xff5a9fd6).withOpacity(0.15),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xff5a9fd6).withOpacity(1.0),
),
borderRadius: BorderRadius.circular(2.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(1.0),
)
),
)
),
You can to adjust contentPadding to center your text vertically, for example:
TextField(
decoration: InputDecoration(
contentPadding: const EdgeInsets.all(<your value>),
)

Flutter wrong position of text in textfield after changing height

According to my design I have a TextField with 30pt of height.
Here's how I am trying to do that:
Container(
height: 30,
child: new TextField(
textAlign: TextAlign.start,
decoration: new InputDecoration(
prefixIcon: Icon(Icons.search),
border: new OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(30.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[800]),
hintText: "Search",
fillColor: Colors.lightGreen),
)),
And here's the result:
How can I align hint and text vertically?
Set contentPadding to EdgeInsets.symmetric(horizontal: 0) in InputDecoration. This will remove the default horizontal padding for the Text:
Container(
height: 35,
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(horizontal: 0),
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0),
),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey[800]),
hintText: "Search",
fillColor: Colors.lightGreen),
),
),

How can I make rounded TextField in flutter?

Material Design for iOS doesn't look good, especially the TextField. So is there any way to create your own ? Or Is ther any way to add some styling to TextField so it will look rounded ?
You can add rounded corners to a TextField within the decoration parameter of it
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
filled: true,
hintStyle: TextStyle(color: Colors.grey[800]),
hintText: "Type in your text",
fillColor: Colors.white70),
)
#Rockvole answer is correct but just in case u don't like the default border around the TextField, you can achieve it by overriding the borderSide attribute of the OutlineInputBorder like this:
TextField(
textAlign: TextAlign.center,
controller: searchCtrl,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Enter a product name eg. pension',
hintStyle: TextStyle(fontSize: 16),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
fillColor: colorSearchBg,
),
),
You can get desired output with the help of Container Have a look..
new Container(
child: new Text (
"Text with rounded edges.",
style: new TextStyle(
color: Colors.blue[500],
fontWeight: FontWeight.w900
)
),
decoration: new BoxDecoration (
borderRadius: new BorderRadius.all(new Radius.circular(10.0)),
color: Colors.black
),
padding: new EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 16.0),
),
You can try this code bellow:
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(10.0),
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
)
I've collected this solution from several methods and solutions over the internet, it works very well with me, so it could help someone out there:
This solution will control the width and height of the TextField, and other properties
Container(
child: Theme(
data: Theme.of(context).copyWith(splashColor: Colors.transparent),
child: TextField(
autofocus: false,
style: TextStyle(fontSize: 15.0, color: Color(0xFFbdc6cf)),
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: 'Search',
contentPadding:
const EdgeInsets.only(left: 14.0, bottom: 12.0, top: 0.0),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(25.7),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
borderRadius: BorderRadius.circular(25.7),
),
),
),
),
decoration: new BoxDecoration (
borderRadius: new BorderRadius.all(new Radius.circular(30.0)),
color: Colors.white ), width: 250, height: 50, margin: new EdgeInsets.fromLTRB(20, 20, 20, 210), padding: new EdgeInsets.fromLTRB(8, 8, 8, 8),
),
You could approach the purpose in a few different ways:
First Approach:
Container(
child: new Text (
"Some Text",
style: TextStyle(
color: Colors.black
)
),
decoration: BoxDecoration (
borderRadius: BorderRadius.all( Radius.circular(15)),
color: Colors.white
),
padding: EdgeInsets.all(16.0),
),
Second Approach:
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(15.0),
),
),
filled: true,
hintStyle: new TextStyle(color: Colors.grey[600]),
hintText: "Hint text",
fillColor: Colors.white),
)
Third Approach:
TextField(
textAlign: TextAlign.center,
controller: someTextXontroller,
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Hint Text',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
contentPadding: EdgeInsets.all(16),
fillColor: Colors.blue,
),
),
Try this in TextFormField:
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20.0),
),
),