I would like to create a text field with a prefix that is always visible. I am creating a phone field similar to the one used for WhatsApp phone number entry. I would like to have the country code as a persistent prefix.
I have tried the prefix and prefixText options for InputDecoration, however, the prefixes are only visible when the textField is in focus. Any help offered on how to achieve this will be highly appreciated.
prefixIcon. An icon that appears before the prefix or prefixText and before the editable part of the text field, within the decoration's container.
How to Handle Input Data In TextFormField In Flutter Using Controller. To handle user input in TextFormField, create an object of TextEditingController class. Create a TextEditingController object like below and assign it to the controller property of TextFormField. Its object will hold the input data.
Steps to change TextField background color in Flutter Step 1: Locate the file where you have placed the TextField widget. Step 2: Inside the TextField widget, add the decoration parameter and assign the InputDecoration widget. Step 3: Inside the InputDecoration widget, add the filled parameter and set it to true .
A TextField in Flutter is a basic input field that allows users to enter text. It is one of the most fundamental widgets in Flutter. Yet there is so much to do with it. For the purpose of this article, we will start with a basic Flutter app setup with nothing but a TextField.
When it is focused then the prefixText shows up and the hint disappears.Inside textField, consider putting an ontap method wherein you set the state so that the hint goes away and the prefix shows up. Because in some situation, when the textfield is tapped, the system still does not understand that is has focus unless something is actually typed.
Because in some situation, when the textfield is tapped, the system still does not understand that is has focus unless something is actually typed. The prefixIcon work around did not work for me, as my input is multiline.
Text-based prefixes are best when no wider than an icon. When they are wider than an icon, the animation and the x value of the floating placeholder is undefined. The animation could end up crossing the prefix or the placeholder could end up too far trailing if it pins to the trailing of the prefix for its floating x.
UPDATE: WE CAN NOW OVERRIDE THE MINIMUM PADDING OF prefixIcon
AND suffixIcon
WITH prefixIconConstraints
.
We can use prefixIcon
instead of prefixText
. Since prefixIcon
accepts any widget, we can use prefixIcon: Text("$")
.
Here is an example:
InputDecoration( isDense: true, prefixIcon:Text("\$"), prefixIconConstraints: BoxConstraints(minWidth: 0, minHeight: 0), ),
This is the perfect solution that worked when I have to display the prefix always visible
prefixIcon: Padding(padding: EdgeInsets.all(15), child: Text('+91 ')),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With