|
| 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 MenuTemplate extends TokenTemplate { |
| 8 | + const MenuTemplate(super.blockName, super.fileName, super.tokens, { |
| 9 | + super.colorSchemePrefix = '_colors.', |
| 10 | + }); |
| 11 | + |
| 12 | + @override |
| 13 | + String generate() => ''' |
| 14 | +class _MenuBarDefaultsM3 extends MenuStyle { |
| 15 | + _MenuBarDefaultsM3(this.context) |
| 16 | + : super( |
| 17 | + elevation: const MaterialStatePropertyAll<double?>(${elevation('md.comp.menu.container')}), |
| 18 | + shape: const MaterialStatePropertyAll<OutlinedBorder>(_defaultMenuBorder), |
| 19 | + alignment: AlignmentDirectional.bottomStart, |
| 20 | + ); |
| 21 | + static const RoundedRectangleBorder _defaultMenuBorder = |
| 22 | + ${shape('md.comp.menu.container', '')}; |
| 23 | +
|
| 24 | + final BuildContext context; |
| 25 | +
|
| 26 | + late final ColorScheme _colors = Theme.of(context).colorScheme; |
| 27 | +
|
| 28 | + @override |
| 29 | + MaterialStateProperty<Color?> get backgroundColor { |
| 30 | + return MaterialStatePropertyAll<Color?>(${componentColor('md.comp.menu.container')}); |
| 31 | + } |
| 32 | +
|
| 33 | + @override |
| 34 | + MaterialStateProperty<Color?>? get shadowColor { |
| 35 | + return MaterialStatePropertyAll<Color?>(${color('md.comp.menu.container.shadow-color')}); |
| 36 | + } |
| 37 | +
|
| 38 | + @override |
| 39 | + MaterialStateProperty<Color?>? get surfaceTintColor { |
| 40 | + return MaterialStatePropertyAll<Color?>(${componentColor('md.comp.menu.container.surface-tint-layer')}); |
| 41 | + } |
| 42 | +
|
| 43 | + @override |
| 44 | + MaterialStateProperty<EdgeInsetsGeometry?>? get padding { |
| 45 | + return MaterialStatePropertyAll<EdgeInsetsGeometry>( |
| 46 | + EdgeInsetsDirectional.symmetric( |
| 47 | + horizontal: math.max( |
| 48 | + _kTopLevelMenuHorizontalMinPadding, |
| 49 | + 2 + Theme.of(context).visualDensity.baseSizeAdjustment.dx, |
| 50 | + ), |
| 51 | + ), |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
| 55 | +
|
| 56 | +class _MenuButtonDefaultsM3 extends ButtonStyle { |
| 57 | + _MenuButtonDefaultsM3(this.context) |
| 58 | + : super( |
| 59 | + animationDuration: kThemeChangeDuration, |
| 60 | + enableFeedback: true, |
| 61 | + alignment: AlignmentDirectional.centerStart, |
| 62 | + ); |
| 63 | + final BuildContext context; |
| 64 | +
|
| 65 | + late final ColorScheme _colors = Theme.of(context).colorScheme; |
| 66 | +
|
| 67 | + @override |
| 68 | + MaterialStateProperty<Color?>? get backgroundColor { |
| 69 | + return ButtonStyleButton.allOrNull<Color>(Colors.transparent); |
| 70 | + } |
| 71 | +
|
| 72 | + // No default shadow color |
| 73 | +
|
| 74 | + // No default surface tint color |
| 75 | +
|
| 76 | + @override |
| 77 | + MaterialStateProperty<double>? get elevation { |
| 78 | + return ButtonStyleButton.allOrNull<double>(0.0); |
| 79 | + } |
| 80 | +
|
| 81 | + @override |
| 82 | + MaterialStateProperty<Color?>? get foregroundColor { |
| 83 | + return MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 84 | + if (states.contains(MaterialState.disabled)) { |
| 85 | + return ${componentColor('md.comp.menu.list-item.disabled.label-text')}; |
| 86 | + } |
| 87 | + if (states.contains(MaterialState.pressed)) { |
| 88 | + return ${componentColor('md.comp.menu.list-item.pressed.label-text')}; |
| 89 | + } |
| 90 | + if (states.contains(MaterialState.hovered)) { |
| 91 | + return ${componentColor('md.comp.menu.list-item.hover.label-text')}; |
| 92 | + } |
| 93 | + if (states.contains(MaterialState.focused)) { |
| 94 | + return ${componentColor('md.comp.menu.list-item.focus.label-text')}; |
| 95 | + } |
| 96 | + return ${componentColor('md.comp.menu.list-item.label-text')}; |
| 97 | + }); |
| 98 | + } |
| 99 | +
|
| 100 | + @override |
| 101 | + MaterialStateProperty<Color?>? get iconColor { |
| 102 | + return MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 103 | + if (states.contains(MaterialState.disabled)) { |
| 104 | + return ${componentColor('md.comp.menu.list-item.with-leading-icon.disabled.leading-icon')}; |
| 105 | + } |
| 106 | + if (states.contains(MaterialState.pressed)) { |
| 107 | + return ${componentColor('md.comp.menu.list-item.with-leading-icon.pressed.icon')}; |
| 108 | + } |
| 109 | + if (states.contains(MaterialState.hovered)) { |
| 110 | + return ${componentColor('md.comp.menu.list-item.with-leading-icon.hover.icon')}; |
| 111 | + } |
| 112 | + if (states.contains(MaterialState.focused)) { |
| 113 | + return ${componentColor('md.comp.menu.list-item.with-leading-icon.focus.icon')}; |
| 114 | + } |
| 115 | + return ${componentColor('md.comp.menu.list-item.with-leading-icon.leading-icon')}; |
| 116 | + }); |
| 117 | + } |
| 118 | +
|
| 119 | + // No default fixedSize |
| 120 | +
|
| 121 | + @override |
| 122 | + MaterialStateProperty<Size>? get maximumSize { |
| 123 | + return ButtonStyleButton.allOrNull<Size>(Size.infinite); |
| 124 | + } |
| 125 | +
|
| 126 | + @override |
| 127 | + MaterialStateProperty<Size>? get minimumSize { |
| 128 | + return ButtonStyleButton.allOrNull<Size>(const Size(64.0, ${tokens['md.comp.menu.list-item.container.height']})); |
| 129 | + } |
| 130 | +
|
| 131 | + @override |
| 132 | + MaterialStateProperty<MouseCursor?>? get mouseCursor { |
| 133 | + return MaterialStateProperty.resolveWith( |
| 134 | + (Set<MaterialState> states) { |
| 135 | + if (states.contains(MaterialState.disabled)) { |
| 136 | + return SystemMouseCursors.basic; |
| 137 | + } |
| 138 | + return SystemMouseCursors.click; |
| 139 | + }, |
| 140 | + ); |
| 141 | + } |
| 142 | +
|
| 143 | + @override |
| 144 | + MaterialStateProperty<Color?>? get overlayColor { |
| 145 | + return MaterialStateProperty.resolveWith( |
| 146 | + (Set<MaterialState> states) { |
| 147 | + if (states.contains(MaterialState.pressed)) { |
| 148 | + return ${componentColor('md.comp.menu.list-item.pressed.state-layer')}; |
| 149 | + } |
| 150 | + if (states.contains(MaterialState.hovered)) { |
| 151 | + return ${componentColor('md.comp.menu.list-item.hover.state-layer')}; |
| 152 | + } |
| 153 | + if (states.contains(MaterialState.focused)) { |
| 154 | + return ${componentColor('md.comp.menu.list-item.focus.state-layer')}; |
| 155 | + } |
| 156 | + return Colors.transparent; |
| 157 | + }, |
| 158 | + ); |
| 159 | + } |
| 160 | +
|
| 161 | + @override |
| 162 | + MaterialStateProperty<EdgeInsetsGeometry>? get padding { |
| 163 | + return ButtonStyleButton.allOrNull<EdgeInsetsGeometry>(_scaledPadding(context)); |
| 164 | + } |
| 165 | +
|
| 166 | + // No default side |
| 167 | +
|
| 168 | + @override |
| 169 | + MaterialStateProperty<OutlinedBorder>? get shape { |
| 170 | + return ButtonStyleButton.allOrNull<OutlinedBorder>(const RoundedRectangleBorder()); |
| 171 | + } |
| 172 | +
|
| 173 | + @override |
| 174 | + InteractiveInkFeatureFactory? get splashFactory => Theme.of(context).splashFactory; |
| 175 | +
|
| 176 | + @override |
| 177 | + MaterialTapTargetSize? get tapTargetSize => Theme.of(context).materialTapTargetSize; |
| 178 | +
|
| 179 | + @override |
| 180 | + MaterialStateProperty<TextStyle?> get textStyle { |
| 181 | + return MaterialStatePropertyAll<TextStyle?>(${textStyle('md.comp.menu.list-item.label-text')}); |
| 182 | + } |
| 183 | +
|
| 184 | + @override |
| 185 | + VisualDensity? get visualDensity => Theme.of(context).visualDensity; |
| 186 | +
|
| 187 | + // The horizontal padding number comes from the spec. |
| 188 | + EdgeInsetsGeometry _scaledPadding(BuildContext context) { |
| 189 | + return ButtonStyleButton.scaledPadding( |
| 190 | + const EdgeInsets.symmetric(horizontal: 12), |
| 191 | + const EdgeInsets.symmetric(horizontal: 8), |
| 192 | + const EdgeInsets.symmetric(horizontal: 4), |
| 193 | + MediaQuery.maybeOf(context)?.textScaleFactor ?? 1, |
| 194 | + ); |
| 195 | + } |
| 196 | +} |
| 197 | +
|
| 198 | +class _MenuDefaultsM3 extends MenuStyle { |
| 199 | + _MenuDefaultsM3(this.context) |
| 200 | + : super( |
| 201 | + elevation: const MaterialStatePropertyAll<double?>(${elevation('md.comp.menu.container')}), |
| 202 | + shape: const MaterialStatePropertyAll<OutlinedBorder>(_defaultMenuBorder), |
| 203 | + alignment: AlignmentDirectional.topEnd, |
| 204 | + ); |
| 205 | + static const RoundedRectangleBorder _defaultMenuBorder = |
| 206 | + ${shape('md.comp.menu.container', '')}; |
| 207 | +
|
| 208 | + final BuildContext context; |
| 209 | +
|
| 210 | + late final ColorScheme _colors = Theme.of(context).colorScheme; |
| 211 | +
|
| 212 | + @override |
| 213 | + MaterialStateProperty<Color?> get backgroundColor { |
| 214 | + return MaterialStatePropertyAll<Color?>(${componentColor('md.comp.menu.container')}); |
| 215 | + } |
| 216 | +
|
| 217 | + @override |
| 218 | + MaterialStateProperty<Color?>? get surfaceTintColor { |
| 219 | + return MaterialStatePropertyAll<Color?>(${componentColor('md.comp.menu.container.surface-tint-layer')}); |
| 220 | + } |
| 221 | +
|
| 222 | + @override |
| 223 | + MaterialStateProperty<Color?>? get shadowColor { |
| 224 | + return MaterialStatePropertyAll<Color?>(${color('md.comp.menu.container.shadow-color')}); |
| 225 | + } |
| 226 | +
|
| 227 | + @override |
| 228 | + MaterialStateProperty<EdgeInsetsGeometry?>? get padding { |
| 229 | + return MaterialStatePropertyAll<EdgeInsetsGeometry>( |
| 230 | + EdgeInsetsDirectional.symmetric( |
| 231 | + vertical: math.max( |
| 232 | + _kMenuVerticalMinPadding, |
| 233 | + 2 + Theme.of(context).visualDensity.baseSizeAdjustment.dy, |
| 234 | + ), |
| 235 | + ), |
| 236 | + ); |
| 237 | + } |
| 238 | +} |
| 239 | +'''; |
| 240 | +} |
0 commit comments