TextField in Flutter is a text input widget most commonly used to collect user input. We can input either from a hardware keyboard or an on-screen keyboard. We can use the TextField widget in different forms, like building forms, sending messages, creating search experiences, and many more. Flutter TextField allows different kinds of customizations like styling and alignment of the text as well as the cursor inside the TextField.
Adding borders to TextField in Flutter is also a major concern of developers while building an app. Delivering the best user experience to end-users is always the utmost priority of developers as well as enterprises. To accomplish that, you need to value several aspects of building a user-friendly app.
Understanding TextField and InputDecoration
To effectively customize the Flutter textfield border, it’s crucial to understand how InputDecoration works. The InputDecoration
widget provides a consistent way to manage the visual presentation of input fields. Within it, you can control different borders:
-
border
-
enabledBorder
-
focusedBorder
-
errorBorder
-
disabledBorder
Each of these parameters takes an InputBorder object, such as OutlineInputBorder
, UnderlineInputBorder
, or InputBorder.none
to determine how the border should appear.
Default Behavior of TextField in Flutter
By default, a TextField or TextFormField comes with a flutter textfield border that is either underlined or outlined depending on the material theme. However, this can be overridden to match any UI/UX specification.
Example of a default TextField:
TextField( decoration: InputDecoration( hintText: 'Enter text here', ), ),
To customize this, we need to replace or style the InputDecoration.
Adding borders to TextField in Flutter
You can add borders to a TextField in Flutter to make your UI more interactive. Making every field prominent that is present on your screen is the only way to make your app user-friendly. A TextField in any Flutter app usually requires a hint text and a label. Let’s move towards the code to add borders to TextField in Flutter.
TextField( decoration: InputDecoration( labelText: 'Username', hintText: 'Enter your username', border: OutlineInputBorder( borderRadius: BorderRadius.circular(10.0), borderSide: BorderSide( color: Colors.blue, width: 2.0, ), ), ), ),
Explanation:
-
labelText
Displays a label above the input field. -
hintText
Provides a placeholder hint inside the field. -
border
Applies a visible border around the TextField usingOutlineInputBorder
. -
borderRadius
Adds rounded corners for a softer UI. -
borderSide
Defines the color and width of the border.
This structure enhances the visibility of input fields, supporting a more user-friendly Flutter UI.
How to Change TextField Border Color in Flutter
Also, you can change the border color of a TextField
using the InputDecoration
class with the OutlineInputBorder
. Here’s how you can do it:
TextFormField(
decoration: InputDecoration(
label: const Text('Name'),
hintText: 'Enter your name',
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.grey),
borderRadius: BorderRadius.circular(10),
),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.black,
width: 2,
),
borderRadius: BorderRadius.circular(15),
),
errorBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.red,
width: 2,
),
borderRadius: BorderRadius.circular(10),
),
),
),
Screenshots of the Output
As you can see, we have declared the label “Name.” The text field shows “Name” as the label. Additionally, we defined the border color as gray and its circular radius as “10.” All the results are displayed accordingly in the code.
Related Post – How to change text color in flutter?
Furthermore, we declared the hint text as “Enter your name”. We declared its color as black, width as “2” and circular radius as 15. As you can see in the screenshot above, the label moves upward, and the hint text displays. It has a border around it with an increased width and circular radius representing what to write in the field.
Moreover, if there is any error in the TextField Flutter, the border will change its color to red.
Responsive Border Radius Based on Screen Size
For applications with adaptive UIs, you may want the flutter textfield border radius to scale based on screen dimensions.
MediaQueryData mediaQuery = MediaQuery.of(context); double radius = mediaQuery.size.width * 0.02; TextField( decoration: InputDecoration( border: OutlineInputBorder( borderRadius: BorderRadius.circular(radius), ), ), ),
This technique enhances responsiveness and ensures a uniform look across multiple screen sizes and orientations.
Conclusion
Customizing the flutter textfield border provides fine-grained control over the appearance of input elements. Whether you’re adjusting the textfield border radius Flutter, modifying colors dynamically, or styling with gradients, Flutter’s flexible widget system allows deep customization of borders in TextField and TextFormField components.
By mastering border customization in Flutter, including how to change TextField border color in Flutter and apply radius modifications, you can design interfaces that are both functional and visually appealing.
We hope this blog helps you one way or the other. Furthermore, if you have any questions or suggestions, you can leave a comment below. Also, you can hire a flutter developer to discuss your ideas related to flutter app development.