Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a text field with a prefix that is always visible in flutter

Tags:

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.

like image 597
Kevin Nzioka Avatar asked Nov 12 '19 13:11

Kevin Nzioka


People also ask

What is prefix icon?

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 do I use TextFormField in flutter?

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.

How do I change the background color of a TextField in flutter?

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 .

What is a textfield in flutter?

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.

How to make the prefix textfield show up when textfield is focused?

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.

Why can't I use the prefix icon in text fields?

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.

When should I use text-based prefixes?

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.


2 Answers

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), ), 
like image 141
Uni Avatar answered Oct 06 '22 23:10

Uni


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 ')), 
like image 22
Jiten Basnet Avatar answered Oct 07 '22 00:10

Jiten Basnet