|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 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 'template.dart'; |
| 6 | + |
| 7 | +class ButtonTemplate extends TokenTemplate { |
| 8 | + const ButtonTemplate(this.tokenGroup, String fileName, Map<String, dynamic> tokens) |
| 9 | + : super(fileName, tokens, |
| 10 | + colorSchemePrefix: '_colors.', |
| 11 | + ); |
| 12 | + |
| 13 | + final String tokenGroup; |
| 14 | + |
| 15 | + String _backgroundColor() { |
| 16 | + if (tokens.containsKey('$tokenGroup.container.color')) { |
| 17 | + return ''' |
| 18 | +
|
| 19 | + MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 20 | + if (states.contains(MaterialState.disabled)) |
| 21 | + return ${componentColor('$tokenGroup.disabled.container')}; |
| 22 | + return ${componentColor('$tokenGroup.container')}; |
| 23 | + })'''; |
| 24 | + } |
| 25 | + return ''' |
| 26 | +
|
| 27 | + ButtonStyleButton.allOrNull<Color>(Colors.transparent)'''; |
| 28 | + } |
| 29 | + |
| 30 | + String _elevation() { |
| 31 | + if (tokens.containsKey('$tokenGroup.container.elevation')) { |
| 32 | + return ''' |
| 33 | +
|
| 34 | + MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 35 | + if (states.contains(MaterialState.disabled)) |
| 36 | + return ${elevation("$tokenGroup.disabled.container")}; |
| 37 | + if (states.contains(MaterialState.hovered)) |
| 38 | + return ${elevation("$tokenGroup.hover.container")}; |
| 39 | + if (states.contains(MaterialState.focused)) |
| 40 | + return ${elevation("$tokenGroup.focus.container")}; |
| 41 | + if (states.contains(MaterialState.pressed)) |
| 42 | + return ${elevation("$tokenGroup.pressed.container")}; |
| 43 | + return ${elevation("$tokenGroup.container")}; |
| 44 | + })'''; |
| 45 | + } |
| 46 | + return ''' |
| 47 | +
|
| 48 | + ButtonStyleButton.allOrNull<double>(0.0)'''; |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + String generate() => ''' |
| 53 | +// Generated version ${tokens["version"]} |
| 54 | +class _TokenDefaultsM3 extends ButtonStyle { |
| 55 | + _TokenDefaultsM3(this.context) |
| 56 | + : super( |
| 57 | + animationDuration: kThemeChangeDuration, |
| 58 | + enableFeedback: true, |
| 59 | + alignment: Alignment.center, |
| 60 | + ); |
| 61 | +
|
| 62 | + final BuildContext context; |
| 63 | + late final ColorScheme _colors = Theme.of(context).colorScheme; |
| 64 | +
|
| 65 | + @override |
| 66 | + MaterialStateProperty<TextStyle?> get textStyle => |
| 67 | + MaterialStateProperty.all<TextStyle?>(${textStyle("$tokenGroup.label-text")}); |
| 68 | +
|
| 69 | + @override |
| 70 | + MaterialStateProperty<Color?>? get backgroundColor =>${_backgroundColor()}; |
| 71 | +
|
| 72 | + @override |
| 73 | + MaterialStateProperty<Color?>? get foregroundColor => |
| 74 | + MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 75 | + if (states.contains(MaterialState.disabled)) |
| 76 | + return ${componentColor('$tokenGroup.disabled.label-text')}; |
| 77 | + return ${componentColor('$tokenGroup.label-text')}; |
| 78 | + }); |
| 79 | +
|
| 80 | + @override |
| 81 | + MaterialStateProperty<Color?>? get overlayColor => |
| 82 | + MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 83 | + if (states.contains(MaterialState.hovered)) |
| 84 | + return ${componentColor('$tokenGroup.hover.state-layer')}; |
| 85 | + if (states.contains(MaterialState.focused)) |
| 86 | + return ${componentColor('$tokenGroup.focus.state-layer')}; |
| 87 | + if (states.contains(MaterialState.pressed)) |
| 88 | + return ${componentColor('$tokenGroup.pressed.state-layer')}; |
| 89 | + return null; |
| 90 | + }); |
| 91 | +
|
| 92 | +${tokens.containsKey("$tokenGroup.container.shadow-color") ? ''' |
| 93 | + @override |
| 94 | + MaterialStateProperty<Color>? get shadowColor => |
| 95 | + ButtonStyleButton.allOrNull<Color>(${color("$tokenGroup.container.shadow-color")});''' : ''' |
| 96 | + // No default shadow color'''} |
| 97 | +
|
| 98 | +${tokens.containsKey("$tokenGroup.container.surface-tint-layer.color") ? ''' |
| 99 | + @override |
| 100 | + MaterialStateProperty<Color>? get surfaceTintColor => |
| 101 | + ButtonStyleButton.allOrNull<Color>(${color("$tokenGroup.container.surface-tint-layer.color")});''' : ''' |
| 102 | + // No default surface tint color'''} |
| 103 | +
|
| 104 | + @override |
| 105 | + MaterialStateProperty<double>? get elevation =>${_elevation()}; |
| 106 | +
|
| 107 | + @override |
| 108 | + MaterialStateProperty<EdgeInsetsGeometry>? get padding => |
| 109 | + ButtonStyleButton.allOrNull<EdgeInsetsGeometry>(_scaledPadding(context)); |
| 110 | +
|
| 111 | + @override |
| 112 | + MaterialStateProperty<Size>? get minimumSize => |
| 113 | + ButtonStyleButton.allOrNull<Size>(const Size(64.0, ${tokens["$tokenGroup.container.height"]})); |
| 114 | +
|
| 115 | + // No default fixedSize |
| 116 | +
|
| 117 | + @override |
| 118 | + MaterialStateProperty<Size>? get maximumSize => |
| 119 | + ButtonStyleButton.allOrNull<Size>(Size.infinite); |
| 120 | +
|
| 121 | +${tokens.containsKey("$tokenGroup.outline.color") ? ''' |
| 122 | + @override |
| 123 | + MaterialStateProperty<BorderSide>? get side => |
| 124 | + MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 125 | + if (states.contains(MaterialState.disabled)) |
| 126 | + return ${border("$tokenGroup.disabled.outline")}; |
| 127 | + return ${border("$tokenGroup.outline")}; |
| 128 | + });''' : ''' |
| 129 | + // No default side'''} |
| 130 | +
|
| 131 | + @override |
| 132 | + MaterialStateProperty<OutlinedBorder>? get shape => |
| 133 | + ButtonStyleButton.allOrNull<OutlinedBorder>(${shape("$tokenGroup.container")}); |
| 134 | +
|
| 135 | + @override |
| 136 | + MaterialStateProperty<MouseCursor?>? get mouseCursor => |
| 137 | + MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 138 | + if (states.contains(MaterialState.disabled)) |
| 139 | + return SystemMouseCursors.basic; |
| 140 | + return SystemMouseCursors.click; |
| 141 | + }); |
| 142 | +
|
| 143 | + @override |
| 144 | + VisualDensity? get visualDensity => Theme.of(context).visualDensity; |
| 145 | +
|
| 146 | + @override |
| 147 | + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; |
| 148 | +
|
| 149 | + @override |
| 150 | + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; |
| 151 | +} |
| 152 | +'''; |
| 153 | +} |
0 commit comments