From cd06ba7ab6215616e994745f445b7c3edc9c2dbd Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Thu, 18 Jan 2024 09:37:06 -0800 Subject: [PATCH] Fix: TextField can inherit `errorStyle` from `InputDecorationTheme`. (#141227) Previously `TextField`s error `cursorColor` was being derived without taking into account any `InputDecorationTheme` defaults. This change respects `InputDecorationTheme` defaults when deriving the error `cursorColor`. Fixes #140607 --- .../flutter/lib/src/material/text_field.dart | 2 +- .../test/material/text_field_test.dart | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index e1942cd7f97..fb3b0bdf433 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -1000,7 +1000,7 @@ class _TextFieldState extends State with RestorationMixin implements bool get _hasError => widget.decoration?.errorText != null || widget.decoration?.error != null || _hasIntrinsicError; - Color get _errorColor => widget.cursorErrorColor ?? widget.decoration?.errorStyle?.color ?? Theme.of(context).colorScheme.error; + Color get _errorColor => widget.cursorErrorColor ?? _getEffectiveDecoration().errorStyle?.color ?? Theme.of(context).colorScheme.error; InputDecoration _getEffectiveDecoration() { final MaterialLocalizations localizations = MaterialLocalizations.of(context); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index e3f7de6fcf8..79323b47b21 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -14966,6 +14966,31 @@ void main() { expect(tester.getTopLeft(find.text('Label')).dy, 12.0); }); + // Regression test for https://github.com/flutter/flutter/issues/140607. + testWidgets('TextFields can inherit errorStyle color from InputDecorationTheme.', (WidgetTester tester) async { + Widget textFieldBuilder() { + return MaterialApp( + theme: ThemeData( + inputDecorationTheme: const InputDecorationTheme( + errorStyle: TextStyle(color: Colors.green), + ), + ), + home: const Scaffold( + body: TextField( + decoration: InputDecoration( + errorText: 'error', + ), + ), + ), + ); + } + + await tester.pumpWidget(textFieldBuilder()); + await tester.pumpAndSettle(); + final EditableTextState state = tester.state(find.byType(EditableText)); + expect(state.widget.cursorColor, Colors.green); + }); + group('MaxLengthEnforcement', () { const int maxLength = 5;