diff --git a/dev/tools/gen_defaults/lib/text_field_template.dart b/dev/tools/gen_defaults/lib/text_field_template.dart index 8bb06fc206..92da1336fb 100644 --- a/dev/tools/gen_defaults/lib/text_field_template.dart +++ b/dev/tools/gen_defaults/lib/text_field_template.dart @@ -12,5 +12,8 @@ class TextFieldTemplate extends TokenTemplate { // Generated version ${tokens["version"]} TextStyle _m3InputStyle(BuildContext context) => ${textStyle("md.comp.filled-text-field.label-text")}!; + +TextStyle _m3CounterErrorStyle(BuildContext context) => + ${textStyle("md.comp.filled-text-field.supporting-text")}!.copyWith(color:${componentColor('md.comp.filled-text-field.error.supporting-text')}); '''; } diff --git a/packages/flutter/lib/src/material/text_field.dart b/packages/flutter/lib/src/material/text_field.dart index ef4f3ce6ed..ccc4edc728 100644 --- a/packages/flutter/lib/src/material/text_field.dart +++ b/packages/flutter/lib/src/material/text_field.dart @@ -939,7 +939,7 @@ class _TextFieldState extends State with RestorationMixin implements return effectiveDecoration.copyWith( errorText: effectiveDecoration.errorText ?? '', counterStyle: effectiveDecoration.errorStyle - ?? themeData.textTheme.caption!.copyWith(color: themeData.errorColor), + ?? (themeData.useMaterial3 ? _m3CounterErrorStyle(context): _m2CounterErrorStyle(context)), counterText: counterText, semanticCounterText: semanticCounterText, ); @@ -1401,6 +1401,9 @@ class _TextFieldState extends State with RestorationMixin implements } } +TextStyle _m2CounterErrorStyle(BuildContext context) => + Theme.of(context).textTheme.caption!.copyWith(color: Theme.of(context).errorColor); + // BEGIN GENERATED TOKEN PROPERTIES - TextField // Do not edit by hand. The code between the "BEGIN GENERATED" and @@ -1414,4 +1417,7 @@ class _TextFieldState extends State with RestorationMixin implements TextStyle _m3InputStyle(BuildContext context) => Theme.of(context).textTheme.bodyLarge!; +TextStyle _m3CounterErrorStyle(BuildContext context) => + Theme.of(context).textTheme.bodySmall!.copyWith(color:Theme.of(context).colorScheme.error); + // END GENERATED TOKEN PROPERTIES - TextField diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index 0348885493..b00b52ec76 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -94,8 +94,9 @@ Widget overlayWithEntry(OverlayEntry entry) { ); } -Widget boilerplate({ required Widget child }) { +Widget boilerplate({ required Widget child, ThemeData? theme }) { return MaterialApp( + theme: theme, home: Localizations( locale: const Locale('en', 'US'), delegates: >[ @@ -4655,6 +4656,38 @@ void main() { expect(counterTextWidget.style!.color, isNot(equals(Colors.deepPurpleAccent))); }); + testWidgets('maxLength shows warning in Material 3', (WidgetTester tester) async { + final TextEditingController textController = TextEditingController(); + final ThemeData theme = ThemeData.from( + colorScheme: const ColorScheme.light().copyWith(error: Colors.deepPurpleAccent), + useMaterial3: true, + ); + await tester.pumpWidget(boilerplate( + theme: theme, + child: TextField( + controller: textController, + maxLength: 10, + maxLengthEnforcement: MaxLengthEnforcement.none, + ), + )); + + await tester.enterText(find.byType(TextField), '0123456789101112'); + await tester.pump(); + + expect(textController.text, '0123456789101112'); + expect(find.text('16/10'), findsOneWidget); + Text counterTextWidget = tester.widget(find.text('16/10')); + expect(counterTextWidget.style!.color, equals(Colors.deepPurpleAccent)); + + await tester.enterText(find.byType(TextField), '0123456789'); + await tester.pump(); + + expect(textController.text, '0123456789'); + expect(find.text('10/10'), findsOneWidget); + counterTextWidget = tester.widget(find.text('10/10')); + expect(counterTextWidget.style!.color, isNot(equals(Colors.deepPurpleAccent))); + }); + testWidgets('maxLength shows warning when maxLengthEnforcement.none with surrogate pairs.', (WidgetTester tester) async { final TextEditingController textController = TextEditingController(); const TextStyle testStyle = TextStyle(color: Colors.deepPurpleAccent);