mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

## Description This improves defaults generation with logging, stats, and token validation. This PR includes these changes: * introduce `TokenLogger`, with a verbose mode * prints versions and tokens usage to the console * outputs `generated/used_tokens.csv`, a list of all used tokens, for use by Google * find token files in `data` automatically * hide tokens `Map` * tokens can be obtained using existing resolvers (e.g. `color`, `shape`), or directly through `getToken`. * tokens can be checked for existence with `tokenAvailable` * remove version from template, since the tokens are aggregated and multiple versions are possible (as is the case currently), it does not make sense to attribute a single version * improve documentation ## Related Issues - Fixes https://github.com/flutter/flutter/issues/122602 ## Tests - Added tests for `TokenLogger` - Regenerated tokens, no-op except version removal ## Future work A future PR should replace or remove the following invalid tokens usages <img width="578" alt="image" src="https://github.com/flutter/flutter/assets/6655696/b6f9e5a7-523f-4f72-94f9-1b0bf4cc9f00">
167 lines
5.2 KiB
Dart
167 lines
5.2 KiB
Dart
// 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 ButtonTemplate extends TokenTemplate {
|
|
const ButtonTemplate(this.tokenGroup, super.blockName, super.fileName, super.tokens, {
|
|
super.colorSchemePrefix = '_colors.',
|
|
});
|
|
|
|
final String tokenGroup;
|
|
|
|
String _backgroundColor() {
|
|
if (tokenAvailable('$tokenGroup.container.color')) {
|
|
return '''
|
|
|
|
MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.disabled)) {
|
|
return ${componentColor('$tokenGroup.disabled.container')};
|
|
}
|
|
return ${componentColor('$tokenGroup.container')};
|
|
})''';
|
|
}
|
|
return '''
|
|
|
|
const MaterialStatePropertyAll<Color>(Colors.transparent)''';
|
|
}
|
|
|
|
String _elevation() {
|
|
if (tokenAvailable('$tokenGroup.container.elevation')) {
|
|
return '''
|
|
|
|
MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.disabled)) {
|
|
return ${elevation("$tokenGroup.disabled.container")};
|
|
}
|
|
if (states.contains(MaterialState.pressed)) {
|
|
return ${elevation("$tokenGroup.pressed.container")};
|
|
}
|
|
if (states.contains(MaterialState.hovered)) {
|
|
return ${elevation("$tokenGroup.hover.container")};
|
|
}
|
|
if (states.contains(MaterialState.focused)) {
|
|
return ${elevation("$tokenGroup.focus.container")};
|
|
}
|
|
return ${elevation("$tokenGroup.container")};
|
|
})''';
|
|
}
|
|
return '''
|
|
|
|
const MaterialStatePropertyAll<double>(0.0)''';
|
|
}
|
|
|
|
String _elevationColor(String token) {
|
|
if (tokenAvailable(token)) {
|
|
return 'MaterialStatePropertyAll<Color>(${color(token)})';
|
|
} else {
|
|
return 'const MaterialStatePropertyAll<Color>(Colors.transparent)';
|
|
}
|
|
}
|
|
|
|
@override
|
|
String generate() => '''
|
|
class _${blockName}DefaultsM3 extends ButtonStyle {
|
|
_${blockName}DefaultsM3(this.context)
|
|
: super(
|
|
animationDuration: kThemeChangeDuration,
|
|
enableFeedback: true,
|
|
alignment: Alignment.center,
|
|
);
|
|
|
|
final BuildContext context;
|
|
late final ColorScheme _colors = Theme.of(context).colorScheme;
|
|
|
|
@override
|
|
MaterialStateProperty<TextStyle?> get textStyle =>
|
|
MaterialStatePropertyAll<TextStyle?>(${textStyle("$tokenGroup.label-text")});
|
|
|
|
@override
|
|
MaterialStateProperty<Color?>? get backgroundColor =>${_backgroundColor()};
|
|
|
|
@override
|
|
MaterialStateProperty<Color?>? get foregroundColor =>
|
|
MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.disabled)) {
|
|
return ${componentColor('$tokenGroup.disabled.label-text')};
|
|
}
|
|
return ${componentColor('$tokenGroup.label-text')};
|
|
});
|
|
|
|
@override
|
|
MaterialStateProperty<Color?>? get overlayColor =>
|
|
MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.pressed)) {
|
|
return ${componentColor('$tokenGroup.pressed.state-layer')};
|
|
}
|
|
if (states.contains(MaterialState.hovered)) {
|
|
return ${componentColor('$tokenGroup.hover.state-layer')};
|
|
}
|
|
if (states.contains(MaterialState.focused)) {
|
|
return ${componentColor('$tokenGroup.focus.state-layer')};
|
|
}
|
|
return null;
|
|
});
|
|
|
|
@override
|
|
MaterialStateProperty<Color>? get shadowColor =>
|
|
${_elevationColor("$tokenGroup.container.shadow-color")};
|
|
|
|
@override
|
|
MaterialStateProperty<Color>? get surfaceTintColor =>
|
|
${_elevationColor("$tokenGroup.container.surface-tint-layer.color")};
|
|
|
|
@override
|
|
MaterialStateProperty<double>? get elevation =>${_elevation()};
|
|
|
|
@override
|
|
MaterialStateProperty<EdgeInsetsGeometry>? get padding =>
|
|
MaterialStatePropertyAll<EdgeInsetsGeometry>(_scaledPadding(context));
|
|
|
|
@override
|
|
MaterialStateProperty<Size>? get minimumSize =>
|
|
const MaterialStatePropertyAll<Size>(Size(64.0, ${getToken("$tokenGroup.container.height")}));
|
|
|
|
// No default fixedSize
|
|
|
|
@override
|
|
MaterialStateProperty<Size>? get maximumSize =>
|
|
const MaterialStatePropertyAll<Size>(Size.infinite);
|
|
|
|
${tokenAvailable("$tokenGroup.outline.color") ? '''
|
|
@override
|
|
MaterialStateProperty<BorderSide>? get side =>
|
|
MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.disabled)) {
|
|
return ${border("$tokenGroup.disabled.outline")};
|
|
}
|
|
return ${border("$tokenGroup.outline")};
|
|
});''' : '''
|
|
// No default side'''}
|
|
|
|
@override
|
|
MaterialStateProperty<OutlinedBorder>? get shape =>
|
|
const MaterialStatePropertyAll<OutlinedBorder>(${shape("$tokenGroup.container", '')});
|
|
|
|
@override
|
|
MaterialStateProperty<MouseCursor?>? get mouseCursor =>
|
|
MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
|
if (states.contains(MaterialState.disabled)) {
|
|
return SystemMouseCursors.basic;
|
|
}
|
|
return SystemMouseCursors.click;
|
|
});
|
|
|
|
@override
|
|
VisualDensity? get visualDensity => Theme.of(context).visualDensity;
|
|
|
|
@override
|
|
MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize;
|
|
|
|
@override
|
|
InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory;
|
|
}
|
|
''';
|
|
}
|