Skip to content

Commit 695c444

Browse files
authored
[material_ui] Migrate M3 NavigationDrawer template to use new gen_defaults (flutter#13068)
Work toward flutter#187899. Fixes flutter#188424. This PR migrates the M3 NavigationDrawer 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 1e35bca commit 695c444

6 files changed

Lines changed: 100 additions & 143 deletions

File tree

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/navigation_drawer_defaults.g.dart renamed to packages/material_ui/lib/src/generated/navigation_drawer_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 '../navigation_drawer.dart';
9+
510
class _NavigationDrawerDefaultsM3 extends NavigationDrawerThemeData {
611
_NavigationDrawerDefaultsM3(this.context)
712
: super(

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

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import 'navigation_drawer_theme.dart';
1919
import 'text_theme.dart';
2020
import 'theme.dart';
2121

22+
part 'generated/navigation_drawer_defaults_m3.g.dart';
23+
2224
/// Material Design Navigation Drawer component.
2325
///
2426
/// On top of [Drawer]s, Navigation drawers offer a persistent and convenient way to switch
@@ -719,68 +721,3 @@ class _SelectableAnimatedBuilderState extends State<_SelectableAnimatedBuilder>
719721
return widget.builder(context, _controller);
720722
}
721723
}
722-
723-
// BEGIN GENERATED TOKEN PROPERTIES - NavigationDrawer
724-
725-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
726-
// "END GENERATED" comments are generated from data in the Material
727-
// Design token database by the script:
728-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
729-
730-
// dart format off
731-
class _NavigationDrawerDefaultsM3 extends NavigationDrawerThemeData {
732-
_NavigationDrawerDefaultsM3(this.context)
733-
: super(
734-
elevation: 1.0,
735-
tileHeight: 56.0,
736-
indicatorShape: const StadiumBorder(),
737-
indicatorSize: const Size(336.0, 56.0),
738-
);
739-
740-
final BuildContext context;
741-
late final ColorScheme _colors = Theme.of(context).colorScheme;
742-
late final TextTheme _textTheme = Theme.of(context).textTheme;
743-
744-
@override
745-
Color? get backgroundColor => _colors.surfaceContainerLow;
746-
747-
@override
748-
Color? get surfaceTintColor => Colors.transparent;
749-
750-
@override
751-
Color? get shadowColor => Colors.transparent;
752-
753-
@override
754-
Color? get indicatorColor => _colors.secondaryContainer;
755-
756-
@override
757-
WidgetStateProperty<IconThemeData?>? get iconTheme {
758-
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
759-
return IconThemeData(
760-
size: 24.0,
761-
color: states.contains(WidgetState.disabled)
762-
? _colors.onSurfaceVariant.withOpacity(0.38)
763-
: states.contains(WidgetState.selected)
764-
? _colors.onSecondaryContainer
765-
: _colors.onSurfaceVariant,
766-
);
767-
});
768-
}
769-
770-
@override
771-
WidgetStateProperty<TextStyle?>? get labelTextStyle {
772-
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
773-
final TextStyle style = _textTheme.labelLarge!;
774-
return style.apply(
775-
color: states.contains(WidgetState.disabled)
776-
? _colors.onSurfaceVariant.withOpacity(0.38)
777-
: states.contains(WidgetState.selected)
778-
? _colors.onSecondaryContainer
779-
: _colors.onSurfaceVariant,
780-
);
781-
});
782-
}
783-
}
784-
// dart format on
785-
786-
// END GENERATED TOKEN PROPERTIES - NavigationDrawer

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import '../templates/list_tile_template.dart';
3737
import '../templates/menu_template.dart';
3838
import '../templates/motion_template.dart';
3939
// import '../templates/navigation_bar_template.dart';
40-
// import '../templates/navigation_drawer_template.dart';
40+
import '../templates/navigation_drawer_template.dart';
4141
import '../templates/navigation_rail_template.dart';
4242

4343
import '../templates/popup_menu_template.dart';
@@ -102,7 +102,7 @@ Future<void> main(List<String> args) async {
102102
const MenuTemplateM3().generateFile(verbose: verbose);
103103
const MotionTemplateM3().generateFile(verbose: verbose);
104104
// const NavigationBarTemplateM3().generateFile(verbose: verbose);
105-
// const NavigationDrawerTemplateM3().generateFile(verbose: verbose);
105+
const NavigationDrawerTemplateM3().generateFile(verbose: verbose);
106106
const NavigationRailTemplateM3().generateFile(verbose: verbose);
107107
const PopupMenuTemplateM3().generateFile(verbose: verbose);
108108
const ProgressIndicatorTemplateM3().generateFile(verbose: verbose);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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/color_role.dart';
6+
import '../data/navigation_drawer.dart';
7+
import 'template.dart';
8+
9+
class NavigationDrawerTemplateM3 extends TokenTemplateM3 {
10+
const NavigationDrawerTemplateM3();
11+
12+
@override
13+
String get name => 'Navigation Drawer';
14+
15+
@override
16+
String get parentFilePath => 'navigation_drawer.dart';
17+
18+
// TODO(QuncCccccc): Replace these with token values once disabled-state
19+
// tokens are available.
20+
static const TokenColorRole _legacyDisabledColor = TokenColorRole.onSurfaceVariant;
21+
static const double _legacyDisabledOpacity = 0.38;
22+
23+
@override
24+
String generateContents(String className) =>
25+
'''
26+
class $className extends NavigationDrawerThemeData {
27+
$className(this.context)
28+
: super(
29+
elevation: ${number(TokenNavigationDrawer.modalContainerElevation)},
30+
tileHeight: ${number(TokenNavigationDrawer.activeIndicatorHeight)},
31+
indicatorShape: ${shape(TokenNavigationDrawer.activeIndicatorShape)},
32+
indicatorSize: const Size(${number(TokenNavigationDrawer.activeIndicatorWidth)}, ${number(TokenNavigationDrawer.activeIndicatorHeight)}),
33+
);
34+
35+
final BuildContext context;
36+
late final ColorScheme _colors = Theme.of(context).colorScheme;
37+
late final TextTheme _textTheme = Theme.of(context).textTheme;
38+
39+
@override
40+
Color? get backgroundColor => ${color(TokenNavigationDrawer.modalContainerColor)};
41+
42+
@override
43+
Color? get surfaceTintColor => Colors.transparent;
44+
45+
@override
46+
Color? get shadowColor => Colors.transparent;
47+
48+
@override
49+
Color? get indicatorColor => ${color(TokenNavigationDrawer.activeIndicatorColor)};
50+
51+
@override
52+
WidgetStateProperty<IconThemeData?>? get iconTheme {
53+
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
54+
return IconThemeData(
55+
size: ${number(TokenNavigationDrawer.iconSize)},
56+
color: states.contains(WidgetState.disabled)
57+
? ${colorWithOpacity(_legacyDisabledColor, _legacyDisabledOpacity)}
58+
: states.contains(WidgetState.selected)
59+
? ${color(TokenNavigationDrawer.activeIconColor)}
60+
: ${color(TokenNavigationDrawer.inactiveIconColor)},
61+
);
62+
});
63+
}
64+
65+
@override
66+
WidgetStateProperty<TextStyle?>? get labelTextStyle {
67+
return WidgetStateProperty.resolveWith((Set<WidgetState> states) {
68+
final TextStyle style = ${textStyle(TokenNavigationDrawer.labelTextType, '_textTheme')}!;
69+
return style.apply(
70+
color: states.contains(WidgetState.disabled)
71+
? ${colorWithOpacity(_legacyDisabledColor, _legacyDisabledOpacity)}
72+
: states.contains(WidgetState.selected)
73+
? ${color(TokenNavigationDrawer.activeLabelTextColor)}
74+
: ${color(TokenNavigationDrawer.inactiveLabelTextColor)},
75+
);
76+
});
77+
}
78+
}
79+
''';
80+
}

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

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

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import '../templates/list_tile_template.dart';
3535
import '../templates/menu_template.dart';
3636
import '../templates/motion_template.dart';
3737
// import '../templates/navigation_bar_template.dart';
38-
// import '../templates/navigation_drawer_template.dart';
38+
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';
@@ -735,8 +735,16 @@ void main() {
735735
});
736736

737737
test('NavigationDrawerTemplateM3 emits M3 NavigationDrawer defaults from tokens', () {
738-
// Intentionally empty, will be implemented during migration. See:
739-
// https://github.com/flutter/flutter/issues/187899
738+
const template = NavigationDrawerTemplateM3();
739+
final String contents = _generateContents(template);
740+
expect(
741+
contents,
742+
contains('class _NavigationDrawerDefaultsM3 extends NavigationDrawerThemeData'),
743+
);
744+
expect(contents, contains('indicatorSize: const Size(336.0, 56.0)'));
745+
expect(contents, contains('_colors.surfaceContainerLow'));
746+
expect(contents, contains('_colors.onSurfaceVariant.withOpacity(0.38)'));
747+
expect(contents, contains('_textTheme.labelLarge!'));
740748
});
741749

742750
test('NavigationRailTemplateM3 emits M3 NavigationRail defaults from tokens', () {

0 commit comments

Comments
 (0)