Skip to content

Commit b189c7e

Browse files
authored
[material_ui] Migrate M3 Badge template to use new gen_defaults (#12218)
Work towards flutter/flutter#187899 Fixes flutter/flutter#188401 ## Pre-Review Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran [the auto-formatter]. - [x] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I [linked to at least one issue that this PR fixes] in the description above. - [x] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1]. - [x] I updated/added any relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1]. - [x] All existing and new tests are passing.
1 parent efcb1f0 commit b189c7e

6 files changed

Lines changed: 66 additions & 78 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
/// A badge's [label] conveys a small amount of information about its
@@ -476,36 +478,3 @@ class _RenderIntrinsicHorizontalStadium extends RenderProxyBox {
476478
size = _computeSize(layoutChild: ChildLayoutHelper.layoutChild, constraints: constraints);
477479
}
478480
}
479-
480-
// BEGIN GENERATED TOKEN PROPERTIES - Badge
481-
482-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
483-
// "END GENERATED" comments are generated from data in the Material
484-
// Design token database by the script:
485-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
486-
487-
// dart format off
488-
class _BadgeDefaultsM3 extends BadgeThemeData {
489-
_BadgeDefaultsM3(this.context) : super(
490-
smallSize: 6.0,
491-
largeSize: 16.0,
492-
padding: const EdgeInsets.symmetric(horizontal: 4),
493-
alignment: AlignmentDirectional.topEnd,
494-
);
495-
496-
final BuildContext context;
497-
late final ThemeData _theme = Theme.of(context);
498-
late final ColorScheme _colors = _theme.colorScheme;
499-
500-
@override
501-
Color? get backgroundColor => _colors.error;
502-
503-
@override
504-
Color? get textColor => _colors.onError;
505-
506-
@override
507-
TextStyle? get textStyle => Theme.of(context).textTheme.labelSmall;
508-
}
509-
// dart format on
510-
511-
// 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'package:args/args.dart';
1414

1515
import '../templates/action_chip_template.dart';
1616
import '../templates/app_bar_template.dart';
17-
// import '../templates/badge_template.dart';
17+
import '../templates/badge_template.dart';
1818
// import '../templates/banner_template.dart';
1919
// import '../templates/bottom_app_bar_template.dart';
2020
import '../templates/bottom_sheet_template.dart';
@@ -65,7 +65,7 @@ Future<void> main(List<String> args) async {
6565

6666
const ActionChipTemplateM3().generateFile(verbose: verbose);
6767
const AppBarTemplateM3().generateFile(verbose: verbose);
68-
// const BadgeTemplateM3().generateFile(verbose: verbose);
68+
const BadgeTemplateM3().generateFile(verbose: verbose);
6969
// const BannerTemplateM3().generateFile(verbose: verbose);
7070
// const BottomAppBarTemplateM3().generateFile(verbose: verbose);
7171
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 & 39 deletions
This file was deleted.

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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';
1415
import '../templates/bottom_sheet_template.dart';
1516
import '../templates/template.dart';
1617
import 'test_fixtures/test_templates.dart';
@@ -101,7 +102,7 @@ void main() {
101102
'_colors.onSurface',
102103
);
103104
});
104-
105+
105106
test('border generates border expression', () {
106107
final template = IconButtonTemplateM3(testPath());
107108
expect(template.border('_colors.outline'), 'BorderSide(color: _colors.outline)');
@@ -204,9 +205,19 @@ void main() {
204205
expect(contents, contains('static const double expandedHeight = 152.0'));
205206
});
206207

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
208+
test('BadgeTemplateM3 emits M3 Badge defaults from badge tokens', () {
209+
final String contents = _generateContents(const BadgeTemplateM3());
210+
expect(contents, contains('class _BadgeDefaultsM3 extends BadgeThemeData'));
211+
expect(contents, contains('smallSize: 6.0'));
212+
expect(contents, contains('largeSize: 16.0'));
213+
expect(contents, contains('padding: const EdgeInsets.symmetric(horizontal: 4)'));
214+
expect(contents, contains('alignment: AlignmentDirectional.topEnd'));
215+
expect(contents, contains('Color? get backgroundColor => _colors.error'));
216+
expect(contents, contains('Color? get textColor => _colors.onError'));
217+
expect(
218+
contents,
219+
contains('TextStyle? get textStyle => Theme.of(context).textTheme.labelSmall'),
220+
);
210221
});
211222

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

0 commit comments

Comments
 (0)