Good day, I am creating TextFormFields in Flutter and I am getting a bit frustrated that I can't have Text on the left of the entry input that STAYS there.
hintText disappears when you enter the field.
prefixText only appears after you enter the field.
prefixIcon does exactly what I want, but I want Text instead of an icon.
The above image uses prefixIcon and hintText.
Does anyone have a suggestion on how to replace the icon I currently have with permanent Text instead, please? Thank you for any and all assistance.
Try using the prefixText property in decoration.
TextField(
controller: _textController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Enter a valid ZIP-Code',
errorText: _hasInputError ? "ZIP-Code is too short" : null,
counterText: _textController.text.length.toString(),
helperText: "ZIP-Code of shipping destination",
labelText: "ZIP-Code",
prefixText: "DE - ",
suffixIcon: Icon(Icons.airport_shuttle),
),
),
Refer this link
I think you cannot use prefixIcon when using decoration.
prefixIcon takes in a Widget type so you can pass a Widget into it if you want
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
hintText: "Password",
prefixIcon: CircleAvatar(
backgroundImage: NetworkImage(kDefaultProfilePicture),
) // You can replace this with a Text("") widget,
suffixIcon: IconButton(
icon: Icon(Icons.visibility),
onPressed: () {},
),
),
),
I used prefixIcon with an UnconstrainedBox:
TextFormField(
decoration: InputDecoration(
prefixIcon: UnconstrainedBox(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text("Date:")
)
)
)
)
Related
I am developing this textfield and for some reason the text is having some space below it. Please let me know the solution to this.
This is my code for textfield.
Expanded(
child: Container(
height: 40,
child: TextField(
keyboardType: TextInputType.number,
controller: textEditingController,
style: TextStyle(
fontSize: 14, height: 0, color: Colors.white),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
decoration: InputDecoration(
// isDense: true,
// errorText: validateQtyTxt.toString(),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)),
labelText: 'QTY > '
// hintText: 'Enter a search term',
),
onChanged: (String newValue) {
(text) => setState(() => _text);
print(newValue);
int qty;
derivativeScanController.buttonClick(newValue, qty);
},
),
),
),
This is the image for reference
enter image description here
There is a property called contentPadding which takes EdgeInsetsGeometry type value with that you can reduce the spacing between textfield and actual text
sample code .
contentPadding: EdgeInsets.zero,
if you want to remove all the spacing change the isDense property to true also
isDense: true,
all the space will be removed
You can use the contentPadding in the decoration property of the TextField to edit its padding.
https://api.flutter.dev/flutter/material/InputDecoration-class.html
TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(...),
),
)
Which you can fill with whatever values you want. You can use things like EdgeInsets.zero or EdgeInsets.symmetric too:
https://api.flutter.dev/flutter/painting/EdgeInsets-class.html
Change the textStyle from 0 to 1 and adjust the text using padding.
I am a beginner in flutter, I was working with the textfield widget. I want the value of textfield when user click enter or when the value is completed. Thanks for the help in advance.
TextField(
cursorColor: primaryColor,
decoration: InputDecoration(
icon: Icon(
Icons.search,
color: primaryColor,
),
hintText: 'Search for your favourite',
border: InputBorder.none,
),
),
To get the text field value once it is completed, you can use the text field onSubmitted function. It provides you with the value once the user submits it. Here is the line of code you need to add.
TextField(
cursorColor: primaryColor,
decoration: InputDecoration(
icon: Icon(
Icons.search,
color: primaryColor,
),
hintText: 'Search for your favourite',
border: InputBorder.none,
),
onSubmitted: (value) {
print(value); // This will print the value submitted by the user. You can add your function inside this.
},
You can use onChanged to print it out.
TextField(
keyboardType: TextInputType.emailAddress,
textAlign: TextAlign.center,
onChanged: (value) {
email = value;
//Do something with the user input.
},
decoration: kTextFieldDecoration.copyWith(hintText: 'Eenter your email')),
if you would like to use an advanced validator to check input value.
Please refer to this page flutter-how-to-change-border-of-text-form-field-on-error
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.
I currently have this TextFormField with a hintText:
the goal is to add Units inside the TextFormField, regardless of whether the user is typing or not. It should kinda look like this:
How to achieve this?
Also, how to center the value?
Here's my current code for the TextFormField:
TextFormField(
decoration: InputDecoration(
hintText: '0.0',
contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 5.0),
filled: true,
fillColor: Colors.white24,
floatingLabelBehavior: FloatingLabelBehavior.never,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: BorderSide.none,
),
counterText: '',
),
),
TheTextFormField decoration can a suffixText property
TextFormField(
controller: c,
// align to center
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: '0.0',
// show kg
suffixText: 'Kg',
)
Text can be centered using the textAlign property
One way is to use TextFormField and text inside row which is inside a container
something like this can help
Container(
child: Row(
children: [TextFormField(), Text('Kg')],
),
),
another way is that you can use
TextFormField(
decoration: InputDecoration(suffixText: 'Kg'),
),
but here the problem is that you will only get suffix text when the TextFormField is enabled, one workaround you can try to make it always visible is to use autovalidate property like
TextFormField(
autovalidateMode: AutovalidateMode.always,
decoration: InputDecoration(suffixText: 'Kg'),
),
I have not tested this autovalidateMode but it should work properly.
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,
),
),