From 58407234f5cb0e9278af6594f4bb5e154198692a Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Fri, 4 Feb 2022 06:40:13 +0200 Subject: [PATCH] Fix Local `RadioTheme` not being inherited by `Radio` Widget (#97713) --- packages/flutter/lib/src/material/radio.dart | 22 +++++++------ .../test/material/radio_theme_test.dart | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/material/radio.dart b/packages/flutter/lib/src/material/radio.dart index 444a1bc5b9..3d77fb147c 100644 --- a/packages/flutter/lib/src/material/radio.dart +++ b/packages/flutter/lib/src/material/radio.dart @@ -7,6 +7,7 @@ import 'package:flutter/widgets.dart'; import 'constants.dart'; import 'debug.dart'; import 'material_state.dart'; +import 'radio_theme.dart'; import 'theme.dart'; import 'theme_data.dart'; import 'toggleable.dart'; @@ -371,11 +372,12 @@ class _RadioState extends State> with TickerProviderStateMixin, Togg Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); final ThemeData themeData = Theme.of(context); + final RadioThemeData radioTheme = RadioTheme.of(context); final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize - ?? themeData.radioTheme.materialTapTargetSize + ?? radioTheme.materialTapTargetSize ?? themeData.materialTapTargetSize; final VisualDensity effectiveVisualDensity = widget.visualDensity - ?? themeData.radioTheme.visualDensity + ?? radioTheme.visualDensity ?? themeData.visualDensity; Size size; switch (effectiveMaterialTapTargetSize) { @@ -390,7 +392,7 @@ class _RadioState extends State> with TickerProviderStateMixin, Togg final MaterialStateProperty effectiveMouseCursor = MaterialStateProperty.resolveWith((Set states) { return MaterialStateProperty.resolveAs(widget.mouseCursor, states) - ?? themeData.radioTheme.mouseCursor?.resolve(states) + ?? radioTheme.mouseCursor?.resolve(states) ?? MaterialStateProperty.resolveAs(MaterialStateMouseCursor.clickable, states); }); @@ -400,33 +402,33 @@ class _RadioState extends State> with TickerProviderStateMixin, Togg final Set inactiveStates = states..remove(MaterialState.selected); final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates) ?? _widgetFillColor.resolve(activeStates) - ?? themeData.radioTheme.fillColor?.resolve(activeStates) + ?? radioTheme.fillColor?.resolve(activeStates) ?? _defaultFillColor.resolve(activeStates); final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates) ?? _widgetFillColor.resolve(inactiveStates) - ?? themeData.radioTheme.fillColor?.resolve(inactiveStates) + ?? radioTheme.fillColor?.resolve(inactiveStates) ?? _defaultFillColor.resolve(inactiveStates); final Set focusedStates = states..add(MaterialState.focused); final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates) ?? widget.focusColor - ?? themeData.radioTheme.overlayColor?.resolve(focusedStates) + ?? radioTheme.overlayColor?.resolve(focusedStates) ?? themeData.focusColor; final Set hoveredStates = states..add(MaterialState.hovered); final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates) ?? widget.hoverColor - ?? themeData.radioTheme.overlayColor?.resolve(hoveredStates) + ?? radioTheme.overlayColor?.resolve(hoveredStates) ?? themeData.hoverColor; final Set activePressedStates = activeStates..add(MaterialState.pressed); final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates) - ?? themeData.radioTheme.overlayColor?.resolve(activePressedStates) + ?? radioTheme.overlayColor?.resolve(activePressedStates) ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha); final Set inactivePressedStates = inactiveStates..add(MaterialState.pressed); final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates) - ?? themeData.radioTheme.overlayColor?.resolve(inactivePressedStates) + ?? radioTheme.overlayColor?.resolve(inactivePressedStates) ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha); return Semantics( @@ -446,7 +448,7 @@ class _RadioState extends State> with TickerProviderStateMixin, Togg ..reactionColor = effectiveActivePressedOverlayColor ..hoverColor = effectiveHoverOverlayColor ..focusColor = effectiveFocusOverlayColor - ..splashRadius = widget.splashRadius ?? themeData.radioTheme.splashRadius ?? kRadialReactionRadius + ..splashRadius = widget.splashRadius ?? radioTheme.splashRadius ?? kRadialReactionRadius ..downPosition = downPosition ..isFocused = states.contains(MaterialState.focused) ..isHovered = states.contains(MaterialState.hovered) diff --git a/packages/flutter/test/material/radio_theme_test.dart b/packages/flutter/test/material/radio_theme_test.dart index 78373c5454..42717fb525 100644 --- a/packages/flutter/test/material/radio_theme_test.dart +++ b/packages/flutter/test/material/radio_theme_test.dart @@ -338,6 +338,37 @@ void main() { reason: 'Active pressed Radio should have overlay color: $activePressedOverlayColor', ); }); + + testWidgets('Local RadioTheme can override global RadioTheme', (WidgetTester tester) async { + const Color globalThemeFillColor = Color(0xfffffff1); + const Color localThemeFillColor = Color(0xffff0000); + + Widget buildRadio({required bool active}) { + return MaterialApp( + theme: ThemeData( + radioTheme: RadioThemeData( + fillColor: MaterialStateProperty.all(globalThemeFillColor), + ), + ), + home: Scaffold( + body: RadioTheme( + data: RadioThemeData( + fillColor: MaterialStateProperty.all(localThemeFillColor), + ), + child: Radio( + value: active ? 1 : 0, + groupValue: 1, + onChanged: (_) { }, + ), + ), + ), + ); + } + + await tester.pumpWidget(buildRadio(active: true)); + await tester.pumpAndSettle(); + expect(_getRadioMaterial(tester), paints..circle(color: localThemeFillColor)); + }); } Finder _findRadio() {