Fix OutlinedButton, TextButton, and IconButton throw exception when passing only one cursor to styleFrom (#125204)

fixes https://github.com/flutter/flutter/issues/118071
This commit is contained in:
Taha Tesser 2023-04-25 00:41:06 +03:00 committed by GitHub
parent b7332ef10a
commit 38c7c2fe93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 29 deletions

View File

@ -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),

View File

@ -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),

View File

@ -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,
@ -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;
}

View File

@ -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),
@ -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;
}

View File

@ -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),
@ -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;
}

View File

@ -1835,7 +1835,6 @@ void main() {
expect(controller.value, <MaterialState>{MaterialState.disabled});
expect(count, 1);
});
}
TextStyle _iconStyle(WidgetTester tester, IconData icon) {

View File

@ -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);
});
});
}

View File

@ -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) {

View File

@ -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) {