TextField in Flutter is the Most Commonly Used
It is a GUI control element that enables the user to enter text information by utilizing a programmable code.
TextField in Flutter is the most commonly used text input widget that allows users to collect inputs from the keyboard into an app.
We can use the TextField widget in many ways: building forms, sending messages, creating search experiences, and many more.
By default, Flutter decorates the TextField with an underline. Using an InputDecoration as the decoration, we can also add several attributes to TextField, such as a label, icon, inline hint text, and error text.
Syntax
TextField (
decoration: InputDecoration(
border: InputBorder.none,
labelText: 'Enter Name',
hintText: 'Enter Your Name'
),
);
Syntax
TextField(
obscureText: true, // This function is used to password hide and show.
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
hintText: 'Enter Password',
),
),
How to retrieve the value of a TextField?
Flutter allows the user to retrieve the text in mainly two ways: the first is the onChanged method, and the other is the controller method.
1)The onChanged method is the easiest way to retrieve the text field value.
This method stores the current value in a simple variable and then uses it in the TextField widget.
Syntax
Ex String value = "";
TextField(
onChanged: (text) {
value = text;
},
)
2)The TextEditingController method is a popular method to retrieve text field values using TextEditingController.
It will be attached to the TextField widget and then listen to changes and control the widget's text value.
Ex TextEditingController mycontroller =TextEditingController();
TextField(
controller: mycontroller,
)
Comments
Post a Comment