Skip to content

Commit ded8502

Browse files
QuncCcccccelliette
authored andcommitted
[material_ui] Migrate M3 Badge template to use new gen_defaults (flutter#12218)
Work towards flutter/flutter#187899 Fixes flutter/flutter#188401 - [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 103a3bd commit ded8502

6 files changed

Lines changed: 65 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: 14 additions & 3 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';
@@ -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)