The TextField and TextFormField widgets in Flutter are the most used widgets. It is used to get user input in a variety of forms such as email, password, phone, home address, etc. But after adding the default TextField/TextFormField, sometimes you might need to customize the border of TextField/TextFormField. So in this tutorial, we will see how to add and customize TextField/TextFormField border in Flutter.
How to add border to a TextField or TextFormField in Flutter
To add a border to a TextField/TextFormField in Flutter, you can specify the decoration property and then use the InputDecoration and OutlineInputBorder widget. The OutlineInputBorder widget has the borderSide widget which you can use to pass in the BorderSide widget with width and color parameter to create the border.
Here are step by step instructions the to add border to textfield/textformfield in Flutter:
- Locate the file where you have placed the
TextField/TextFormField
widget. - Inside the
TextField/TextFormField
widget, add thedecoration
parameter and assign theInputDecoration
widget. - Inside the
InputDecoration
widget, add theenabledBorder
parameter and assign theOutlineInputBorder
widget. - Inside the
OutlineInputBorder
, add theBorderSide
widget withcolor
parameter and set the color of your choice.
Code Example:
const TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
width: 4, color: Colors.green), //<-- SEE HERE
),
),
)
DartHow to add border radius or rounded border to TextField or TextFormField
To add border radius or create rounded border around the TextField/TextFormField widget, add the decoration property and then use OutlineInputBorder widget. The OutlineInputBorder widget accepts the borderRadius parameter. You can use the borderRadius parameter with BorderRadius.circular(50.0) to create the cricular border around the TextField.
Code Example:
TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(width: 4, color: Colors.green), //<-- SEE HERE
borderRadius: BorderRadius.circular(20.0),
),
),
)
DartDisplay TextField or TextFormField error border
To display TextField/TextFormField
error border, you can add the decoration
parameter inside the TextField widget and then assign the InputDecoration
widget. Inside the InputDecoration, you can add border for the error state by specifying errorBorder
the parameter. You can assign the OutlineInputBorder
widget and add border as usual.
Code Example:
const TextField(
decoration: InputDecoration(
errorBorder: OutlineInputBorder( //<-- SEE HERE
borderSide: BorderSide(
width: 3, color: Colors.redAccent),
),
),
)
DartDisplay TextField or TextFormField focus border
To display TextField/TextFormField
error border, you can add the decoration
parameter inside the TextField widget and then assign the InputDecoration
widget. Inside the InputDecoration, you can add border for the error state by specifying focusedBorder
the parameter. You can assign the OutlineInputBorder
widget and add border as usual.
Code Example:
const TextField(
decoration: InputDecoration(
focusedBorder: OutlineInputBorder( //<-- SEE HERE
borderSide: BorderSide(
width: 3, color: Colors.blueAccent),
),
),
)
DartShow only the bottom border in TextField or TextFormField
To show only the bottom border in TextField/TextFormField, you can specify the decoration property and then use the InputDecoration and UnderlineInputBorder widget. The UnderlineInputBorder widget accepts the borderSide widget which you can use to create the border.
Code Example:
const TextField(
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder( //<-- SEE HERE
borderSide: BorderSide(
width: 3, color: Colors.greenAccent),
),
),
)
DartHow to change TextField or TextFormField border width or size
To change the TextField/TextFormField border width, first add the border as usual and then simply modify the width value inside the BorderSide parameter.
Code Example:
TextField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide:
BorderSide(
width: 5, //<-- SEE HERE
color: Colors.greenAccent,),
borderRadius: BorderRadius.circular(50.0),
),
),
),
//----------
const TextField(
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
width: 3, //<-- SEE HERE
color: Colors.greenAccent,
),
),
),
)
DartCustomize TextField or TextFormField border globally
In the previous section, we saw how to customize the TextField/TextFormField border at the page level. but sometimes you might be looking to have a common style across all the pages of your app. In that case, you might want to customize the TextField/TextFormField border color at the app level.
You can change the TextField/TextFormField border color globally by defining the inputDecorationTheme and then adding the OutlineInputBorder widget. Inside the OutlineInputBorder widget, you can specify which type of border you want to change. for example, enabledBorder
, focusedBorder
, and so on, and then assign the color.
Here’s how you do it:
- Locate the
MaterialApp
widget. - Inside the MaterialApp, add the
theme
parameter withThemeData
class assigned. - Inside the
ThemeData
add theinputDecorationTheme
parameter and then assign theInputDecorationTheme
. - Inside the
InputDecorationTheme
add theenabledBorder
parameter and then assign theOutlineInputBorder
. - Inside the
OutlineInputBorder
add theborderSide
parameter and then assign theBorderSide
withcolor
of your choice.
Code Example:
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
inputDecorationTheme: const InputDecorationTheme(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(width: 3, color: Colors.greenAccent),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(width: 3, color: Colors.amberAccent),
),
),
),
home: ChangeTextFieldBorderColorDemo(),
);
Dart