Skip to content

Commit bbc191c

Browse files
authored
[material_ui] Migrate M3 FAB template to use new gen_defaults (flutter#12921)
Work toward flutter#187899. Fixes flutter#188415. This PR migrates the M3 FloatingActionButton defaults template to use the new `gen_defaults` in `material_ui`. The generated defaults remain unchanged. ## Pre-Review Checklist
1 parent af73549 commit bbc191c

6 files changed

Lines changed: 104 additions & 155 deletions

File tree

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

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import 'text_theme.dart';
2121
import 'theme.dart';
2222
import 'tooltip.dart';
2323

24+
part 'generated/fab_defaults_m3.g.dart';
25+
2426
class _DefaultHeroTag {
2527
const _DefaultHeroTag();
2628
@override
@@ -781,74 +783,3 @@ class _FABDefaultsM2 extends FloatingActionButtonThemeData {
781783
@override
782784
TextStyle? get extendedTextStyle => _theme.textTheme.labelLarge!.copyWith(letterSpacing: 1.2);
783785
}
784-
785-
// BEGIN GENERATED TOKEN PROPERTIES - FAB
786-
787-
// Do not edit by hand. The code between the "BEGIN GENERATED" and
788-
// "END GENERATED" comments are generated from data in the Material
789-
// Design token database by the script:
790-
// dev/tools/gen_defaults/bin/gen_defaults.dart.
791-
792-
// dart format off
793-
class _FABDefaultsM3 extends FloatingActionButtonThemeData {
794-
_FABDefaultsM3(this.context, this.type, this.hasChild)
795-
: super(
796-
elevation: 6.0,
797-
focusElevation: 6.0,
798-
hoverElevation: 8.0,
799-
highlightElevation: 6.0,
800-
enableFeedback: true,
801-
sizeConstraints: const BoxConstraints.tightFor(
802-
width: 56.0,
803-
height: 56.0,
804-
),
805-
smallSizeConstraints: const BoxConstraints.tightFor(
806-
width: 40.0,
807-
height: 40.0,
808-
),
809-
largeSizeConstraints: const BoxConstraints.tightFor(
810-
width: 96.0,
811-
height: 96.0,
812-
),
813-
extendedSizeConstraints: const BoxConstraints.tightFor(
814-
height: 56.0,
815-
),
816-
extendedIconLabelSpacing: 8.0,
817-
);
818-
819-
final BuildContext context;
820-
final _FloatingActionButtonType type;
821-
final bool hasChild;
822-
late final ColorScheme _colors = Theme.of(context).colorScheme;
823-
late final TextTheme _textTheme = Theme.of(context).textTheme;
824-
825-
bool get _isExtended => type == _FloatingActionButtonType.extended;
826-
827-
@override Color? get foregroundColor => _colors.onPrimaryContainer;
828-
@override Color? get backgroundColor => _colors.primaryContainer;
829-
@override Color? get splashColor => _colors.onPrimaryContainer.withOpacity(0.1);
830-
@override Color? get focusColor => _colors.onPrimaryContainer.withOpacity(0.1);
831-
@override Color? get hoverColor => _colors.onPrimaryContainer.withOpacity(0.08);
832-
833-
@override
834-
ShapeBorder? get shape => switch (type) {
835-
_FloatingActionButtonType.regular => const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))),
836-
_FloatingActionButtonType.small => const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0))),
837-
_FloatingActionButtonType.large => const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(28.0))),
838-
_FloatingActionButtonType.extended => const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))),
839-
};
840-
841-
@override
842-
double? get iconSize => switch (type) {
843-
_FloatingActionButtonType.regular => 24.0,
844-
_FloatingActionButtonType.small => 24.0,
845-
_FloatingActionButtonType.large => 36.0,
846-
_FloatingActionButtonType.extended => 24.0,
847-
};
848-
849-
@override EdgeInsetsGeometry? get extendedPadding => EdgeInsetsDirectional.only(start: hasChild && _isExtended ? 16.0 : 20.0, end: 20.0);
850-
@override TextStyle? get extendedTextStyle => _textTheme.labelLarge;
851-
}
852-
// dart format on
853-
854-
// END GENERATED TOKEN PROPERTIES - FAB

packages/material_ui/tool/gen_defaults/temporarily_excluded/generated/fab_defaults.g.dart renamed to packages/material_ui/lib/src/generated/fab_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 '../floating_action_button.dart';
9+
510
class _FABDefaultsM3 extends FloatingActionButtonThemeData {
611
_FABDefaultsM3(this.context, this.type, this.hasChild)
712
: super(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import '../templates/date_picker_template.dart';
2929
import '../templates/divider_template.dart';
3030
import '../templates/drawer_template.dart';
3131
import '../templates/expansion_tile_template.dart';
32-
// import '../templates/fab_template.dart';
32+
import '../templates/fab_template.dart';
3333
import '../templates/filter_chip_template.dart';
3434
import '../templates/icon_button_template.dart';
3535
import '../templates/input_chip_template.dart';
@@ -87,7 +87,7 @@ Future<void> main(List<String> args) async {
8787
const DividerTemplateM3().generateFile(verbose: verbose);
8888
const DrawerTemplateM3().generateFile(verbose: verbose);
8989
const ExpansionTileTemplateM3().generateFile(verbose: verbose);
90-
// const FabTemplateM3().generateFile(verbose: verbose);
90+
const FabTemplateM3().generateFile(verbose: verbose);
9191
const FilterChipTemplateM3().generateFile(verbose: verbose);
9292
// const IconButtonTemplateM3().generateFile(verbose: verbose);
9393
const IconButtonTemplateM3E('Icon Button').generateFile(verbose: verbose);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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/extended_fab.dart';
6+
import '../data/fab.dart';
7+
import '../data/fab_large.dart';
8+
import '../data/fab_primary_container.dart';
9+
import '../data/fab_small.dart';
10+
import 'template.dart';
11+
12+
class FabTemplateM3 extends TokenTemplateM3 {
13+
const FabTemplateM3();
14+
15+
@override
16+
String get name => 'FAB';
17+
18+
@override
19+
String get parentFilePath => 'floating_action_button.dart';
20+
21+
@override
22+
String get className => '_FABDefaultsM3';
23+
24+
@override
25+
String generateContents(String className) =>
26+
'''
27+
class $className extends FloatingActionButtonThemeData {
28+
$className(this.context, this.type, this.hasChild)
29+
: super(
30+
elevation: ${number(TokenFabPrimaryContainer.containerElevation)},
31+
focusElevation: ${number(TokenFabPrimaryContainer.focusedContainerElevation)},
32+
hoverElevation: ${number(TokenFabPrimaryContainer.hoveredContainerElevation)},
33+
highlightElevation: ${number(TokenFabPrimaryContainer.pressedContainerElevation)},
34+
enableFeedback: true,
35+
sizeConstraints: const BoxConstraints.tightFor(
36+
width: ${number(TokenFab.containerWidth)},
37+
height: ${number(TokenFab.containerHeight)},
38+
),
39+
smallSizeConstraints: const BoxConstraints.tightFor(
40+
width: ${number(TokenFabSmall.containerWidth)},
41+
height: ${number(TokenFabSmall.containerHeight)},
42+
),
43+
largeSizeConstraints: const BoxConstraints.tightFor(
44+
width: ${number(TokenFabLarge.containerWidth)},
45+
height: ${number(TokenFabLarge.containerHeight)},
46+
),
47+
extendedSizeConstraints: const BoxConstraints.tightFor(
48+
height: ${number(TokenExtendedFab.containerHeight)},
49+
),
50+
extendedIconLabelSpacing: 8.0,
51+
);
52+
53+
final BuildContext context;
54+
final _FloatingActionButtonType type;
55+
final bool hasChild;
56+
late final ColorScheme _colors = Theme.of(context).colorScheme;
57+
late final TextTheme _textTheme = Theme.of(context).textTheme;
58+
59+
bool get _isExtended => type == _FloatingActionButtonType.extended;
60+
61+
@override Color? get foregroundColor => ${color(TokenFabPrimaryContainer.iconColor)};
62+
@override Color? get backgroundColor => ${color(TokenFabPrimaryContainer.containerColor)};
63+
@override Color? get splashColor => ${colorWithOpacity(TokenFabPrimaryContainer.pressedStateLayerColor, TokenFabPrimaryContainer.pressedStateLayerOpacity)};
64+
@override Color? get focusColor => ${colorWithOpacity(TokenFabPrimaryContainer.focusedStateLayerColor, TokenFabPrimaryContainer.focusedStateLayerOpacity)};
65+
@override Color? get hoverColor => ${colorWithOpacity(TokenFabPrimaryContainer.hoveredStateLayerColor, TokenFabPrimaryContainer.hoveredStateLayerOpacity)};
66+
67+
@override
68+
ShapeBorder? get shape => switch (type) {
69+
_FloatingActionButtonType.regular => ${shape(TokenFab.containerShape)},
70+
_FloatingActionButtonType.small => ${shape(TokenFabSmall.containerShape)},
71+
_FloatingActionButtonType.large => ${shape(TokenFabLarge.containerShape)},
72+
_FloatingActionButtonType.extended => ${shape(TokenExtendedFab.containerShape)},
73+
};
74+
75+
@override
76+
double? get iconSize => switch (type) {
77+
_FloatingActionButtonType.regular => ${number(TokenFab.iconSize)},
78+
_FloatingActionButtonType.small => ${number(TokenFabSmall.iconSize)},
79+
_FloatingActionButtonType.large => ${number(TokenFabLarge.iconSize)},
80+
_FloatingActionButtonType.extended => ${number(TokenExtendedFab.iconSize)},
81+
};
82+
83+
@override EdgeInsetsGeometry? get extendedPadding => EdgeInsetsDirectional.only(start: hasChild && _isExtended ? ${number(TokenExtendedFab.leadingSpace)} : ${number(TokenExtendedFab.trailingSpace)}, end: ${number(TokenExtendedFab.trailingSpace)});
84+
@override TextStyle? get extendedTextStyle => ${textStyle(TokenExtendedFab.labelText, '_textTheme')};
85+
}
86+
''';
87+
}

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

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

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import '../templates/date_picker_template.dart';
2525
import '../templates/divider_template.dart';
2626
import '../templates/drawer_template.dart';
2727
import '../templates/expansion_tile_template.dart';
28-
// import '../templates/fab_template.dart';
28+
import '../templates/fab_template.dart';
2929
import '../templates/filter_chip_template.dart';
3030
import '../templates/icon_button_template.dart' as icon_button;
3131
import '../templates/input_chip_template.dart';
@@ -593,8 +593,13 @@ void main() {
593593
});
594594

595595
test('FabTemplateM3 emits M3 Fab defaults from tokens', () {
596-
// Intentionally empty, will be implemented during migration. See:
597-
// https://github.com/flutter/flutter/issues/187899
596+
const template = FabTemplateM3();
597+
expect(template.className, '_FABDefaultsM3');
598+
599+
final String contents = _generateContents(template);
600+
expect(contents, contains('Color? get backgroundColor => _colors.primaryContainer'));
601+
expect(contents, contains(RegExp(r'_FloatingActionButtonType\.large\s+=> 36\.0')));
602+
expect(contents, contains('TextStyle? get extendedTextStyle => _textTheme.labelLarge'));
598603
});
599604

600605
test('FilterChipTemplateM3 emits M3 FilterChip defaults from tokens', () {

0 commit comments

Comments
 (0)