InputDecoration , how many border type are there?
- User
InputDecoration , how many border type are there?
- Assistant
In Flutter, the
InputDecorationclass is used to styleTextFieldandTextFormFieldwidgets. TheInputDecorationclass provides several properties to customise the appearance of the input field, including various border types.There are five types of borders you can specify in
InputDecoration. Each border type corresponds to a different state of theTextField:-
enabledBorder:- The border to display when the
TextFieldis enabled and not focused.
- The border to display when the
-
focusedBorder:- The border to display when the
TextFieldis focused.
- The border to display when the
-
errorBorder:- The border to display when the
TextFieldhas an error and is not focused.
- The border to display when the
-
focusedErrorBorder:- The border to display when the
TextFieldhas an error and is focused.
- The border to display when the
-
disabledBorder:- The border to display when the
TextFieldis disabled.
- The border to display when the
Here is an example demonstrating how to use these border types in an
InputDecoration:TextField( decoration: InputDecoration( labelText: 'Enter text', // Border when the TextField is enabled and not focused enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.grey, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), // Border when the TextField is focused focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.blue, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), // Border when the TextField has an error and is not focused errorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.red, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), // Border when the TextField has an error and is focused focusedErrorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.redAccent, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), // Border when the TextField is disabled disabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.grey.shade400, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), ), )Border Types
In addition to specifying different borders for different states, Flutter also provides several pre-defined border types that you can use for these properties. Here are the most commonly used border types:
UnderlineInputBorder:- Displays a simple underline below the text field.
UnderlineInputBorder( borderSide: BorderSide(color: Colors.blue), )OutlineInputBorder:- Displays a rectangular border around the text field.
OutlineInputBorder( borderSide: BorderSide(color: Colors.blue), borderRadius: BorderRadius.circular(10.0), )StadiumBorder:- Displays a stadium-shaped (pill-shaped) border around the text field.
StadiumBorder( side: BorderSide(color: Colors.blue), )RoundedRectangleBorder:- Displays a border with rounded corners around the text field.
RoundedRectangleBorder( side: BorderSide(color: Colors.blue), borderRadius: BorderRadius.circular(10.0), )InputBorder.none:- Displays no border.
InputBorder.none
You can use these border types for any of the five border properties (
enabledBorder,focusedBorder,errorBorder,focusedErrorBorder, anddisabledBorder) in theInputDecoration.Complete Example with Different Border Types
Here is a complete example that demonstrates using different border types for each state:
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( home: Scaffold( appBar: AppBar(title: const Text('InputDecoration Border Example')), body: const Padding( padding: EdgeInsets.all(16.0), child: MyTextField(), ), ), ); } } class MyTextField extends StatelessWidget { const MyTextField({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return TextField( decoration: InputDecoration( labelText: 'Enter text', enabledBorder: const UnderlineInputBorder( borderSide: BorderSide(color: Colors.grey), ), focusedBorder: OutlineInputBorder( borderSide: const BorderSide(color: Colors.blue, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), errorBorder: const StadiumBorder( side: BorderSide(color: Colors.red, width: 2.0), ), focusedErrorBorder: RoundedRectangleBorder( side: const BorderSide(color: Colors.redAccent, width: 2.0), borderRadius: BorderRadius.circular(10.0), ), disabledBorder: InputBorder.none, ), ); } }In this example:
- enabledBorder uses
UnderlineInputBorder. - focusedBorder uses
OutlineInputBorder. - errorBorder uses
StadiumBorder. - focusedErrorBorder uses
RoundedRectangleBorder. - disabledBorder uses
InputBorder.none.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.