Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions packages/material_ui/lib/src/banner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import 'scaffold.dart';
import 'text_theme.dart';
import 'theme.dart';

part 'generated/banner_defaults_m3.g.dart';

// Examples can assume:
// late BuildContext context;

Expand Down Expand Up @@ -489,35 +491,3 @@ class _BannerDefaultsM2 extends MaterialBannerThemeData {
@override
TextStyle? get contentTextStyle => _theme.textTheme.bodyMedium;
}

// BEGIN GENERATED TOKEN PROPERTIES - Banner

// Do not edit by hand. The code between the "BEGIN GENERATED" and
// "END GENERATED" comments are generated from data in the Material
// Design token database by the script:
// dev/tools/gen_defaults/bin/gen_defaults.dart.

// dart format off
class _BannerDefaultsM3 extends MaterialBannerThemeData {
_BannerDefaultsM3(this.context)
: super(elevation: 1.0);

final BuildContext context;
late final ColorScheme _colors = Theme.of(context).colorScheme;
late final TextTheme _textTheme = Theme.of(context).textTheme;

@override
Color? get backgroundColor => _colors.surfaceContainerLow;

@override
Color? get surfaceTintColor => Colors.transparent;

@override
Color? get dividerColor => _colors.outlineVariant;

@override
TextStyle? get contentTextStyle => _textTheme.bodyMedium;
}
// dart format on

// END GENERATED TOKEN PROPERTIES - Banner
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Do not edit by hand. The code is generated from data in the Material
// Design token database by the script:
// packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart.
part of '../banner.dart';

class _BannerDefaultsM3 extends MaterialBannerThemeData {
_BannerDefaultsM3(this.context) : super(elevation: 1.0);

Expand Down
5 changes: 3 additions & 2 deletions packages/material_ui/tool/gen_defaults/bin/gen_defaults.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import 'package:args/args.dart';
// import '../templates/action_chip_template.dart';
import '../templates/app_bar_template.dart';
// import '../templates/badge_template.dart';
// import '../templates/banner_template.dart';
import '../templates/banner_template.dart';

// import '../templates/bottom_app_bar_template.dart';
// import '../templates/bottom_sheet_template.dart';
// import '../templates/button_template.dart';
Expand Down Expand Up @@ -66,7 +67,7 @@ Future<void> main(List<String> args) async {
// const ActionChipTemplateM3().generateFile(verbose: verbose);
const AppBarTemplateM3().generateFile(verbose: verbose);
// const BadgeTemplateM3().generateFile(verbose: verbose);
// const BannerTemplateM3().generateFile(verbose: verbose);
const BannerTemplateM3().generateFile(verbose: verbose);
// const BottomAppBarTemplateM3().generateFile(verbose: verbose);
// const BottomSheetTemplateM3().generateFile(verbose: verbose);
// const ButtonTemplateM3().generateFile(verbose: verbose);
Expand Down
63 changes: 63 additions & 0 deletions packages/material_ui/tool/gen_defaults/data/banner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2013 The Flutter Authors.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Version: 38.1.12

import 'color_role.dart';
import 'shape_struct.dart';
import 'typescale.dart';
import 'typescale_struct.dart';

class TokenBanner {
/// md.comp.banner.container.color
static const TokenColorRole containerColor = TokenColorRole.surfaceContainerLow;

/// md.comp.banner.container.elevation
static const double containerElevation = 1.00;

/// md.comp.banner.container.shape
static const ShapeStruct containerShape = ShapeStruct(
family: 'SHAPE_FAMILY_ROUNDED_CORNERS',
topLeft: 0.00,
topRight: 0.00,
bottomLeft: 0.00,
bottomRight: 0.00,
);

/// md.comp.banner.desktop.with-single-line.container.height
static const double desktopWithSingleLineContainerHeight = 52.00;

/// md.comp.banner.desktop.with-three-lines.container.height
static const double desktopWithThreeLinesContainerHeight = 90.00;

/// md.comp.banner.desktop.with-two-lines.with-image.container.height
static const double desktopWithTwoLinesWithImageContainerHeight = 72.00;

/// md.comp.banner.mobile.with-single-line.container.height
static const double mobileWithSingleLineContainerHeight = 54.00;

/// md.comp.banner.mobile.with-two-lines.container.height
static const double mobileWithTwoLinesContainerHeight = 112.00;

/// md.comp.banner.mobile.with-two-lines.with-image.container.height
static const double mobileWithTwoLinesWithImageContainerHeight = 120.00;

/// md.comp.banner.supporting-text.color
static const TokenColorRole supportingTextColor = TokenColorRole.onSurfaceVariant;

/// md.comp.banner.supporting-text.type
static const TypescaleStruct supportingTextType = TokenTypescale.bodyMedium;

/// md.comp.banner.with-image.image.shape
static const ShapeStruct withImageImageShape = ShapeStruct(
family: 'SHAPE_FAMILY_CIRCULAR',
topLeft: 0.00,
topRight: 0.00,
bottomLeft: 0.00,
bottomRight: 0.00,
);

/// md.comp.banner.with-image.image.size
static const double withImageImageSize = 40.00;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../data/banner.dart';
import '../data/divider.dart';
import 'template.dart';

class BannerTemplateM3 extends TokenTemplateM3 {
const BannerTemplateM3();

@override
String get name => 'Banner';

@override
String get parentFilePath => 'banner.dart';

@override
String generateContents(String className) =>
'''
class $className extends MaterialBannerThemeData {
$className(this.context)
: super(elevation: ${number(TokenBanner.containerElevation)});

final BuildContext context;
late final ColorScheme _colors = Theme.of(context).colorScheme;
late final TextTheme _textTheme = Theme.of(context).textTheme;

@override
Color? get backgroundColor => ${color(TokenBanner.containerColor, '_colors')};

@override
Color? get surfaceTintColor => Colors.transparent;

@override
Color? get dividerColor => ${color(TokenDivider.color, '_colors')};

@override
TextStyle? get contentTextStyle => ${textStyle(TokenBanner.supportingTextType, '_textTheme')};
}
''';
}
68 changes: 68 additions & 0 deletions packages/material_ui/tool/gen_defaults/templates/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:meta/meta.dart';

import '../data/color_role.dart';
import '../data/shape_struct.dart';
import '../data/typescale.dart';
import '../data/typescale_struct.dart';

enum _MaterialVersion { material3, material3Expressive }

Expand Down Expand Up @@ -110,6 +112,72 @@ abstract class TokenTemplate {
return '${color(role, prefix)}.withOpacity(${number(opacity)})';
}

/// Generates a [TextTheme] expression for the given typescale token.
String textStyle(TypescaleStruct type, String prefix) {
final String? textStyleName = _textStyleName(type);
if (textStyleName == null) {
throw UnsupportedError('Unsupported typescale token.');
}
return '$prefix.$textStyleName';
}

String? _textStyleName(TypescaleStruct type) {
if (_hasSameTypescaleValues(type, TokenTypescale.bodyLarge)) {
return 'bodyLarge';
}
if (_hasSameTypescaleValues(type, TokenTypescale.bodyMedium)) {
return 'bodyMedium';
}
if (_hasSameTypescaleValues(type, TokenTypescale.bodySmall)) {
return 'bodySmall';
}
if (_hasSameTypescaleValues(type, TokenTypescale.displayLarge)) {
return 'displayLarge';
}
if (_hasSameTypescaleValues(type, TokenTypescale.displayMedium)) {
return 'displayMedium';
}
if (_hasSameTypescaleValues(type, TokenTypescale.displaySmall)) {
return 'displaySmall';
}
if (_hasSameTypescaleValues(type, TokenTypescale.headlineLarge)) {
return 'headlineLarge';
}
if (_hasSameTypescaleValues(type, TokenTypescale.headlineMedium)) {
return 'headlineMedium';
}
if (_hasSameTypescaleValues(type, TokenTypescale.headlineSmall)) {
return 'headlineSmall';
}
if (_hasSameTypescaleValues(type, TokenTypescale.labelLarge)) {
return 'labelLarge';
}
if (_hasSameTypescaleValues(type, TokenTypescale.labelMedium)) {
return 'labelMedium';
}
if (_hasSameTypescaleValues(type, TokenTypescale.labelSmall)) {
return 'labelSmall';
}
if (_hasSameTypescaleValues(type, TokenTypescale.titleLarge)) {
return 'titleLarge';
}
if (_hasSameTypescaleValues(type, TokenTypescale.titleMedium)) {
return 'titleMedium';
}
if (_hasSameTypescaleValues(type, TokenTypescale.titleSmall)) {
return 'titleSmall';
}
return null;
}

bool _hasSameTypescaleValues(TypescaleStruct a, TypescaleStruct b) {
return a.fontFamily == b.fontFamily &&
a.fontSize == b.fontSize &&
a.fontWeight == b.fontWeight &&
a.lineHeight == b.lineHeight &&
a.letterSpacing == b.letterSpacing;
}

/// Generates an [OutlinedBorder] expression for a shape token.
///
/// Currently supports:
Expand Down

This file was deleted.

22 changes: 19 additions & 3 deletions packages/material_ui/tool/gen_defaults/test/gen_defaults_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'dart:io';
import 'package:test/test.dart';
import '../data/color_role.dart';
import '../data/shape_struct.dart';
import '../data/typescale.dart';
import '../templates/app_bar_template.dart';
import '../templates/banner_template.dart';
import '../templates/template.dart';
import 'test_fixtures/test_templates.dart';

Expand Down Expand Up @@ -148,6 +150,15 @@ void main() {
);
});

test('textStyle generates text theme expression', () {
final template = IconButtonTemplateM3(testPath());
expect(
template.textStyle(TokenTypescale.labelSmall, 'Theme.of(context).textTheme'),
'Theme.of(context).textTheme.labelSmall',
);
expect(template.textStyle(TokenTypescale.bodyMedium, '_textTheme'), '_textTheme.bodyMedium');
});

test('ActionChipTemplateM3 emits M3 ActionChip defaults from tokens', () {
// Intentionally empty, will be implemented during migration. See:
// https://github.com/flutter/flutter/issues/187899
Expand All @@ -170,9 +181,14 @@ void main() {
// https://github.com/flutter/flutter/issues/187899
});

test('BannerTemplateM3 emits M3 Banner defaults from tokens', () {
// Intentionally empty, will be implemented during migration. See:
// https://github.com/flutter/flutter/issues/187899
test('BannerTemplateM3 emits M3 Banner defaults from banner tokens', () {
final String contents = const BannerTemplateM3().generateContents('_BannerDefaultsM3');
expect(contents, contains('class _BannerDefaultsM3 extends MaterialBannerThemeData'));
expect(contents, contains('elevation: 1.0'));
expect(contents, contains('Color? get backgroundColor => _colors.surfaceContainerLow'));
expect(contents, contains('Color? get surfaceTintColor => Colors.transparent'));
expect(contents, contains('Color? get dividerColor => _colors.outlineVariant'));
expect(contents, contains('TextStyle? get contentTextStyle => _textTheme.bodyMedium'));
});

test('BottomAppBarTemplateM3 emits M3 BottomAppBar defaults from tokens', () {
Expand Down