4
4
5
5
import 'package:flutter/widgets.dart' ;
6
6
7
+ import 'color_scheme.dart' ;
8
+ import 'colors.dart' ;
7
9
import 'constants.dart' ;
8
10
import 'debug.dart' ;
9
11
import 'material_state.dart' ;
@@ -360,30 +362,17 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, Togg
360
362
});
361
363
}
362
364
363
- MaterialStateProperty <Color > get _defaultFillColor {
364
- final ThemeData themeData = Theme .of (context);
365
- return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
366
- if (states.contains (MaterialState .disabled)) {
367
- return themeData.disabledColor;
368
- }
369
- if (states.contains (MaterialState .selected)) {
370
- return themeData.colorScheme.secondary;
371
- }
372
- return themeData.unselectedWidgetColor;
373
- });
374
- }
375
-
376
365
@override
377
366
Widget build (BuildContext context) {
378
367
assert (debugCheckHasMaterial (context));
379
- final ThemeData themeData = Theme .of (context);
380
368
final RadioThemeData radioTheme = RadioTheme .of (context);
369
+ final RadioThemeData defaults = Theme .of (context).useMaterial3 ? _RadioDefaultsM3 (context) : _RadioDefaultsM2 (context);
381
370
final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize
382
371
?? radioTheme.materialTapTargetSize
383
- ?? themeData .materialTapTargetSize;
372
+ ?? defaults .materialTapTargetSize! ;
384
373
final VisualDensity effectiveVisualDensity = widget.visualDensity
385
374
?? radioTheme.visualDensity
386
- ?? themeData .visualDensity;
375
+ ?? defaults .visualDensity! ;
387
376
Size size;
388
377
switch (effectiveMaterialTapTargetSize) {
389
378
case MaterialTapTargetSize .padded:
@@ -405,36 +394,47 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, Togg
405
394
// so that they can be lerped between.
406
395
final Set <MaterialState > activeStates = states..add (MaterialState .selected);
407
396
final Set <MaterialState > inactiveStates = states..remove (MaterialState .selected);
408
- final Color effectiveActiveColor = widget.fillColor? .resolve (activeStates)
397
+ final Color ? activeColor = widget.fillColor? .resolve (activeStates)
409
398
?? _widgetFillColor.resolve (activeStates)
410
- ?? radioTheme.fillColor? .resolve (activeStates)
411
- ?? _defaultFillColor. resolve (activeStates);
412
- final Color effectiveInactiveColor = widget.fillColor? .resolve (inactiveStates)
399
+ ?? radioTheme.fillColor? .resolve (activeStates);
400
+ final Color effectiveActiveColor = activeColor ?? defaults.fillColor ! . resolve (activeStates)! ;
401
+ final Color ? inactiveColor = widget.fillColor? .resolve (inactiveStates)
413
402
?? _widgetFillColor.resolve (inactiveStates)
414
- ?? radioTheme.fillColor? .resolve (inactiveStates)
415
- ?? _defaultFillColor. resolve (inactiveStates);
403
+ ?? radioTheme.fillColor? .resolve (inactiveStates);
404
+ final Color effectiveInactiveColor = inactiveColor ?? defaults.fillColor ! . resolve (inactiveStates)! ;
416
405
417
406
final Set <MaterialState > focusedStates = states..add (MaterialState .focused);
418
- final Color effectiveFocusOverlayColor = widget.overlayColor? .resolve (focusedStates)
407
+ Color effectiveFocusOverlayColor = widget.overlayColor? .resolve (focusedStates)
419
408
?? widget.focusColor
420
409
?? radioTheme.overlayColor? .resolve (focusedStates)
421
- ?? themeData.focusColor ;
410
+ ?? defaults.overlayColor ! . resolve (focusedStates) ! ;
422
411
423
412
final Set <MaterialState > hoveredStates = states..add (MaterialState .hovered);
424
- final Color effectiveHoverOverlayColor = widget.overlayColor? .resolve (hoveredStates)
425
- ?? widget.hoverColor
426
- ?? radioTheme.overlayColor? .resolve (hoveredStates)
427
- ?? themeData.hoverColor ;
413
+ Color effectiveHoverOverlayColor = widget.overlayColor? .resolve (hoveredStates)
414
+ ?? widget.hoverColor
415
+ ?? radioTheme.overlayColor? .resolve (hoveredStates)
416
+ ?? defaults.overlayColor ! . resolve (hoveredStates) ! ;
428
417
429
418
final Set <MaterialState > activePressedStates = activeStates..add (MaterialState .pressed);
430
419
final Color effectiveActivePressedOverlayColor = widget.overlayColor? .resolve (activePressedStates)
431
- ?? radioTheme.overlayColor? .resolve (activePressedStates)
432
- ?? effectiveActiveColor.withAlpha (kRadialReactionAlpha);
420
+ ?? radioTheme.overlayColor? .resolve (activePressedStates)
421
+ ?? activeColor? .withAlpha (kRadialReactionAlpha)
422
+ ?? defaults.overlayColor! .resolve (activePressedStates)! ;
433
423
434
424
final Set <MaterialState > inactivePressedStates = inactiveStates..add (MaterialState .pressed);
435
425
final Color effectiveInactivePressedOverlayColor = widget.overlayColor? .resolve (inactivePressedStates)
436
- ?? radioTheme.overlayColor? .resolve (inactivePressedStates)
437
- ?? effectiveActiveColor.withAlpha (kRadialReactionAlpha);
426
+ ?? radioTheme.overlayColor? .resolve (inactivePressedStates)
427
+ ?? inactiveColor? .withAlpha (kRadialReactionAlpha)
428
+ ?? defaults.overlayColor! .resolve (inactivePressedStates)! ;
429
+
430
+ if (downPosition != null ) {
431
+ effectiveHoverOverlayColor = states.contains (MaterialState .selected)
432
+ ? effectiveActivePressedOverlayColor
433
+ : effectiveInactivePressedOverlayColor;
434
+ effectiveFocusOverlayColor = states.contains (MaterialState .selected)
435
+ ? effectiveActivePressedOverlayColor
436
+ : effectiveInactivePressedOverlayColor;
437
+ }
438
438
439
439
return Semantics (
440
440
inMutuallyExclusiveGroup: true ,
@@ -485,3 +485,134 @@ class _RadioPainter extends ToggleablePainter {
485
485
}
486
486
}
487
487
}
488
+
489
+ // Hand coded defaults based on Material Design 2.
490
+ class _RadioDefaultsM2 extends RadioThemeData {
491
+ _RadioDefaultsM2 (this .context);
492
+
493
+ final BuildContext context;
494
+ late final ThemeData _theme = Theme .of (context);
495
+ late final ColorScheme _colors = _theme.colorScheme;
496
+
497
+ @override
498
+ MaterialStateProperty <Color > get fillColor {
499
+ return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
500
+ if (states.contains (MaterialState .disabled)) {
501
+ return _theme.disabledColor;
502
+ }
503
+ if (states.contains (MaterialState .selected)) {
504
+ return _colors.secondary;
505
+ }
506
+ return _theme.unselectedWidgetColor;
507
+ });
508
+ }
509
+
510
+ @override
511
+ MaterialStateProperty <Color > get overlayColor {
512
+ return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
513
+ if (states.contains (MaterialState .pressed)) {
514
+ return fillColor.resolve (states).withAlpha (kRadialReactionAlpha);
515
+ }
516
+ if (states.contains (MaterialState .focused)) {
517
+ return _theme.focusColor;
518
+ }
519
+ if (states.contains (MaterialState .hovered)) {
520
+ return _theme.hoverColor;
521
+ }
522
+ return Colors .transparent;
523
+ });
524
+ }
525
+
526
+ @override
527
+ MaterialTapTargetSize get materialTapTargetSize => _theme.materialTapTargetSize;
528
+
529
+ @override
530
+ VisualDensity get visualDensity => _theme.visualDensity;
531
+ }
532
+
533
+ // BEGIN GENERATED TOKEN PROPERTIES - Radio<T>
534
+
535
+ // Do not edit by hand. The code between the "BEGIN GENERATED" and
536
+ // "END GENERATED" comments are generated from data in the Material
537
+ // Design token database by the script:
538
+ // dev/tools/gen_defaults/bin/gen_defaults.dart.
539
+
540
+ // Token database version: v0_132
541
+
542
+ class _RadioDefaultsM3 extends RadioThemeData {
543
+ _RadioDefaultsM3 (this .context);
544
+
545
+ final BuildContext context;
546
+ late final ThemeData _theme = Theme .of (context);
547
+ late final ColorScheme _colors = _theme.colorScheme;
548
+
549
+ @override
550
+ MaterialStateProperty <Color > get fillColor {
551
+ return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
552
+ if (states.contains (MaterialState .selected)) {
553
+ if (states.contains (MaterialState .disabled)) {
554
+ return _colors.onSurface.withOpacity (0.38 );
555
+ }
556
+ if (states.contains (MaterialState .pressed)) {
557
+ return _colors.primary;
558
+ }
559
+ if (states.contains (MaterialState .hovered)) {
560
+ return _colors.primary;
561
+ }
562
+ if (states.contains (MaterialState .focused)) {
563
+ return _colors.primary;
564
+ }
565
+ return _colors.primary;
566
+ }
567
+ if (states.contains (MaterialState .disabled)) {
568
+ return _colors.onSurface.withOpacity (0.38 );
569
+ }
570
+ if (states.contains (MaterialState .pressed)) {
571
+ return _colors.onSurface;
572
+ }
573
+ if (states.contains (MaterialState .hovered)) {
574
+ return _colors.onSurface;
575
+ }
576
+ if (states.contains (MaterialState .focused)) {
577
+ return _colors.onSurface;
578
+ }
579
+ return _colors.onSurfaceVariant;
580
+ });
581
+ }
582
+
583
+ @override
584
+ MaterialStateProperty <Color > get overlayColor {
585
+ return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
586
+ if (states.contains (MaterialState .selected)) {
587
+ if (states.contains (MaterialState .pressed)) {
588
+ return _colors.onSurface.withOpacity (0.12 );
589
+ }
590
+ if (states.contains (MaterialState .hovered)) {
591
+ return _colors.primary.withOpacity (0.08 );
592
+ }
593
+ if (states.contains (MaterialState .focused)) {
594
+ return _colors.primary.withOpacity (0.12 );
595
+ }
596
+ return Colors .transparent;
597
+ }
598
+ if (states.contains (MaterialState .pressed)) {
599
+ return _colors.primary.withOpacity (0.12 );
600
+ }
601
+ if (states.contains (MaterialState .hovered)) {
602
+ return _colors.onSurface.withOpacity (0.08 );
603
+ }
604
+ if (states.contains (MaterialState .focused)) {
605
+ return _colors.onSurface.withOpacity (0.12 );
606
+ }
607
+ return Colors .transparent;
608
+ });
609
+ }
610
+
611
+ @override
612
+ MaterialTapTargetSize get materialTapTargetSize => _theme.materialTapTargetSize;
613
+
614
+ @override
615
+ VisualDensity get visualDensity => _theme.visualDensity;
616
+ }
617
+
618
+ // END GENERATED TOKEN PROPERTIES - Radio<T>
0 commit comments