I have a TextField like this, where the input text and the hint text are sized differently.
TextField(
style: Theme.of(context).textTheme.subhead.copyWith(
fontSize: 70.0,
),
decoration: InputDecoration(
hintText: 'Enter a number',
hideDivider: true,
hintStyle: TextStyle(
color: Colors.grey[500],
fontSize: 25.0,
),
),
);
While the user input text is centered in my parent container, the hintText is not (top has more space than the bottom). Is there a way to vertically center the hint text as well?
The reason the hint text is not the same size as the input text is that it takes up more space than the green Container has, so it looks like Enter a nu..., which isn't very user friendly. (So alternatively, is there a way to have a newline in the hintText?)
I see this is over 2 years old but in case others run into this problem:
InputDecoration has the property contentPadding.
TextField(
decoration: InputDecoration(
hintText: 'Hint Text',
contentPadding: EdgeInsets.all(10.0),
),
);
Add textAlignVertical: TextAlignVertical.center, in your TextField.
Note: make sure you are not modifying it with other Containers or anything else & if you do then check those widgets as well.
TextField(
textAlignVertical: TextAlignVertical.center,
),
new TextField(
decoration: new InputDecoration(
hintText: 'your hint',
),
textAlign: TextAlign.center,
)
textAlign: TextAlign.center will make your hint appear in the center.
Below is the solution for your problem:
Container(
color: Colors.red,
width: 1000,
height: 500,
alignment: Alignment.center,
child: TextFormField(
keyboardType: TextInputType.emailAddress,
autofocus: false,
style: new TextStyle(fontSize: 20),
decoration: InputDecoration(
hintText: 'Email', hintStyle: TextStyle(color: Color(0xff777777))),
),
)
Related
When the TextDield receives focus the label receives a margin in relation to the prefixIcon.
Current
Objective:
Container(
padding: EdgeInsets.all(10),
child: TextFormField(
keyboardType: TextInputType.number,
textInputAction: TextInputAction.continueAction,
style: TextStyle(fontSize: 20),
decoration: InputDecoration(
alignLabelWithHint: true,
prefixIcon: Icon(Icons.device_thermostat),
labelText: "Temperature",
border: OutlineInputBorder(),
hintText: "Enter temperature",
),
),
)
Try replacing prefixIcon: Icon(Icons.device_thermostat), with prefix: Icon(Icons.device_thermostat),. Here is an example on DartPad with one field using prefixIcon and a second using prefix.
Screenshot of the problem
I am trying to add an Email, which is a long text, but when the text field got full it cut all text in half.
here is my code:
Container(
height: 45,
decoration: BoxDecoration(
color: HexColor(MyColors.white),
),
child: TextFormField(
controller: _email,
maxLength: 100,
autofocus: true,
validator: validateEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
counterText: "",
border: OutlineInputBorder(),
),
),
),
I get your point.. your problem there is text got high up.. you can set
Just adjust the contentPadding top bottom so it can be centered
decoration: InputDecoration(
contentPadding:
const EdgeInsets.only(
left: 8.0,
bottom: 8.0,
top: 8.0)),```
My problem get solved by adding those lines:
maxLines: 1,
decoration: InputDecoration(
isDense: true,
.
.
.)
I think your problem happens because you are using Container in the wrong way, so your Container is covering your TextFormField. So, in my opinion you can use Container, but with minimal attribute, or without attributes at all. But, I'm more prefer to not using Container.
And also, you can add the maxLines as null if you want it to be really long.
Your code should be like:
TextFormField(
maxLines: null,
controller: _email,
maxLength: 100,
autofocus: true,
validator: validateEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
counterText: "",
border: OutlineInputBorder(),
),
style: TextStyle(
color: Colors.black,
),
),
Also, you can improve the design later by your self.
(NOTE: In my codes, I'm not using Container)
I am using TextFormField for my flutter application search field.
When i type a text bigger that the width of textformfield, then entered text it cropped to half an not visible completely.
Please find my below code and attached image for more understanding of problem.
TextFormField(
focusNode: focusNode,
controller: textController,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintText: AppStrings.searchCatHint,
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
);
Screen for problem:
Similar problem i have seen in another question also, but no solution provided.
Similar issue link
Text display proper with #CarlosSR suggestion. But alignment issue as below.
I found a solution for the issue.
Problem description: On entering new text, old text should move to left without any problem.
Solution: I have specified a height for my TextFormField with Wrapping with Container. Code looks like below.
Container(
height:40,
child: TextFormFiled(...),
);
This worked for me. Thanks a lot for all your responses.
wrapping textformfield with container:
error will not occur:
Container(
color: Colors.black12,
width: 280,
height: 45,
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
),
),
error is with padding I guess if you are using:
Container(
color: Colors.black12,
width: 280,
height: 45,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
),
),
),
This way you can only scroll the widget in horizontal axis, plus you add a padding to get your text in the position you want. In other, I recommend you to check for Align widget.
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
focusNode: focusNode,
controller: textController,
decoration: InputDecoration(
border: InputBorder.none,
icon: Icon(
Icons.search,
),
hintText: AppStrings.searchCatHint,
hintStyle: TextStyle(
fontSize: 17,),
),
autovalidate: true,
autocorrect: false,
autofocus: true,
),
),
),
I too happend to have the exact issue.The thing that i noticed that when i have a textfield in an container of some hardcoded height you might encounter this issue.Try removing the height of the container or just remove the textfields bottom padding.This will help.
The solution to this is using the CupertinoTextField widget. You can read about it here.
I created TextFields with a custom height and a different background color. Here is the code snippet:
Container(
width: width,
height: 35,
decoration: BoxDecoration(
color: Constants.greyColor,
borderRadius: BorderRadius.all(Radius.circular(2)),
),
padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
textAlign: descriptor == null ? TextAlign.center : TextAlign.left,
decoration: InputDecoration(
hintText: placeholder,
hintStyle: Constants.textStyleTextInputHint,
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 0),
),
onChanged: (value) {
state.didChange(value);
},
obscureText: isPassword,
style: Constants.textStyleTextInput,
cursorColor: Constants.primaryColor,
),
),
It worked fine until I recently updated Flutter to version 1.12.13+hotfix.5. Now the hint text as well as the input text are not centered anymore. It seems as if the Container does not change the height of the TextField anymore. Like so:
If I add a prefixIcon the text and the icon will get perfectly centered. Like so:
Does anybody know how I can center the text without an Icon?
Thank you!
I think it's because you add a contentPadding property, even tho it's only horizontal. Have you tried removing it?
TextField(
textAlign: TextAlign.center
decoration: InputDecoration(
hintText: ...,
hintStyle: ...,
border: ...,
// contentPadding:
)
)
I'm trying to make a TextFormField that has an unbounded input field, in the sense where if the user hits the enter key the box can be infinitely expanded. However, it seems that the prefixIcon attribute is wrapped in a Center, so whenever the user hits Enter the icon is realigned to the center of the text box, making for a particularly weird experience.
I've been trying to keep that icon from moving but nothing seems to work.
This is my form field:
TextFormField(
maxLines: null,
keyboardType: TextInputType.multiline,
style: theme.textTheme.body1,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.description,
color: theme.iconTheme.color,
),
contentPadding: EdgeInsets.all(15.0),
hintText: 'Enter description'
),
)
you can use prefix: as instead of prefixIcon.
TextFormField(
maxLines: null,
keyboardType: TextInputType.multiline,
decoration: InputDecoration(
prefix: Icon(Icons.email),
hintText: 'Enter description'),
),
I solved by adding padding to the prefixIcon
prefixIcon: Padding(
padding: const EdgeInsets.only(bottom: 80),
child: Icon(
icon,
color: color,
),
),
Another hack is that you can also add some padding to the top to allow the ICON to move downwards
prefixIcon: Padding(
padding: const EdgeInsets.only(top:8),
child: Icon(
Icons.email,
color: Colors.blue,
),
),