I want to apply a theme in my Flutter app on the disabled text fields' label because the grey color I have right now is quite hard to read.
I'd like to apply it to my entire app, so I'd like to use theming, however, I didn't find any solution that would enable my to customize the label's text style only if the text form field is disabled
How can I theme and set globally the color the disabled text form field's label in Flutter?
I know how to change the label's text style conditionally, however, I need to remember to always use the same style (or I could wrap the widget, but that sounds also suboptimal). I can customize the label's color via the decoration
named parameter, like so:
TextFormField(
enabled: isEnabled,
decoration: InputDecoration(
labelText: 'Value',
labelStyle: TextStyle(color: isEnabled ? Colors.green : Colors.red),
),
// .... other fields, like controller might come here
),
You can use a InputDecorationTheme .
MaterialApp has a property theme
where you can set a custom ThemeData.
ThemeData has a property inputDecorationTheme
where you can set a InputDecorationTheme .
And InputDecorationTheme has a lot of properties that you can use to customize your text fields.
MaterialApp(
theme: ThemeData(
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(
vertical: 22,
horizontal: 26,
),
labelStyle: TextStyle(
fontSize: 35,
decorationColor: Colors.red,
),
),
)
You could use Theme to wrap around your widget set the property disabledColor.
Example: Demo
final customText = Theme(
data: ThemeData(
disabledColor: Colors.blue,
),
child: TextFormField(
enabled: false,
decoration: const InputDecoration(
icon: Icon(Icons.person),
hintText: 'What do people call you?',
labelText: 'Name *',
),
),
);
or Globally
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
disabledColor: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With