Skip to content
Merged
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
2 changes: 2 additions & 0 deletions dev/tools/gen_defaults/bin/gen_defaults.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:gen_defaults/fab_template.dart';
import 'package:gen_defaults/typography_template.dart';

Map<String, dynamic> _readTokenFile(String fileName) {
return jsonDecode(File('dev/tools/gen_defaults/data/$fileName').readAsStringSync()) as Map<String, dynamic>;
Expand Down Expand Up @@ -70,4 +71,5 @@ Future<void> main(List<String> args) async {
}

FABTemplate('$materialLib/floating_action_button.dart', tokens).updateFile();
TypographyTemplate('$materialLib/typography.dart', tokens).updateFile();
}
3 changes: 1 addition & 2 deletions dev/tools/gen_defaults/lib/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ abstract class TokenTemplate {
}

String textStyle(String tokenName) {
final String fontName = '$tokenName.text-style';
return tokens[fontName]!.toString();
return tokens['$tokenName.text-style']!.toString();
}
}
76 changes: 76 additions & 0 deletions dev/tools/gen_defaults/lib/typography_template.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'template.dart';

class TypographyTemplate extends TokenTemplate {
const TypographyTemplate(String fileName, Map<String, dynamic> tokens) : super(fileName, tokens);

@override
String generate() => '''
// Generated version ${tokens["version"]}
class _M3Typography {
_M3Typography._();

${_textTheme('englishLike', 'alphabetic')}

${_textTheme('dense', 'ideographic')}

${_textTheme('tall', 'alphabetic')}
}
''';

String _textTheme(String name, String baseline) {
final StringBuffer theme = StringBuffer('static const TextTheme $name = TextTheme(\n');
theme.writeln(' displayLarge: ${_textStyleDef('md.sys.typescale.display-large', '$name displayLarge 2021', baseline)},');
theme.writeln(' displayMedium: ${_textStyleDef('md.sys.typescale.display-medium', '$name displayMedium 2021', baseline)},');
theme.writeln(' displaySmall: ${_textStyleDef('md.sys.typescale.display-small', '$name displaySmall 2021', baseline)},');
theme.writeln(' headlineLarge: ${_textStyleDef('md.sys.typescale.headline-large', '$name headlineLarge 2021', baseline)},');
theme.writeln(' headlineMedium: ${_textStyleDef('md.sys.typescale.headline-medium', '$name headlineMedium 2021', baseline)},');
theme.writeln(' headlineSmall: ${_textStyleDef('md.sys.typescale.headline-small', '$name headlineSmall 2021', baseline)},');
theme.writeln(' titleLarge: ${_textStyleDef('md.sys.typescale.title-large', '$name titleLarge 2021', baseline)},');
theme.writeln(' titleMedium: ${_textStyleDef('md.sys.typescale.title-medium', '$name titleMedium 2021', baseline)},');
theme.writeln(' titleSmall: ${_textStyleDef('md.sys.typescale.title-small', '$name titleSmall 2021', baseline)},');
theme.writeln(' labelLarge: ${_textStyleDef('md.sys.typescale.label-large', '$name labelLarge 2021', baseline)},');
theme.writeln(' labelMedium: ${_textStyleDef('md.sys.typescale.label-medium', '$name labelMedium 2021', baseline)},');
theme.writeln(' labelSmall: ${_textStyleDef('md.sys.typescale.label-small', '$name labelSmall 2021', baseline)},');
theme.writeln(' bodyLarge: ${_textStyleDef('md.sys.typescale.body-large', '$name bodyLarge 2021', baseline)},');
theme.writeln(' bodyMedium: ${_textStyleDef('md.sys.typescale.body-medium', '$name bodyMedium 2021', baseline)},');
theme.writeln(' bodySmall: ${_textStyleDef('md.sys.typescale.body-small', '$name bodySmall 2021', baseline)},');
theme.write(' );');
return theme.toString();
}

String _textStyleDef(String tokenName, String debugLabel, String baseline) {
final StringBuffer style = StringBuffer("TextStyle(debugLabel: '$debugLabel'");
style.write(', inherit: false');
style.write(', fontSize: ${_fontSize(tokenName)}');
style.write(', fontWeight: ${_fontWeight(tokenName)}');
style.write(', letterSpacing: ${_fontSpacing(tokenName)}');
style.write(', height: ${_fontHeight(tokenName)}');
style.write(', textBaseline: TextBaseline.$baseline');
style.write(', leadingDistribution: TextLeadingDistribution.even');
style.write(')');
return style.toString();
}

String _fontSize(String textStyleTokenName) {
return tokens['$textStyleTokenName.size']!.toString();
}

String _fontWeight(String textStyleTokenName) {
final String weightValue = tokens[tokens['$textStyleTokenName.weight']!]!.toString();
return 'FontWeight.w$weightValue';
}

String _fontSpacing(String textStyleTokenName) {
return tokens['$textStyleTokenName.tracking']!.toString();
}

String _fontHeight(String textStyleTokenName) {
final double size = tokens['$textStyleTokenName.size']! as double;
final double lineHeight = tokens['$textStyleTokenName.line-height']! as double;
return (lineHeight / size).toStringAsFixed(2);
}
}
5 changes: 4 additions & 1 deletion packages/flutter/lib/src/material/theme_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ class ThemeData with Diagnosticable {
splashColor ??= isDark ? _kDarkThemeSplashColor : _kLightThemeSplashColor;

// TYPOGRAPHY & ICONOGRAPHY
typography ??= Typography.material2014(platform: platform);
typography ??= useMaterial3 ? Typography.material2021(platform: platform) : Typography.material2014(platform: platform);
TextTheme defaultTextTheme = isDark ? typography.white : typography.black;
TextTheme defaultPrimaryTextTheme = primaryIsDark ? typography.white : typography.black;
TextTheme defaultAccentTextTheme = accentIsDark ? typography.white : typography.black;
Expand Down Expand Up @@ -1134,6 +1134,9 @@ class ThemeData with Diagnosticable {
/// start using new colors, typography and other features of Material 3.
/// If false, they will use the Material 2 look and feel.
///
/// If true, the default Typography will be [Typography.material2021],
/// otherwise it will default to [Typography.material2014].
///
/// During the migration to Material 3, turning this on may yield
/// inconsistent look and feel in your app. Some components will be migrated
/// before others and typography changes will be coming in stages.
Expand Down
Loading