Let’s talk about flutter textfield in flutter app, but first if you are new and want a complete setup and want to build your first app instantly, then click here, now let’s jump into text field widget in flutter app, what it really is, its role, and usage in flutter app. So let’s get right into flutter text field widget.
Introduction
Text field is used to input values from user in the flutter app, it can be an integer, string etc. Textfield in flutter app is used if the user want to input some data e.g. in the login screen where the user is required to input email or password or in the registration screen where a new user is added based on the data that is inputted using the textfields. Let’s now directly jump into textfield implementation.
Flutter Text Field
Flutter Text Field Implementation
Scaffold( body: Center( child: TextField(), ), )

Input Text Style
style: TextStyle( color: Colors.blue.shade900, fontSize: 20 )

Input Decoration
TextField( decoration: InputDecoration(), )
Flutter Textfield Hint Text
decoration: InputDecoration( hintText: 'Enter something' )
Using the hint text constructor of the input decoration class, we can give a hint message to the user about what to fill in that specific field or any text we want, whenever the user starts inputting value in that textfield then that hint text disappears and if the user remove all the inputted value then this hint text will again be visible.
Flutter Textfield Hint Style
hintStyle: TextStyle(color: Colors.grey, fontSize: 15)
We can use the hint style constructor of the input decoration class to style our hint text. As you can see above, we have specified the hint text color to grey and font size to 15. You can change other properties like font weight, style etc.
Flutter Textfield Fill Color and Filled
fillColor: Colors.blue.shade200, filled: true,

Note

Flutter Textfield Prefix Icon
prefixIcon: Icon(Icons.email)

Flutter Textfield Prefix Icon Constraints
prefixIconConstraints: BoxConstraints(maxHeight: 30, maxWidth: 30)

Flutter Textfield Suffix Icon and Constraints
suffixIcon: Icon(Icons.visibility_off), suffixIconConstraints: BoxConstraints(maxHeight: 30, maxWidth: 30)

Flutter Textfield Prefix
prefix: Icon( Icons.email, color: Colors.grey, size: 14, )

It is different from the prefix icon, the constructor prefix takes a widget and is only visible when the user taps on the text field or starts typing. When the text field is focused, we can see the prefix. As you can see in the image above, the small email icon after the prefix icon is the prefix.
Flutter Textfield Border
border: OutlineInputBorder( borderRadius: BorderRadius.circular(50), borderSide: BorderSide(color: Colors.grey) )

Flutter Textfield Auto Focus
autofocus: true

Source Code
Source code for this text field is given below:
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Text Field', debugShowCheckedModeBanner: false, home: HomePage(), ); } } class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @override State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return SafeArea( child: Scaffold( body: Center( child: Padding( padding: EdgeInsets.symmetric(horizontal: 20), child: TextField( autofocus: true, style: TextStyle(color: Colors.blue.shade900, fontSize: 20), decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(50), borderSide: BorderSide(color: Colors.grey)), prefixIcon: Icon(Icons.email), suffixIcon: Icon(Icons.visibility_off), hintText: 'Enter your email', hintStyle: TextStyle(color: Colors.grey, fontSize: 15)), ), ), ), ), ); } }
Note:
That’s all for this article, hope you enjoyed it and have learnt a lot from it. Implement it in your code and share your experience with us. We would love to see how you have used it in your flutter app. We would be looking forward for your response. Hope to see you in our next articles in which we will dig deep into other amazing widgets. Thanks you.