Skip to content

Commit 48b67e1

Browse files
authored
[material_ui] Migrate M3 Radio template to use new gen_defaults (#13210)
Work toward flutter/flutter#187899. Fixes flutter/flutter#188428. This PR migrates the M3 Radio defaults template to use the new gen_defaults in material_ui. The generated defaults remain unchanged. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 695c444 commit 48b67e1

5 files changed

Lines changed: 43 additions & 122 deletions

File tree

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/radio_defaults.g.dart renamed to packages/material_ui/lib/src/generated/radio_defaults_m3.g.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
// Do not edit by hand. The code is generated from data in the Material
6+
// Design token database by the script:
7+
// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart.
8+
part of '../radio.dart';
9+
510
class _RadioDefaultsM3 extends RadioThemeData {
611
_RadioDefaultsM3(this.context);
712

‎packages/material_ui/lib/src/radio.dart‎

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import 'radio_theme.dart';
2020
import 'theme.dart';
2121
import 'theme_data.dart';
2222

23+
part 'generated/radio_defaults_m3.g.dart';
24+
2325
// Examples can assume:
2426
// late BuildContext context;
2527
// enum SingingCharacter { lafayette }
@@ -896,94 +898,3 @@ class _RadioDefaultsM2 extends RadioThemeData {
896898
WidgetStateProperty<Color> get backgroundColor =>
897899
WidgetStateProperty.all<Color>(Colors.transparent);
898900
}
899-
900-
// BEGIN GENERATED TOKEN PROPERTIES - Radio<T>
901-
902-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
903-
// "END GENERATED" comments are generated from data in the Material
904-
// Design token database by the script:
905-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
906-
907-
// dart format off
908-
class _RadioDefaultsM3 extends RadioThemeData {
909-
_RadioDefaultsM3(this.context);
910-
911-
final BuildContext context;
912-
late final ThemeData _theme = Theme.of(context);
913-
late final ColorScheme _colors = _theme.colorScheme;
914-
915-
@override
916-
WidgetStateProperty<Color> get fillColor {
917-
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
918-
if (states.contains(WidgetState.selected)) {
919-
if (states.contains(WidgetState.disabled)) {
920-
return _colors.onSurface.withOpacity(0.38);
921-
}
922-
if (states.contains(WidgetState.pressed)) {
923-
return _colors.primary;
924-
}
925-
if (states.contains(WidgetState.hovered)) {
926-
return _colors.primary;
927-
}
928-
if (states.contains(WidgetState.focused)) {
929-
return _colors.primary;
930-
}
931-
return _colors.primary;
932-
}
933-
if (states.contains(WidgetState.disabled)) {
934-
return _colors.onSurface.withOpacity(0.38);
935-
}
936-
if (states.contains(WidgetState.pressed)) {
937-
return _colors.onSurface;
938-
}
939-
if (states.contains(WidgetState.hovered)) {
940-
return _colors.onSurface;
941-
}
942-
if (states.contains(WidgetState.focused)) {
943-
return _colors.onSurface;
944-
}
945-
return _colors.onSurfaceVariant;
946-
});
947-
}
948-
949-
@override
950-
WidgetStateProperty<Color> get overlayColor {
951-
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
952-
if (states.contains(WidgetState.selected)) {
953-
if (states.contains(WidgetState.pressed)) {
954-
return _colors.onSurface.withOpacity(0.1);
955-
}
956-
if (states.contains(WidgetState.hovered)) {
957-
return _colors.primary.withOpacity(0.08);
958-
}
959-
if (states.contains(WidgetState.focused)) {
960-
return _colors.primary.withOpacity(0.1);
961-
}
962-
return Colors.transparent;
963-
}
964-
if (states.contains(WidgetState.pressed)) {
965-
return _colors.primary.withOpacity(0.1);
966-
}
967-
if (states.contains(WidgetState.hovered)) {
968-
return _colors.onSurface.withOpacity(0.08);
969-
}
970-
if (states.contains(WidgetState.focused)) {
971-
return _colors.onSurface.withOpacity(0.1);
972-
}
973-
return Colors.transparent;
974-
});
975-
}
976-
977-
@override
978-
MaterialTapTargetSize get materialTapTargetSize => _theme.materialTapTargetSize;
979-
980-
@override
981-
VisualDensity get visualDensity => _theme.visualDensity;
982-
983-
@override
984-
WidgetStateProperty<Color> get backgroundColor =>
985-
WidgetStateProperty.all<Color>(Colors.transparent);
986-
}
987-
// dart format on
988-
989-
// END GENERATED TOKEN PROPERTIES - Radio<T>

‎packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import '../templates/navigation_rail_template.dart';
4343
import '../templates/popup_menu_template.dart';
4444
import '../templates/progress_indicator_template.dart';
4545

46-
// import '../templates/radio_template.dart';
46+
import '../templates/radio_template.dart';
4747
import '../templates/range_slider_template.dart';
4848
import '../templates/search_bar_template.dart';
4949
import '../templates/search_view_template.dart';
@@ -106,7 +106,7 @@ Future<void> main(List<String> args) async {
106106
const NavigationRailTemplateM3().generateFile(verbose: verbose);
107107
const PopupMenuTemplateM3().generateFile(verbose: verbose);
108108
const ProgressIndicatorTemplateM3().generateFile(verbose: verbose);
109-
// const RadioTemplateM3().generateFile(verbose: verbose);
109+
const RadioTemplateM3().generateFile(verbose: verbose);
110110
const RangeSliderTemplateM3().generateFile(verbose: verbose);
111111
const SearchBarTemplateM3().generateFile(verbose: verbose);
112112
const SearchViewTemplateM3().generateFile(verbose: verbose);

packages/material_ui/tool/gen_defaults/temporarily_excluded/templates/radio_template.dart renamed to packages/material_ui/tool/gen_defaults/templates/radio_template.dart

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import '../data/radio_button.dart';
56
import 'template.dart';
67

7-
class RadioTemplate extends TokenTemplate {
8-
const RadioTemplate(
9-
super.blockName,
10-
super.fileName,
11-
super.tokens, {
12-
super.colorSchemePrefix = '_colors.',
13-
});
8+
class RadioTemplateM3 extends TokenTemplateM3 {
9+
const RadioTemplateM3();
1410

1511
@override
16-
String generate() =>
12+
String get name => 'Radio';
13+
14+
@override
15+
String get parentFilePath => 'radio.dart';
16+
17+
@override
18+
String generateContents(String className) =>
1719
'''
18-
class _RadioDefaultsM3 extends RadioThemeData {
19-
_RadioDefaultsM3(this.context);
20+
class $className extends RadioThemeData {
21+
$className(this.context);
2022
2123
final BuildContext context;
2224
late final ThemeData _theme = Theme.of(context);
@@ -27,32 +29,32 @@ class _RadioDefaultsM3 extends RadioThemeData {
2729
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
2830
if (states.contains(WidgetState.selected)) {
2931
if (states.contains(WidgetState.disabled)) {
30-
return ${componentColor('md.comp.radio-button.disabled.selected.icon')};
32+
return ${colorWithOpacity(TokenRadioButton.disabledSelectedIconColor, TokenRadioButton.disabledSelectedIconOpacity)};
3133
}
3234
if (states.contains(WidgetState.pressed)) {
33-
return ${componentColor('md.comp.radio-button.selected.pressed.icon')};
35+
return ${color(TokenRadioButton.selectedPressedIconColor)};
3436
}
3537
if (states.contains(WidgetState.hovered)) {
36-
return ${componentColor('md.comp.radio-button.selected.hover.icon')};
38+
return ${color(TokenRadioButton.selectedHoverIconColor)};
3739
}
3840
if (states.contains(WidgetState.focused)) {
39-
return ${componentColor('md.comp.radio-button.selected.focus.icon')};
41+
return ${color(TokenRadioButton.selectedFocusIconColor)};
4042
}
41-
return ${componentColor('md.comp.radio-button.selected.icon')};
43+
return ${color(TokenRadioButton.selectedIconColor)};
4244
}
4345
if (states.contains(WidgetState.disabled)) {
44-
return ${componentColor('md.comp.radio-button.disabled.unselected.icon')};
46+
return ${colorWithOpacity(TokenRadioButton.disabledUnselectedIconColor, TokenRadioButton.disabledUnselectedIconOpacity)};
4547
}
4648
if (states.contains(WidgetState.pressed)) {
47-
return ${componentColor('md.comp.radio-button.unselected.pressed.icon')};
49+
return ${color(TokenRadioButton.unselectedPressedIconColor)};
4850
}
4951
if (states.contains(WidgetState.hovered)) {
50-
return ${componentColor('md.comp.radio-button.unselected.hover.icon')};
52+
return ${color(TokenRadioButton.unselectedHoverIconColor)};
5153
}
5254
if (states.contains(WidgetState.focused)) {
53-
return ${componentColor('md.comp.radio-button.unselected.focus.icon')};
55+
return ${color(TokenRadioButton.unselectedFocusIconColor)};
5456
}
55-
return ${componentColor('md.comp.radio-button.unselected.icon')};
57+
return ${color(TokenRadioButton.unselectedIconColor)};
5658
});
5759
}
5860
@@ -61,24 +63,24 @@ class _RadioDefaultsM3 extends RadioThemeData {
6163
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
6264
if (states.contains(WidgetState.selected)) {
6365
if (states.contains(WidgetState.pressed)) {
64-
return ${componentColor('md.comp.radio-button.selected.pressed.state-layer')};
66+
return ${colorWithOpacity(TokenRadioButton.selectedPressedStateLayerColor, TokenRadioButton.selectedPressedStateLayerOpacity)};
6567
}
6668
if (states.contains(WidgetState.hovered)) {
67-
return ${componentColor('md.comp.radio-button.selected.hover.state-layer')};
69+
return ${colorWithOpacity(TokenRadioButton.selectedHoverStateLayerColor, TokenRadioButton.selectedHoverStateLayerOpacity)};
6870
}
6971
if (states.contains(WidgetState.focused)) {
70-
return ${componentColor('md.comp.radio-button.selected.focus.state-layer')};
72+
return ${colorWithOpacity(TokenRadioButton.selectedFocusStateLayerColor, TokenRadioButton.selectedFocusStateLayerOpacity)};
7173
}
7274
return Colors.transparent;
7375
}
7476
if (states.contains(WidgetState.pressed)) {
75-
return ${componentColor('md.comp.radio-button.unselected.pressed.state-layer')};
77+
return ${colorWithOpacity(TokenRadioButton.unselectedPressedStateLayerColor, TokenRadioButton.unselectedPressedStateLayerOpacity)};
7678
}
7779
if (states.contains(WidgetState.hovered)) {
78-
return ${componentColor('md.comp.radio-button.unselected.hover.state-layer')};
80+
return ${colorWithOpacity(TokenRadioButton.unselectedHoverStateLayerColor, TokenRadioButton.unselectedHoverStateLayerOpacity)};
7981
}
8082
if (states.contains(WidgetState.focused)) {
81-
return ${componentColor('md.comp.radio-button.unselected.focus.state-layer')};
83+
return ${colorWithOpacity(TokenRadioButton.unselectedFocusStateLayerColor, TokenRadioButton.unselectedFocusStateLayerOpacity)};
8284
}
8385
return Colors.transparent;
8486
});

‎packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import '../templates/navigation_drawer_template.dart';
3939
import '../templates/navigation_rail_template.dart';
4040
import '../templates/popup_menu_template.dart';
4141
import '../templates/progress_indicator_template.dart';
42-
// import '../templates/radio_template.dart';
42+
import '../templates/radio_template.dart';
4343
import '../templates/range_slider_template.dart';
4444
import '../templates/search_bar_template.dart';
4545
import '../templates/search_view_template.dart';
@@ -810,8 +810,11 @@ void main() {
810810
});
811811

812812
test('RadioTemplateM3 emits M3 Radio defaults from tokens', () {
813-
// Intentionally empty, will be implemented during migration. See:
814-
// https://github.com/flutter/flutter/issues/187899
813+
final String contents = _generateContents(const RadioTemplateM3());
814+
expect(contents, contains('class _RadioDefaultsM3 extends RadioThemeData'));
815+
expect(contents, contains('_colors.onSurface.withOpacity(0.38)'));
816+
expect(contents, contains('_colors.onSurface.withOpacity(0.1)'));
817+
expect(contents, contains('_colors.onSurfaceVariant'));
815818
});
816819

817820
test('RangeSliderTemplateM3 emits M3 RangeSlider defaults from tokens', () {

0 commit comments

Comments
 (0)