07-21-2023, 09:43 PM
### Update
As mentioned by [Guilherme][1] in the comment, the later version of Flutter uses different logic to get the color
```dart
Color _getActiveColor(ThemeData themeData) {
if (isFocused) {
return themeData.colorScheme.primary;
}
return themeData.hintColor;
}
```
[Source][2]
You will now need to set it from the `colorScheme` instead
```dart
ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
primary: activeColor,
),
)
```
---
### Original answer
After digging the source code for the `InputDecorator` used to determine the label color, here's what I found.
```dart
TextStyle _getFloatingLabelStyle(ThemeData themeData) {
final Color color = decoration.errorText != null
? decoration.errorStyle?.color ?? themeData.errorColor
: _getActiveColor(themeData);
final TextStyle style = themeData.textTheme.subtitle1.merge(widget.baseStyle);
return style
.copyWith(color: decoration.enabled ? color : themeData.disabledColor)
.merge(decoration.labelStyle);
}
Color _getActiveColor(ThemeData themeData) {
if (isFocused) {
switch (themeData.brightness) {
case Brightness.dark:
return themeData.accentColor;
case Brightness.light:
return themeData.primaryColor;
}
}
return themeData.hintColor;
}
```
In short, to change the label color, set the `primaryColor` light theme or `accentColor` for dark theme.
Another tip: To change the label color while it's not focused, set `hintColor`.
```dart
ThemeData.dark().copyWith(
primaryColor: Colors.red,
accentColor: Colors.white,
hintColor: Colors.pink,
)
```
[1]:
As mentioned by [Guilherme][1] in the comment, the later version of Flutter uses different logic to get the color
```dart
Color _getActiveColor(ThemeData themeData) {
if (isFocused) {
return themeData.colorScheme.primary;
}
return themeData.hintColor;
}
```
[Source][2]
You will now need to set it from the `colorScheme` instead
```dart
ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
primary: activeColor,
),
)
```
---
### Original answer
After digging the source code for the `InputDecorator` used to determine the label color, here's what I found.
```dart
TextStyle _getFloatingLabelStyle(ThemeData themeData) {
final Color color = decoration.errorText != null
? decoration.errorStyle?.color ?? themeData.errorColor
: _getActiveColor(themeData);
final TextStyle style = themeData.textTheme.subtitle1.merge(widget.baseStyle);
return style
.copyWith(color: decoration.enabled ? color : themeData.disabledColor)
.merge(decoration.labelStyle);
}
Color _getActiveColor(ThemeData themeData) {
if (isFocused) {
switch (themeData.brightness) {
case Brightness.dark:
return themeData.accentColor;
case Brightness.light:
return themeData.primaryColor;
}
}
return themeData.hintColor;
}
```
In short, to change the label color, set the `primaryColor` light theme or `accentColor` for dark theme.
Another tip: To change the label color while it's not focused, set `hintColor`.
```dart
ThemeData.dark().copyWith(
primaryColor: Colors.red,
accentColor: Colors.white,
hintColor: Colors.pink,
)
```
[1]:
[To see links please register here]
[2]:[To see links please register here]