Skip to content

Commit

Permalink
Fix OutlinedButton, TextButton, and IconButton throw exception …
Browse files Browse the repository at this point in the history
…when passing only one cursor to `styleFrom` (#125204)

fixes flutter/flutter#118071
  • Loading branch information
TahaTesser authored Apr 24, 2023
1 parent b7332ef commit 38c7c2f
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 29 deletions.
4 changes: 1 addition & 3 deletions packages/flutter/lib/src/material/elevated_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ class ElevatedButton extends ButtonStyleButton {
final MaterialStateProperty<double>? elevationValue = (elevation == null)
? null
: _ElevatedButtonDefaultElevation(elevation);
final MaterialStateProperty<MouseCursor?>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _ElevatedButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);
final MaterialStateProperty<MouseCursor?> mouseCursor = _ElevatedButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);

return ButtonStyle(
textStyle: MaterialStatePropertyAll<TextStyle?>(textStyle),
Expand Down
5 changes: 1 addition & 4 deletions packages/flutter/lib/src/material/filled_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ class FilledButton extends ButtonStyleButton {
final MaterialStateProperty<Color?>? overlayColor = (foreground == null)
? null
: _FilledButtonDefaultOverlay(foreground);
final MaterialStateProperty<MouseCursor?>? mouseCursor =
(enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _FilledButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);
final MaterialStateProperty<MouseCursor?> mouseCursor = _FilledButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);

return ButtonStyle(
textStyle: MaterialStatePropertyAll<TextStyle?>(textStyle),
Expand Down
12 changes: 5 additions & 7 deletions packages/flutter/lib/src/material/icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,7 @@ class IconButton extends StatelessWidget {
final MaterialStateProperty<Color?>? overlayColor = (foregroundColor == null && hoverColor == null && focusColor == null && highlightColor == null)
? null
: _IconButtonDefaultOverlay(foregroundColor, focusColor, hoverColor, highlightColor);
final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _IconButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);
final MaterialStateProperty<MouseCursor?> mouseCursor = _IconButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);

return ButtonStyle(
backgroundColor: buttonBackgroundColor,
Expand Down Expand Up @@ -1061,14 +1059,14 @@ class _IconButtonDefaultOverlay extends MaterialStateProperty<Color?> {
}

@immutable
class _IconButtonDefaultMouseCursor extends MaterialStateProperty<MouseCursor> with Diagnosticable {
class _IconButtonDefaultMouseCursor extends MaterialStateProperty<MouseCursor?> with Diagnosticable {
_IconButtonDefaultMouseCursor(this.enabledCursor, this.disabledCursor);

final MouseCursor enabledCursor;
final MouseCursor disabledCursor;
final MouseCursor? enabledCursor;
final MouseCursor? disabledCursor;

@override
MouseCursor resolve(Set<MaterialState> states) {
MouseCursor? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return disabledCursor;
}
Expand Down
12 changes: 5 additions & 7 deletions packages/flutter/lib/src/material/outlined_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ class OutlinedButton extends ButtonStyleButton {
final MaterialStateProperty<Color?>? overlayColor = (foreground == null)
? null
: _OutlinedButtonDefaultOverlay(foreground);
final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _OutlinedButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);
final MaterialStateProperty<MouseCursor?> mouseCursor = _OutlinedButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);

return ButtonStyle(
textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle),
Expand Down Expand Up @@ -395,14 +393,14 @@ class _OutlinedButtonDefaultOverlay extends MaterialStateProperty<Color?> with D
}

@immutable
class _OutlinedButtonDefaultMouseCursor extends MaterialStateProperty<MouseCursor> with Diagnosticable {
class _OutlinedButtonDefaultMouseCursor extends MaterialStateProperty<MouseCursor?> with Diagnosticable {
_OutlinedButtonDefaultMouseCursor(this.enabledCursor, this.disabledCursor);

final MouseCursor enabledCursor;
final MouseCursor disabledCursor;
final MouseCursor? enabledCursor;
final MouseCursor? disabledCursor;

@override
MouseCursor resolve(Set<MaterialState> states) {
MouseCursor? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return disabledCursor;
}
Expand Down
12 changes: 5 additions & 7 deletions packages/flutter/lib/src/material/text_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ class TextButton extends ButtonStyleButton {
: disabledIconColor == null
? ButtonStyleButton.allOrNull<Color?>(iconColor)
: _TextButtonDefaultIconColor(iconColor, disabledIconColor);
final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null
: _TextButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);
final MaterialStateProperty<MouseCursor?> mouseCursor = _TextButtonDefaultMouseCursor(enabledMouseCursor, disabledMouseCursor);

return ButtonStyle(
textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle),
Expand Down Expand Up @@ -456,14 +454,14 @@ class _TextButtonDefaultIconColor extends MaterialStateProperty<Color?> {
}

@immutable
class _TextButtonDefaultMouseCursor extends MaterialStateProperty<MouseCursor> with Diagnosticable {
class _TextButtonDefaultMouseCursor extends MaterialStateProperty<MouseCursor?> with Diagnosticable {
_TextButtonDefaultMouseCursor(this.enabledCursor, this.disabledCursor);

final MouseCursor enabledCursor;
final MouseCursor disabledCursor;
final MouseCursor? enabledCursor;
final MouseCursor? disabledCursor;

@override
MouseCursor resolve(Set<MaterialState> states) {
MouseCursor? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return disabledCursor;
}
Expand Down
1 change: 0 additions & 1 deletion packages/flutter/test/material/elevated_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,6 @@ void main() {
expect(controller.value, <MaterialState>{MaterialState.disabled});
expect(count, 1);
});

}

TextStyle _iconStyle(WidgetTester tester, IconData icon) {
Expand Down
20 changes: 20 additions & 0 deletions packages/flutter/test/material/icon_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,26 @@ void main() {
testWidgets('black87 icon color defined by users shows correctly in Material3', (WidgetTester tester) async {

});

testWidgets("IconButton.styleFrom doesn't throw exception on passing only one cursor", (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/118071.
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Material(
child: IconButton(
style: OutlinedButton.styleFrom(
enabledMouseCursor: SystemMouseCursors.text,
),
onPressed: () {},
icon: const Icon(Icons.add),
),
),
),
);

expect(tester.takeException(), isNull);
});
});
}

Expand Down
18 changes: 18 additions & 0 deletions packages/flutter/test/material/outlined_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,24 @@ void main() {
expect(controller.value, <MaterialState>{MaterialState.disabled});
expect(count, 1);
});

testWidgets("OutlinedButton.styleFrom doesn't throw exception on passing only one cursor", (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/118071.
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
enabledMouseCursor: SystemMouseCursors.text,
),
onPressed: () {},
child: const Text('button'),
),
),
);

expect(tester.takeException(), isNull);
});
}

TextStyle _iconStyle(WidgetTester tester, IconData icon) {
Expand Down
18 changes: 18 additions & 0 deletions packages/flutter/test/material/text_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,24 @@ void main() {
expect(material.textStyle!.color, colorScheme.onSurface.withOpacity(0.38));
expect(iconColor(), equals(Colors.blue));
});

testWidgets("TextButton.styleFrom doesn't throw exception on passing only one cursor", (WidgetTester tester) async {
// This is a regression test for https://github.com/flutter/flutter/issues/118071.
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: TextButton(
style: TextButton.styleFrom(
enabledMouseCursor: SystemMouseCursors.text,
),
onPressed: () {},
child: const Text('button'),
),
),
);

expect(tester.takeException(), isNull);
});
}

TextStyle? _iconStyle(WidgetTester tester, IconData icon) {
Expand Down

0 comments on commit 38c7c2f

Please sign in to comment.