Skip to content

Commit 5840723

Browse files
authored
Fix Local RadioTheme not being inherited by Radio Widget (#97713)
1 parent 7eb1035 commit 5840723

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

packages/flutter/lib/src/material/radio.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter/widgets.dart';
77
import 'constants.dart';
88
import 'debug.dart';
99
import 'material_state.dart';
10+
import 'radio_theme.dart';
1011
import 'theme.dart';
1112
import 'theme_data.dart';
1213
import 'toggleable.dart';
@@ -371,11 +372,12 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, Togg
371372
Widget build(BuildContext context) {
372373
assert(debugCheckHasMaterial(context));
373374
final ThemeData themeData = Theme.of(context);
375+
final RadioThemeData radioTheme = RadioTheme.of(context);
374376
final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize
375-
?? themeData.radioTheme.materialTapTargetSize
377+
?? radioTheme.materialTapTargetSize
376378
?? themeData.materialTapTargetSize;
377379
final VisualDensity effectiveVisualDensity = widget.visualDensity
378-
?? themeData.radioTheme.visualDensity
380+
?? radioTheme.visualDensity
379381
?? themeData.visualDensity;
380382
Size size;
381383
switch (effectiveMaterialTapTargetSize) {
@@ -390,7 +392,7 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, Togg
390392

391393
final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) {
392394
return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
393-
?? themeData.radioTheme.mouseCursor?.resolve(states)
395+
?? radioTheme.mouseCursor?.resolve(states)
394396
?? MaterialStateProperty.resolveAs<MouseCursor>(MaterialStateMouseCursor.clickable, states);
395397
});
396398

@@ -400,33 +402,33 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, Togg
400402
final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected);
401403
final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates)
402404
?? _widgetFillColor.resolve(activeStates)
403-
?? themeData.radioTheme.fillColor?.resolve(activeStates)
405+
?? radioTheme.fillColor?.resolve(activeStates)
404406
?? _defaultFillColor.resolve(activeStates);
405407
final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates)
406408
?? _widgetFillColor.resolve(inactiveStates)
407-
?? themeData.radioTheme.fillColor?.resolve(inactiveStates)
409+
?? radioTheme.fillColor?.resolve(inactiveStates)
408410
?? _defaultFillColor.resolve(inactiveStates);
409411

410412
final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
411413
final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
412414
?? widget.focusColor
413-
?? themeData.radioTheme.overlayColor?.resolve(focusedStates)
415+
?? radioTheme.overlayColor?.resolve(focusedStates)
414416
?? themeData.focusColor;
415417

416418
final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
417419
final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
418420
?? widget.hoverColor
419-
?? themeData.radioTheme.overlayColor?.resolve(hoveredStates)
421+
?? radioTheme.overlayColor?.resolve(hoveredStates)
420422
?? themeData.hoverColor;
421423

422424
final Set<MaterialState> activePressedStates = activeStates..add(MaterialState.pressed);
423425
final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates)
424-
?? themeData.radioTheme.overlayColor?.resolve(activePressedStates)
426+
?? radioTheme.overlayColor?.resolve(activePressedStates)
425427
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
426428

427429
final Set<MaterialState> inactivePressedStates = inactiveStates..add(MaterialState.pressed);
428430
final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates)
429-
?? themeData.radioTheme.overlayColor?.resolve(inactivePressedStates)
431+
?? radioTheme.overlayColor?.resolve(inactivePressedStates)
430432
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
431433

432434
return Semantics(
@@ -446,7 +448,7 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, Togg
446448
..reactionColor = effectiveActivePressedOverlayColor
447449
..hoverColor = effectiveHoverOverlayColor
448450
..focusColor = effectiveFocusOverlayColor
449-
..splashRadius = widget.splashRadius ?? themeData.radioTheme.splashRadius ?? kRadialReactionRadius
451+
..splashRadius = widget.splashRadius ?? radioTheme.splashRadius ?? kRadialReactionRadius
450452
..downPosition = downPosition
451453
..isFocused = states.contains(MaterialState.focused)
452454
..isHovered = states.contains(MaterialState.hovered)

packages/flutter/test/material/radio_theme_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,37 @@ void main() {
338338
reason: 'Active pressed Radio should have overlay color: $activePressedOverlayColor',
339339
);
340340
});
341+
342+
testWidgets('Local RadioTheme can override global RadioTheme', (WidgetTester tester) async {
343+
const Color globalThemeFillColor = Color(0xfffffff1);
344+
const Color localThemeFillColor = Color(0xffff0000);
345+
346+
Widget buildRadio({required bool active}) {
347+
return MaterialApp(
348+
theme: ThemeData(
349+
radioTheme: RadioThemeData(
350+
fillColor: MaterialStateProperty.all<Color>(globalThemeFillColor),
351+
),
352+
),
353+
home: Scaffold(
354+
body: RadioTheme(
355+
data: RadioThemeData(
356+
fillColor: MaterialStateProperty.all<Color>(localThemeFillColor),
357+
),
358+
child: Radio<int>(
359+
value: active ? 1 : 0,
360+
groupValue: 1,
361+
onChanged: (_) { },
362+
),
363+
),
364+
),
365+
);
366+
}
367+
368+
await tester.pumpWidget(buildRadio(active: true));
369+
await tester.pumpAndSettle();
370+
expect(_getRadioMaterial(tester), paints..circle(color: localThemeFillColor));
371+
});
341372
}
342373

343374
Finder _findRadio() {

0 commit comments

Comments
 (0)