Skip to content

Commit e941cd1

Browse files
authored
[material_ui] Migrate M3 Badge template to use new gen_defaults (#12733)
Work towards flutter/flutter#191088 **Ports over #12218 which landed on the `m3e_migration` feature branch.** ### Original PR description Work towards flutter/flutter#187899 Fixes flutter/flutter#188401 ## 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 0ad8af3 commit e941cd1

6 files changed

Lines changed: 103 additions & 79 deletions

File tree

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

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import 'badge_theme.dart';
1717
import 'color_scheme.dart';
1818
import 'theme.dart';
1919

20+
part 'generated/badge_defaults_m3.g.dart';
21+
2022
/// A Material Design "badge".
2123
///
2224
/// Learn more about [Badge] on the [Flutter YouTube channel](https://www.youtube.com/watch?v=_CIHLJHVoN8).
@@ -484,36 +486,3 @@ class _RenderIntrinsicHorizontalStadium extends RenderProxyBox {
484486
size = _computeSize(layoutChild: ChildLayoutHelper.layoutChild, constraints: constraints);
485487
}
486488
}
487-
488-
// BEGIN GENERATED TOKEN PROPERTIES - Badge
489-
490-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
491-
// "END GENERATED" comments are generated from data in the Material
492-
// Design token database by the script:
493-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
494-
495-
// dart format off
496-
class _BadgeDefaultsM3 extends BadgeThemeData {
497-
_BadgeDefaultsM3(this.context) : super(
498-
smallSize: 6.0,
499-
largeSize: 16.0,
500-
padding: const EdgeInsets.symmetric(horizontal: 4),
501-
alignment: AlignmentDirectional.topEnd,
502-
);
503-
504-
final BuildContext context;
505-
late final ThemeData _theme = Theme.of(context);
506-
late final ColorScheme _colors = _theme.colorScheme;
507-
508-
@override
509-
Color? get backgroundColor => _colors.error;
510-
511-
@override
512-
Color? get textColor => _colors.onError;
513-
514-
@override
515-
TextStyle? get textStyle => Theme.of(context).textTheme.labelSmall;
516-
}
517-
// dart format on
518-
519-
// END GENERATED TOKEN PROPERTIES - Badge

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/badge_defaults.g.dart renamed to packages/material_ui/lib/src/generated/badge_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 '../badge.dart';
9+
510
class _BadgeDefaultsM3 extends BadgeThemeData {
611
_BadgeDefaultsM3(this.context)
712
: super(

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import 'package:args/args.dart';
1414

1515
import '../templates/action_chip_template.dart';
1616
import '../templates/app_bar_template.dart';
17-
18-
// import '../templates/badge_template.dart';
17+
import '../templates/badge_template.dart';
1918
// import '../templates/banner_template.dart';
2019
// import '../templates/bottom_app_bar_template.dart';
2120
import '../templates/bottom_sheet_template.dart';
@@ -67,7 +66,7 @@ Future<void> main(List<String> args) async {
6766

6867
const ActionChipTemplateM3().generateFile(verbose: verbose);
6968
const AppBarTemplateM3().generateFile(verbose: verbose);
70-
// const BadgeTemplateM3().generateFile(verbose: verbose);
69+
const BadgeTemplateM3().generateFile(verbose: verbose);
7170
// const BannerTemplateM3().generateFile(verbose: verbose);
7271
// const BottomAppBarTemplateM3().generateFile(verbose: verbose);
7372
const BottomSheetTemplateM3().generateFile(verbose: verbose);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import '../data/badge.dart';
6+
import 'template.dart';
7+
8+
class BadgeTemplateM3 extends TokenTemplateM3 {
9+
const BadgeTemplateM3();
10+
11+
@override
12+
String get name => 'Badge';
13+
14+
@override
15+
String get parentFilePath => 'badge.dart';
16+
17+
@override
18+
String generateContents(String className) =>
19+
'''
20+
class $className extends BadgeThemeData {
21+
$className(this.context) : super(
22+
smallSize: ${number(TokenBadge.size)},
23+
largeSize: ${number(TokenBadge.largeSize)},
24+
padding: const EdgeInsets.symmetric(horizontal: 4),
25+
alignment: AlignmentDirectional.topEnd,
26+
);
27+
28+
final BuildContext context;
29+
late final ThemeData _theme = Theme.of(context);
30+
late final ColorScheme _colors = _theme.colorScheme;
31+
32+
@override
33+
Color? get backgroundColor => ${color(TokenBadge.color, '_colors')};
34+
35+
@override
36+
Color? get textColor => ${color(TokenBadge.largeLabelTextColor, '_colors')};
37+
38+
@override
39+
TextStyle? get textStyle => ${textStyle(TokenBadge.largeLabelTextType, 'Theme.of(context).textTheme')};
40+
}
41+
''';
42+
}

‎packages/material_ui/tool/gen_defaults/temporarily_excluded/templates/badge_template.dart‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,46 @@ import '../data/typescale.dart';
1111
import '../data/typescale_emphasized.dart';
1212
import '../templates/action_chip_template.dart';
1313
import '../templates/app_bar_template.dart';
14+
import '../templates/badge_template.dart';
15+
// import '../templates/banner_template.dart';
16+
// import '../templates/bottom_app_bar_template.dart';
1417
import '../templates/bottom_sheet_template.dart';
18+
// import '../templates/button_template.dart';
19+
// import '../templates/card_template.dart';
20+
// import '../templates/checkbox_template.dart';
21+
// import '../templates/chip_template.dart';
22+
// import '../templates/color_scheme_template.dart';
23+
// import '../templates/date_picker_template.dart';
24+
// import '../templates/dialog_template.dart';
25+
// import '../templates/divider_template.dart';
26+
// import '../templates/drawer_template.dart';
27+
// import '../templates/expansion_tile_template.dart';
28+
// import '../templates/fab_template.dart';
29+
// import '../templates/filter_chip_template.dart';
30+
// import '../templates/icon_button_template.dart';
31+
// import '../templates/input_chip_template.dart';
32+
// import '../templates/input_decorator_template.dart';
33+
// import '../templates/list_tile_template.dart';
34+
// import '../templates/menu_template.dart';
35+
// import '../templates/motion_template.dart';
36+
// import '../templates/navigation_bar_template.dart';
37+
// import '../templates/navigation_drawer_template.dart';
38+
// import '../templates/navigation_rail_template.dart';
39+
// import '../templates/popup_menu_template.dart';
40+
// import '../templates/progress_indicator_template.dart';
41+
// import '../templates/radio_template.dart';
42+
// import '../templates/range_slider_template.dart';
43+
// import '../templates/search_bar_template.dart';
44+
// import '../templates/search_view_template.dart';
45+
// import '../templates/segmented_button_template.dart';
46+
// import '../templates/slider_template.dart';
47+
// import '../templates/snackbar_template.dart';
48+
// import '../templates/surface_tint_template.dart';
49+
// import '../templates/switch_template.dart';
50+
// import '../templates/tabs_template.dart';
51+
// import '../templates/text_field_template.dart';
52+
// import '../templates/time_picker_template.dart';
53+
// import '../templates/typography_template.dart';
1554
import '../templates/template.dart';
1655
import 'test_fixtures/test_templates.dart';
1756

@@ -204,9 +243,19 @@ void main() {
204243
expect(contents, contains('static const double expandedHeight = 152.0'));
205244
});
206245

207-
test('BadgeTemplateM3 emits M3 Badge defaults from tokens', () {
208-
// Intentionally empty, will be implemented during migration. See:
209-
// https://github.com/flutter/flutter/issues/187899
246+
test('BadgeTemplateM3 emits M3 Badge defaults from badge tokens', () {
247+
final String contents = _generateContents(const BadgeTemplateM3());
248+
expect(contents, contains('class _BadgeDefaultsM3 extends BadgeThemeData'));
249+
expect(contents, contains('smallSize: 6.0'));
250+
expect(contents, contains('largeSize: 16.0'));
251+
expect(contents, contains('padding: const EdgeInsets.symmetric(horizontal: 4)'));
252+
expect(contents, contains('alignment: AlignmentDirectional.topEnd'));
253+
expect(contents, contains('Color? get backgroundColor => _colors.error'));
254+
expect(contents, contains('Color? get textColor => _colors.onError'));
255+
expect(
256+
contents,
257+
contains('TextStyle? get textStyle => Theme.of(context).textTheme.labelSmall'),
258+
);
210259
});
211260

212261
test('BannerTemplateM3 emits M3 Banner defaults from tokens', () {

0 commit comments

Comments
 (0)