Copy any previous IconThemeData instead of overwriting it in CupertinoButton (#149777)

This changes how the `IconThemeData` is passed to the child of `CupertinoButton`.

Previously, it would derive the foreground color from the current theme and create a new `IconThemeData` object.
This causes any `IconThemeData` to be overwritten.

This change uses `IconTheme.of` to get the currently applied icon theme and only overwrites the properties provided by the `CupertinoButton` instead of the whole object.

Fixes #149172.
This commit is contained in:
Ricardo Boss
2024-06-27 05:53:58 +02:00
committed by GitHub
parent e4c18b702a
commit 805d79607e
2 changed files with 34 additions and 1 deletions

View File

@@ -32,6 +32,10 @@ const double _kCupertinoFocusColorSaturation = 0.835;
/// [EdgeInsets.zero], should be used to prevent clipping larger [child]
/// widgets.
///
/// Preserves any parent [IconThemeData] but overwrites its [IconThemeData.color]
/// with the [CupertinoThemeData.primaryColor] (or
/// [CupertinoThemeData.primaryContrastingColor] if the button is disabled).
///
/// {@tool dartpad}
/// This sample shows produces an enabled and disabled [CupertinoButton] and
/// [CupertinoButton.filled].
@@ -291,6 +295,7 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
.toColor();
final TextStyle textStyle = themeData.textTheme.textStyle.copyWith(color: foregroundColor);
final IconThemeData iconTheme = IconTheme.of(context).copyWith(color: foregroundColor);
return MouseRegion(
cursor: enabled && kIsWeb ? SystemMouseCursors.click : MouseCursor.defer,
@@ -344,7 +349,7 @@ class _CupertinoButtonState extends State<CupertinoButton> with SingleTickerProv
child: DefaultTextStyle(
style: textStyle,
child: IconTheme(
data: IconThemeData(color: foregroundColor),
data: iconTheme,
child: widget.child,
),
),

View File

@@ -608,6 +608,34 @@ void main() {
expect(focused, isFalse);
expect(focusNode.hasFocus, isFalse);
});
testWidgets('IconThemeData is not replaced by CupertinoButton', (WidgetTester tester) async {
const IconThemeData givenIconTheme = IconThemeData(size: 12.0);
IconThemeData? actualIconTheme;
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: IconTheme(
data: givenIconTheme,
child: CupertinoButton(
onPressed: () {},
child: Builder(
builder: (BuildContext context) {
actualIconTheme = IconTheme.of(context);
return const Placeholder();
}
),
),
),
),
),
);
expect(actualIconTheme?.size, givenIconTheme.size);
});
}
Widget boilerplate({ required Widget child }) {