Skip to content

Commit 5de92e4

Browse files
authored
[material_ui] Migrate M3 SearchBar template to use new gen_defaults (flutter#13216)
Work toward flutter/flutter#187899. Fixes flutter/flutter#188430. This PR migrates the M3 SearchBar 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 3bd8bac commit 5de92e4

5 files changed

Lines changed: 44 additions & 112 deletions

File tree

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/search_bar_defaults.g.dart renamed to packages/material_ui/lib/src/generated/search_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 '../search_anchor.dart';
9+
510
class _SearchBarDefaultsM3 extends SearchBarThemeData {
611
_SearchBarDefaultsM3(this.context);
712

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

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import 'text_field.dart';
3535
import 'text_theme.dart';
3636
import 'theme.dart';
3737

38+
part 'generated/search_bar_defaults_m3.g.dart';
39+
3840
const int _kOpenViewMilliseconds = 600;
3941
const Duration _kOpenViewDuration = Duration(milliseconds: _kOpenViewMilliseconds);
4042
const Duration _kAnchorFadeDuration = Duration(milliseconds: 150);
@@ -1974,81 +1976,6 @@ class _SearchBarState extends State<SearchBar> {
19741976
}
19751977
}
19761978

1977-
// BEGIN GENERATED TOKEN PROPERTIES - SearchBar
1978-
1979-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
1980-
// "END GENERATED" comments are generated from data in the Material
1981-
// Design token database by the script:
1982-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
1983-
1984-
// dart format off
1985-
class _SearchBarDefaultsM3 extends SearchBarThemeData {
1986-
_SearchBarDefaultsM3(this.context);
1987-
1988-
final BuildContext context;
1989-
late final ColorScheme _colors = Theme.of(context).colorScheme;
1990-
late final TextTheme _textTheme = Theme.of(context).textTheme;
1991-
1992-
@override
1993-
WidgetStateProperty<Color?>? get backgroundColor =>
1994-
MaterialStatePropertyAll<Color>(_colors.surfaceContainerHigh);
1995-
1996-
@override
1997-
WidgetStateProperty<double>? get elevation =>
1998-
const MaterialStatePropertyAll<double>(6.0);
1999-
2000-
@override
2001-
WidgetStateProperty<Color>? get shadowColor =>
2002-
MaterialStatePropertyAll<Color>(_colors.shadow);
2003-
2004-
@override
2005-
WidgetStateProperty<Color>? get surfaceTintColor =>
2006-
const MaterialStatePropertyAll<Color>(Colors.transparent);
2007-
2008-
@override
2009-
WidgetStateProperty<Color?>? get overlayColor =>
2010-
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
2011-
if (states.contains(WidgetState.pressed)) {
2012-
return _colors.onSurface.withOpacity(0.1);
2013-
}
2014-
if (states.contains(WidgetState.hovered)) {
2015-
return _colors.onSurface.withOpacity(0.08);
2016-
}
2017-
if (states.contains(WidgetState.focused)) {
2018-
return Colors.transparent;
2019-
}
2020-
return Colors.transparent;
2021-
});
2022-
2023-
// No default side
2024-
2025-
@override
2026-
WidgetStateProperty<OutlinedBorder>? get shape =>
2027-
const MaterialStatePropertyAll<OutlinedBorder>(StadiumBorder());
2028-
2029-
@override
2030-
WidgetStateProperty<EdgeInsetsGeometry>? get padding =>
2031-
const MaterialStatePropertyAll<EdgeInsetsGeometry>(EdgeInsets.symmetric(horizontal: 8.0));
2032-
2033-
@override
2034-
WidgetStateProperty<TextStyle?> get textStyle =>
2035-
MaterialStatePropertyAll<TextStyle?>(_textTheme.bodyLarge?.copyWith(color: _colors.onSurface));
2036-
2037-
@override
2038-
WidgetStateProperty<TextStyle?> get hintStyle =>
2039-
MaterialStatePropertyAll<TextStyle?>(_textTheme.bodyLarge?.copyWith(color: _colors.onSurfaceVariant));
2040-
2041-
@override
2042-
BoxConstraints get constraints =>
2043-
const BoxConstraints(minWidth: 360.0, maxWidth: 800.0, minHeight: 56.0);
2044-
2045-
@override
2046-
TextCapitalization get textCapitalization => TextCapitalization.none;
2047-
}
2048-
// dart format on
2049-
2050-
// END GENERATED TOKEN PROPERTIES - SearchBar
2051-
20521979
// BEGIN GENERATED TOKEN PROPERTIES - SearchView
20531980

20541981
// Do not edit by hand. The code between the "BEGIN GENERATED" and

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import '../templates/progress_indicator_template.dart';
4444

4545
// import '../templates/radio_template.dart';
4646
// import '../templates/range_slider_template.dart';
47-
// import '../templates/search_bar_template.dart';
47+
import '../templates/search_bar_template.dart';
4848
// import '../templates/search_view_template.dart';
4949
// import '../templates/segmented_button_template.dart';
5050
// import '../templates/slider_template.dart';
@@ -107,7 +107,7 @@ Future<void> main(List<String> args) async {
107107
const ProgressIndicatorTemplateM3().generateFile(verbose: verbose);
108108
// const RadioTemplateM3().generateFile(verbose: verbose);
109109
// const RangeSliderTemplateM3().generateFile(verbose: verbose);
110-
// const SearchBarTemplateM3().generateFile(verbose: verbose);
110+
const SearchBarTemplateM3().generateFile(verbose: verbose);
111111
// const SearchViewTemplateM3().generateFile(verbose: verbose);
112112
// const SegmentedButtonTemplateM3().generateFile(verbose: verbose);
113113
// const SliderTemplateM3().generateFile(verbose: verbose);

packages/material_ui/tool/gen_defaults/temporarily_excluded/templates/search_bar_template.dart renamed to packages/material_ui/tool/gen_defaults/templates/search_bar_template.dart

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,58 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import '../data/search_bar.dart';
56
import 'template.dart';
67

7-
class SearchBarTemplate extends TokenTemplate {
8-
const SearchBarTemplate(
9-
super.blockName,
10-
super.fileName,
11-
super.tokens, {
12-
super.colorSchemePrefix = '_colors.',
13-
super.textThemePrefix = '_textTheme.',
14-
});
15-
16-
String _surfaceTint() {
17-
final String? color = colorOrTransparent(
18-
'md.comp.search-bar.container.surface-tint-layer.color',
19-
);
20-
final surfaceTintColor = 'MaterialStatePropertyAll<Color>($color);';
21-
if (color == 'Colors.transparent') {
22-
return 'const $surfaceTintColor';
23-
}
24-
return surfaceTintColor;
25-
}
8+
class SearchBarTemplateM3 extends TokenTemplateM3 {
9+
const SearchBarTemplateM3();
2610

2711
@override
28-
String generate() =>
12+
String get name => 'Search Bar';
13+
14+
@override
15+
String get parentFilePath => 'search_anchor.dart';
16+
17+
// TODO(QuncCccccc): Replace this value if a focused state layer token becomes available.
18+
static const String _legacyFocusedStateLayerColor = 'Colors.transparent';
19+
20+
@override
21+
String generateContents(String className) =>
2922
'''
30-
class _SearchBarDefaultsM3 extends SearchBarThemeData {
31-
_SearchBarDefaultsM3(this.context);
23+
class $className extends SearchBarThemeData {
24+
$className(this.context);
3225
3326
final BuildContext context;
3427
late final ColorScheme _colors = Theme.of(context).colorScheme;
3528
late final TextTheme _textTheme = Theme.of(context).textTheme;
3629
3730
@override
3831
WidgetStateProperty<Color?>? get backgroundColor =>
39-
MaterialStatePropertyAll<Color>(${componentColor("md.comp.search-bar.container")});
32+
MaterialStatePropertyAll<Color>(${color(TokenSearchBar.containerColor)});
4033
4134
@override
4235
WidgetStateProperty<double>? get elevation =>
43-
const MaterialStatePropertyAll<double>(${elevation("md.comp.search-bar.container")});
36+
const MaterialStatePropertyAll<double>(${TokenSearchBar.containerElevation});
4437
4538
@override
4639
WidgetStateProperty<Color>? get shadowColor =>
4740
MaterialStatePropertyAll<Color>(_colors.shadow);
4841
4942
@override
5043
WidgetStateProperty<Color>? get surfaceTintColor =>
51-
${_surfaceTint()}
44+
const MaterialStatePropertyAll<Color>(Colors.transparent);
5245
5346
@override
5447
WidgetStateProperty<Color?>? get overlayColor =>
5548
WidgetStateProperty.resolveWith((Set<WidgetState> states) {
5649
if (states.contains(WidgetState.pressed)) {
57-
return ${componentColor("md.comp.search-bar.pressed.state-layer")};
50+
return ${colorWithOpacity(TokenSearchBar.pressedStateLayerColor, TokenSearchBar.pressedStateLayerOpacity)};
5851
}
5952
if (states.contains(WidgetState.hovered)) {
60-
return ${componentColor("md.comp.search-bar.hover.state-layer")};
53+
return ${colorWithOpacity(TokenSearchBar.hoverStateLayerColor, TokenSearchBar.hoverStateLayerOpacity)};
6154
}
6255
if (states.contains(WidgetState.focused)) {
63-
return ${colorOrTransparent("md.comp.search-bar.focused.state-layer")};
56+
return $_legacyFocusedStateLayerColor;
6457
}
6558
return Colors.transparent;
6659
});
@@ -69,23 +62,27 @@ class _SearchBarDefaultsM3 extends SearchBarThemeData {
6962
7063
@override
7164
WidgetStateProperty<OutlinedBorder>? get shape =>
72-
const MaterialStatePropertyAll<OutlinedBorder>(${shape('md.comp.search-bar.container', '')});
65+
const MaterialStatePropertyAll<OutlinedBorder>(${shape(TokenSearchBar.containerShape, '')});
7366
7467
@override
7568
WidgetStateProperty<EdgeInsetsGeometry>? get padding =>
7669
const MaterialStatePropertyAll<EdgeInsetsGeometry>(EdgeInsets.symmetric(horizontal: 8.0));
7770
7871
@override
7972
WidgetStateProperty<TextStyle?> get textStyle =>
80-
MaterialStatePropertyAll<TextStyle?>(${textStyleWithColor('md.comp.search-bar.input-text')});
73+
MaterialStatePropertyAll<TextStyle?>(${textStyle(TokenSearchBar.inputTextType, '_textTheme')}?.copyWith(
74+
color: ${color(TokenSearchBar.inputTextColor)},
75+
));
8176
8277
@override
8378
WidgetStateProperty<TextStyle?> get hintStyle =>
84-
MaterialStatePropertyAll<TextStyle?>(${textStyleWithColor('md.comp.search-bar.supporting-text')});
79+
MaterialStatePropertyAll<TextStyle?>(${textStyle(TokenSearchBar.supportingTextType, '_textTheme')}?.copyWith(
80+
color: ${color(TokenSearchBar.supportingTextColor)},
81+
));
8582
8683
@override
8784
BoxConstraints get constraints =>
88-
const BoxConstraints(minWidth: 360.0, maxWidth: 800.0, minHeight: ${getToken('md.comp.search-bar.container.height')});
85+
const BoxConstraints(minWidth: 360.0, maxWidth: 800.0, minHeight: ${TokenSearchBar.containerHeight});
8986
9087
@override
9188
TextCapitalization get textCapitalization => TextCapitalization.none;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import '../templates/motion_template.dart';
4141
import '../templates/progress_indicator_template.dart';
4242
// import '../templates/radio_template.dart';
4343
// import '../templates/range_slider_template.dart';
44-
// import '../templates/search_bar_template.dart';
44+
import '../templates/search_bar_template.dart';
4545
// import '../templates/search_view_template.dart';
4646
// import '../templates/segmented_button_template.dart';
4747
// import '../templates/slider_template.dart';
@@ -799,8 +799,11 @@ void main() {
799799
});
800800

801801
test('SearchBarTemplateM3 emits M3 SearchBar defaults from tokens', () {
802-
// Intentionally empty, will be implemented during migration. See:
803-
// https://github.com/flutter/flutter/issues/187899
802+
final String contents = _generateContents(const SearchBarTemplateM3());
803+
expect(contents, contains('class _SearchBarDefaultsM3 extends SearchBarThemeData'));
804+
expect(contents, contains('_colors.surfaceContainerHigh'));
805+
expect(contents, contains('_colors.onSurface.withOpacity(0.1)'));
806+
expect(contents, contains('const MaterialStatePropertyAll<OutlinedBorder>(StadiumBorder())'));
804807
});
805808

806809
test('SearchViewTemplateM3 emits M3 SearchView defaults from tokens', () {

0 commit comments

Comments
 (0)