Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add M3 support for iconbuttons in error state in TextFields (#119925)
Browse files Browse the repository at this point in the history
* add m3 iconbutton override

* changes

* spring cleaning

* whitespace fix

* sneaky whitespaces
  • Loading branch information
esouthren authored Feb 3, 2023
1 parent e0b2138 commit c5e8757
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
58 changes: 38 additions & 20 deletions packages/flutter/lib/src/material/input_decorator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import 'package:flutter/widgets.dart';
import 'color_scheme.dart';
import 'colors.dart';
import 'constants.dart';
import 'icon_button.dart';
import 'icon_button_theme.dart';
import 'input_border.dart';
import 'material.dart';
import 'material_state.dart';
Expand Down Expand Up @@ -2307,19 +2309,27 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
cursor: SystemMouseCursors.basic,
child: ConstrainedBox(
constraints: decoration.prefixIconConstraints ??
themeData.visualDensity.effectiveConstraints(
const BoxConstraints(
minWidth: kMinInteractiveDimension,
minHeight: kMinInteractiveDimension,
),
themeData.visualDensity.effectiveConstraints(
const BoxConstraints(
minWidth: kMinInteractiveDimension,
minHeight: kMinInteractiveDimension,
),
),
child: IconTheme.merge(
data: IconThemeData(
color: _getPrefixIconColor(themeData, defaults),
size: iconSize,
),
child: Semantics(
child: decoration.prefixIcon,
child: IconButtonTheme(
data: IconButtonThemeData(
style: IconButton.styleFrom(
foregroundColor: _getPrefixIconColor(themeData, defaults),
iconSize: iconSize,
),
),
child: Semantics(
child: decoration.prefixIcon,
),
),
),
),
Expand All @@ -2334,24 +2344,32 @@ class _InputDecoratorState extends State<InputDecorator> with TickerProviderStat
cursor: SystemMouseCursors.basic,
child: ConstrainedBox(
constraints: decoration.suffixIconConstraints ??
themeData.visualDensity.effectiveConstraints(
const BoxConstraints(
minWidth: kMinInteractiveDimension,
minHeight: kMinInteractiveDimension,
),
themeData.visualDensity.effectiveConstraints(
const BoxConstraints(
minWidth: kMinInteractiveDimension,
minHeight: kMinInteractiveDimension,
),
child: IconTheme.merge(
data: IconThemeData(
color: _getSuffixIconColor(themeData, defaults),
size: iconSize,
),
child: Semantics(
child: decoration.suffixIcon,
child: IconTheme.merge(
data: IconThemeData(
color: _getSuffixIconColor(themeData, defaults),
size: iconSize,
),
child: IconButtonTheme(
data: IconButtonThemeData(
style: IconButton.styleFrom(
foregroundColor: _getSuffixIconColor(themeData, defaults),
iconSize: iconSize,
),
),
child: Semantics(
child: decoration.suffixIcon,
),
),
),
),
),
),
);
);

final Widget helperError = _HelperError(
textAlign: textAlign,
Expand Down
34 changes: 34 additions & 0 deletions packages/flutter/test/material/input_decorator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ double getOpacity(WidgetTester tester, String textValue) {
return opacityWidget.opacity.value;
}

TextStyle? getIconStyle(WidgetTester tester, IconData icon) {
final RichText iconRichText = tester.widget<RichText>(
find.descendant(of: find.byIcon(icon), matching: find.byType(RichText)),
);
return iconRichText.text.style;
}

void main() {
for(final bool useMaterial3 in <bool>[true, false]){
testWidgets('InputDecorator input/label text layout', (WidgetTester tester) async {
Expand Down Expand Up @@ -1732,6 +1739,33 @@ void main() {
expect(tester.widget<IconTheme>(find.widgetWithIcon(IconTheme,Icons.close).first).data.color, Colors.red);
});

testWidgets('InputDecorator suffixIconColor in M3 error state', (WidgetTester tester) async {
final ThemeData theme = ThemeData(
useMaterial3: true,
iconButtonTheme: const IconButtonThemeData(
style: ButtonStyle(
foregroundColor: MaterialStatePropertyAll<Color>(Colors.blue),
),
),
);
await tester.pumpWidget(
MaterialApp(
theme: theme,
home: Material(
child: TextField(
decoration: InputDecoration(
suffixIcon: IconButton(icon: const Icon(Icons.close), onPressed: () {}),
errorText: 'error state',
filled: true,
),
),
),
),
);

expect(getIconStyle(tester, Icons.close)?.color, theme.colorScheme.error);
});

testWidgets('InputDecorator prefix/suffix widgets', (WidgetTester tester) async {
const Key pKey = Key('p');
const Key sKey = Key('s');
Expand Down

0 comments on commit c5e8757

Please sign in to comment.