Skip to content

Commit 7a7912f

Browse files
authored
[material_ui] Migrate AppBar M3 template to use new gen_defaults script (#12707)
Work towards flutter/flutter#191088 **Ports over #12122 which landed on the `m3e_migration` feature branch.** ### Original PR description Fixes flutter/flutter#188397 Initially gen_defaults script and its templates were used to handle JSON tokens. After we fix the token pipeline and make it possible to get M3E tokens, we use Dart constants and re-created the gen_defaults in material_ui. After decoupling, the old gen_defaults will be deprecated soon. This PR is to: create a template for M3 AppBar so we can use new script to generate M3 defaults. move the generated code to generated/ folder so all M3 and M3E generated code will be in the same location. ## 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 2e33644 commit 7a7912f

6 files changed

Lines changed: 143 additions & 226 deletions

File tree

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

Lines changed: 2 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import 'tabs.dart';
3434
import 'text_theme.dart';
3535
import 'theme.dart';
3636

37+
part 'generated/app_bar_defaults_m3.g.dart';
38+
3739
// Examples can assume:
3840
// late String _logoAsset;
3941
// double _myToolbarHeight = 250.0;
@@ -2576,112 +2578,3 @@ class _AppBarDefaultsM2 extends AppBarThemeData {
25762578
@override
25772579
EdgeInsets? get actionsPadding => EdgeInsets.zero;
25782580
}
2579-
2580-
// BEGIN GENERATED TOKEN PROPERTIES - AppBar
2581-
2582-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
2583-
// "END GENERATED" comments are generated from data in the Material
2584-
// Design token database by the script:
2585-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
2586-
2587-
// dart format off
2588-
class _AppBarDefaultsM3 extends AppBarThemeData {
2589-
_AppBarDefaultsM3(this.context)
2590-
: super(
2591-
elevation: 0.0,
2592-
scrolledUnderElevation: 3.0,
2593-
titleSpacing: NavigationToolbar.kMiddleSpacing,
2594-
toolbarHeight: 64.0,
2595-
);
2596-
2597-
final BuildContext context;
2598-
late final ThemeData _theme = Theme.of(context);
2599-
late final ColorScheme _colors = _theme.colorScheme;
2600-
late final TextTheme _textTheme = _theme.textTheme;
2601-
2602-
@override
2603-
Color? get backgroundColor => _colors.surface;
2604-
2605-
@override
2606-
Color? get foregroundColor => _colors.onSurface;
2607-
2608-
@override
2609-
Color? get shadowColor => Colors.transparent;
2610-
2611-
@override
2612-
Color? get surfaceTintColor => Colors.transparent;
2613-
2614-
@override
2615-
IconThemeData? get iconTheme => IconThemeData(
2616-
color: _colors.onSurface,
2617-
size: 24.0,
2618-
);
2619-
2620-
@override
2621-
IconThemeData? get actionsIconTheme => IconThemeData(
2622-
color: _colors.onSurfaceVariant,
2623-
size: 24.0,
2624-
);
2625-
2626-
@override
2627-
TextStyle? get toolbarTextStyle => _textTheme.bodyMedium;
2628-
2629-
@override
2630-
TextStyle? get titleTextStyle => _textTheme.titleLarge;
2631-
2632-
// TODO(Craftplacer): Consider using EdgeInsets.only(right: 8.0) instead of
2633-
// EdgeInsets.zero for Material 3 in the future,
2634-
// https://github.com/flutter/flutter/issues/155747
2635-
@override
2636-
EdgeInsets? get actionsPadding => EdgeInsets.zero;
2637-
}
2638-
2639-
// Variant configuration
2640-
class _MediumScrollUnderFlexibleConfig with _ScrollUnderFlexibleConfig {
2641-
_MediumScrollUnderFlexibleConfig(this.context);
2642-
2643-
final BuildContext context;
2644-
late final ThemeData _theme = Theme.of(context);
2645-
late final ColorScheme _colors = _theme.colorScheme;
2646-
late final TextTheme _textTheme = _theme.textTheme;
2647-
2648-
static const double collapsedHeight = 64.0;
2649-
static const double expandedHeight = 112.0;
2650-
2651-
@override
2652-
TextStyle? get collapsedTextStyle =>
2653-
_textTheme.titleLarge?.apply(color: _colors.onSurface);
2654-
2655-
@override
2656-
TextStyle? get expandedTextStyle =>
2657-
_textTheme.headlineSmall?.apply(color: _colors.onSurface);
2658-
2659-
@override
2660-
EdgeInsetsGeometry get expandedTitlePadding => const EdgeInsets.fromLTRB(16, 0, 16, 20);
2661-
}
2662-
2663-
class _LargeScrollUnderFlexibleConfig with _ScrollUnderFlexibleConfig {
2664-
_LargeScrollUnderFlexibleConfig(this.context);
2665-
2666-
final BuildContext context;
2667-
late final ThemeData _theme = Theme.of(context);
2668-
late final ColorScheme _colors = _theme.colorScheme;
2669-
late final TextTheme _textTheme = _theme.textTheme;
2670-
2671-
static const double collapsedHeight = 64.0;
2672-
static const double expandedHeight = 152.0;
2673-
2674-
@override
2675-
TextStyle? get collapsedTextStyle =>
2676-
_textTheme.titleLarge?.apply(color: _colors.onSurface);
2677-
2678-
@override
2679-
TextStyle? get expandedTextStyle =>
2680-
_textTheme.headlineMedium?.apply(color: _colors.onSurface);
2681-
2682-
@override
2683-
EdgeInsetsGeometry get expandedTitlePadding => const EdgeInsets.fromLTRB(16, 0, 16, 28);
2684-
}
2685-
// dart format on
2686-
2687-
// END GENERATED TOKEN PROPERTIES - AppBar

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/app_bar_defaults.g.dart renamed to packages/material_ui/lib/src/generated/app_bar_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 '../app_bar.dart';
9+
510
class _AppBarDefaultsM3 extends AppBarThemeData {
611
_AppBarDefaultsM3(this.context)
712
: super(

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212

1313
import 'package:args/args.dart';
1414

15-
// TODO(elliette): Import template files.
16-
// import '../templates/x_template.dart';
15+
import '../templates/app_bar_template.dart';
1716

1817
Future<void> main(List<String> args) async {
1918
// Parse arguments
2019
final parser = ArgParser();
2120
parser.addFlag('verbose', abbr: 'v', help: 'Enable verbose output', negatable: false);
2221
final ArgResults argResults = parser.parse(args);
2322
// TODO(elliette): Add token logger when verbose flag is used.
24-
// ignore: unused_local_variable
2523
final verbose = argResults['verbose'] as bool;
26-
// TODO(elliette): Invoke template generators.
27-
// const XTemplate().generateFile(verbose: verbose);
24+
const AppBarTemplateM3().generateFile(verbose: verbose);
2825
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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/app_bar.dart';
6+
import '../data/app_bar_large.dart';
7+
import '../data/app_bar_medium.dart';
8+
import '../data/app_bar_small.dart';
9+
import 'template.dart';
10+
11+
class AppBarTemplateM3 extends TokenTemplateM3 {
12+
const AppBarTemplateM3();
13+
14+
@override
15+
String get name => 'App Bar';
16+
17+
@override
18+
String get parentFilePath => 'app_bar.dart';
19+
20+
@override
21+
String generateContents(String className) =>
22+
'''
23+
class $className extends AppBarThemeData {
24+
$className(this.context)
25+
: super(
26+
elevation: ${number(TokenAppBar.containerElevation)},
27+
scrolledUnderElevation: ${number(TokenAppBar.onScrollContainerElevation)},
28+
titleSpacing: NavigationToolbar.kMiddleSpacing,
29+
toolbarHeight: ${number(TokenAppBarSmall.containerHeight)},
30+
);
31+
32+
final BuildContext context;
33+
late final ThemeData _theme = Theme.of(context);
34+
late final ColorScheme _colors = _theme.colorScheme;
35+
late final TextTheme _textTheme = _theme.textTheme;
36+
37+
@override
38+
Color? get backgroundColor => ${color(TokenAppBar.containerColor, '_colors')};
39+
40+
@override
41+
Color? get foregroundColor => ${color(TokenAppBar.titleColor, '_colors')};
42+
43+
@override
44+
Color? get shadowColor => Colors.transparent;
45+
46+
@override
47+
Color? get surfaceTintColor => Colors.transparent;
48+
49+
@override
50+
IconThemeData? get iconTheme => IconThemeData(
51+
color: ${color(TokenAppBar.leadingIconColor, '_colors')},
52+
size: ${number(TokenAppBar.iconSize)},
53+
);
54+
55+
@override
56+
IconThemeData? get actionsIconTheme => IconThemeData(
57+
color: ${color(TokenAppBar.trailingIconColor, '_colors')},
58+
size: ${number(TokenAppBar.iconSize)},
59+
);
60+
61+
@override
62+
TextStyle? get toolbarTextStyle => _textTheme.bodyMedium;
63+
64+
@override
65+
TextStyle? get titleTextStyle => _textTheme.titleLarge;
66+
67+
// TODO(Craftplacer): Consider using EdgeInsets.only(right: 8.0) instead of
68+
// EdgeInsets.zero for Material 3 in the future,
69+
// https://github.com/flutter/flutter/issues/155747
70+
@override
71+
EdgeInsets? get actionsPadding => EdgeInsets.zero;
72+
}
73+
74+
// Variant configuration
75+
class _MediumScrollUnderFlexibleConfig with _ScrollUnderFlexibleConfig {
76+
_MediumScrollUnderFlexibleConfig(this.context);
77+
78+
final BuildContext context;
79+
late final ThemeData _theme = Theme.of(context);
80+
late final ColorScheme _colors = _theme.colorScheme;
81+
late final TextTheme _textTheme = _theme.textTheme;
82+
83+
static const double collapsedHeight = ${number(TokenAppBarSmall.containerHeight)};
84+
static const double expandedHeight = ${number(TokenAppBarMedium.containerHeight)};
85+
86+
@override
87+
TextStyle? get collapsedTextStyle =>
88+
_textTheme.titleLarge?.apply(color: ${color(TokenAppBar.titleColor, '_colors')});
89+
90+
@override
91+
TextStyle? get expandedTextStyle =>
92+
_textTheme.headlineSmall?.apply(color: ${color(TokenAppBar.titleColor, '_colors')});
93+
94+
@override
95+
EdgeInsetsGeometry get expandedTitlePadding => const EdgeInsets.fromLTRB(16, 0, 16, 20);
96+
}
97+
98+
class _LargeScrollUnderFlexibleConfig with _ScrollUnderFlexibleConfig {
99+
_LargeScrollUnderFlexibleConfig(this.context);
100+
101+
final BuildContext context;
102+
late final ThemeData _theme = Theme.of(context);
103+
late final ColorScheme _colors = _theme.colorScheme;
104+
late final TextTheme _textTheme = _theme.textTheme;
105+
106+
static const double collapsedHeight = ${number(TokenAppBarSmall.containerHeight)};
107+
static const double expandedHeight = ${number(TokenAppBarLarge.containerHeight)};
108+
109+
@override
110+
TextStyle? get collapsedTextStyle =>
111+
_textTheme.titleLarge?.apply(color: ${color(TokenAppBar.titleColor, '_colors')});
112+
113+
@override
114+
TextStyle? get expandedTextStyle =>
115+
_textTheme.headlineMedium?.apply(color: ${color(TokenAppBar.titleColor, '_colors')});
116+
117+
@override
118+
EdgeInsetsGeometry get expandedTitlePadding => const EdgeInsets.fromLTRB(16, 0, 16, 28);
119+
}
120+
''';
121+
}

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

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

0 commit comments

Comments
 (0)