Skip to content

Commit 4e6f88a

Browse files
authored
[material_ui] Migrate M3 TextField template to use new gen_defaults (#13275)
Work toward flutter/flutter#187899. Fixes flutter/flutter#188438. This PR migrates the M3 `TextField` defaults template to use the new gen_defaults in material_ui. The generated defaults remain unchanged. ## Pre-Review Checklist
1 parent cc839b7 commit 4e6f88a

6 files changed

Lines changed: 60 additions & 53 deletions

File tree

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/text_field_defaults.g.dart renamed to packages/material_ui/lib/src/generated/text_field_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 '../text_field.dart';
9+
510
TextStyle? _m3StateInputStyle(BuildContext context) =>
611
WidgetStateTextStyle.resolveWith((Set<WidgetState> states) {
712
if (states.contains(WidgetState.disabled)) {

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import 'theme.dart';
3434
export 'package:flutter/services.dart'
3535
show SmartDashesType, SmartQuotesType, TextCapitalization, TextInputAction, TextInputType;
3636

37+
part 'generated/text_field_defaults_m3.g.dart';
38+
3739
// Examples can assume:
3840
// late BuildContext context;
3941
// late FocusNode myFocusNode;
@@ -1909,26 +1911,3 @@ TextStyle? _m2StateInputStyle(BuildContext context) =>
19091911

19101912
TextStyle _m2CounterErrorStyle(BuildContext context) =>
19111913
Theme.of(context).textTheme.bodySmall!.copyWith(color: Theme.of(context).colorScheme.error);
1912-
1913-
// BEGIN GENERATED TOKEN PROPERTIES - TextField
1914-
1915-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
1916-
// "END GENERATED" comments are generated from data in the Material
1917-
// Design token database by the script:
1918-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
1919-
1920-
// dart format off
1921-
TextStyle? _m3StateInputStyle(BuildContext context) => WidgetStateTextStyle.resolveWith((Set<WidgetState> states) {
1922-
if (states.contains(WidgetState.disabled)) {
1923-
return TextStyle(color: Theme.of(context).textTheme.bodyLarge!.color?.withOpacity(0.38));
1924-
}
1925-
return TextStyle(color: Theme.of(context).textTheme.bodyLarge!.color);
1926-
});
1927-
1928-
TextStyle _m3InputStyle(BuildContext context) => Theme.of(context).textTheme.bodyLarge!;
1929-
1930-
TextStyle _m3CounterErrorStyle(BuildContext context) =>
1931-
Theme.of(context).textTheme.bodySmall!.copyWith(color: Theme.of(context).colorScheme.error);
1932-
// dart format on
1933-
1934-
// END GENERATED TOKEN PROPERTIES - TextField

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import '../templates/snackbar_template.dart';
5454
// import '../templates/switch_template.dart';
5555
import '../templates/tabs_template.dart';
5656

57-
// import '../templates/text_field_template.dart';
57+
import '../templates/text_field_template.dart';
5858
// import '../templates/time_picker_template.dart';
5959
import '../templates/typography_template.dart';
6060

@@ -116,7 +116,7 @@ Future<void> main(List<String> args) async {
116116
// const SurfaceTintTemplateM3().generateFile(verbose: verbose);
117117
// const SwitchTemplateM3().generateFile(verbose: verbose);
118118
const TabsTemplateM3().generateFile(verbose: verbose);
119-
// const TextFieldTemplateM3().generateFile(verbose: verbose);
119+
const TextFieldTemplateM3().generateFile(verbose: verbose);
120120
// const TimePickerTemplateM3().generateFile(verbose: verbose);
121121
const TypographyTemplateM3().generateFile(verbose: verbose);
122122
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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/filled_text_field.dart';
6+
import 'template.dart';
7+
8+
class TextFieldTemplateM3 extends TokenTemplateM3 {
9+
const TextFieldTemplateM3();
10+
11+
@override
12+
String get name => 'Text Field';
13+
14+
@override
15+
String get parentFilePath => 'text_field.dart';
16+
17+
@override
18+
String get className => '';
19+
20+
@override
21+
String generateContents(String className) =>
22+
'''
23+
TextStyle? _m3StateInputStyle(BuildContext context) => WidgetStateTextStyle.resolveWith((Set<WidgetState> states) {
24+
if (states.contains(WidgetState.disabled)) {
25+
return TextStyle(color: ${textStyle(TokenFilledTextField.labelTextType, 'Theme.of(context).textTheme')}!.color?.withOpacity(${TokenFilledTextField.disabledLabelTextOpacity}));
26+
}
27+
return TextStyle(color: ${textStyle(TokenFilledTextField.labelTextType, 'Theme.of(context).textTheme')}!.color);
28+
});
29+
30+
TextStyle _m3InputStyle(BuildContext context) => ${textStyle(TokenFilledTextField.labelTextType, 'Theme.of(context).textTheme')}!;
31+
32+
TextStyle _m3CounterErrorStyle(BuildContext context) =>
33+
${textStyle(TokenFilledTextField.supportingTextType, 'Theme.of(context).textTheme')}!.copyWith(color: ${color(TokenFilledTextField.errorSupportingTextColor, 'Theme.of(context).colorScheme')});
34+
''';
35+
}

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

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

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import '../templates/snackbar_template.dart';
5050
// import '../templates/switch_template.dart';
5151
import '../templates/tabs_template.dart';
5252
import '../templates/template.dart';
53-
// import '../templates/text_field_template.dart';
53+
import '../templates/text_field_template.dart';
5454
// import '../templates/time_picker_template.dart';
5555
import '../templates/typography_template.dart';
5656
import 'test_fixtures/test_templates.dart';
@@ -874,8 +874,21 @@ void main() {
874874
});
875875

876876
test('TextFieldTemplateM3 emits M3 TextField defaults from tokens', () {
877-
// Intentionally empty, will be implemented during migration. See:
878-
// https://github.com/flutter/flutter/issues/187899
877+
final String contents = _generateContents(const TextFieldTemplateM3());
878+
expect(contents, contains('TextStyle? _m3StateInputStyle(BuildContext context)'));
879+
expect(contents, contains('Theme.of(context).textTheme.bodyLarge!.color?.withOpacity(0.38)'));
880+
expect(
881+
contents,
882+
contains(
883+
'TextStyle _m3InputStyle(BuildContext context) => Theme.of(context).textTheme.bodyLarge!',
884+
),
885+
);
886+
expect(
887+
contents,
888+
contains(
889+
'Theme.of(context).textTheme.bodySmall!.copyWith(color: Theme.of(context).colorScheme.error)',
890+
),
891+
);
879892
});
880893

881894
test('TimePickerTemplateM3 emits M3 TimePicker defaults from tokens', () {

0 commit comments

Comments
 (0)