|
| 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 SegmentedButtonTemplate extends TokenTemplate { |
| 8 | + const SegmentedButtonTemplate(super.blockName, super.fileName, super.tokens, { |
| 9 | + super.colorSchemePrefix = '_colors.', |
| 10 | + }); |
| 11 | + |
| 12 | + String _layerOpacity(String layerToken) { |
| 13 | + if (tokens.containsKey(layerToken)) { |
| 14 | + final String? layerValue = tokens[layerToken] as String?; |
| 15 | + if (tokens.containsKey(layerValue)) { |
| 16 | + final String? opacityValue = opacity(layerValue!); |
| 17 | + if (opacityValue != null) { |
| 18 | + return '.withOpacity($opacityValue)'; |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + return ''; |
| 23 | + } |
| 24 | + |
| 25 | + String _stateColor(String componentToken, String type, String state) { |
| 26 | + final String baseColor = color('$componentToken.$type.$state.state-layer.color', ''); |
| 27 | + if (baseColor.isEmpty) { |
| 28 | + return 'null'; |
| 29 | + } |
| 30 | + final String opacity = _layerOpacity('$componentToken.$state.state-layer.opacity'); |
| 31 | + return '$baseColor$opacity'; |
| 32 | + } |
| 33 | + |
| 34 | + @override |
| 35 | + String generate() => ''' |
| 36 | +class _SegmentedButtonDefaultsM3 extends SegmentedButtonThemeData { |
| 37 | + _SegmentedButtonDefaultsM3(this.context); |
| 38 | +
|
| 39 | + final BuildContext context; |
| 40 | + late final ThemeData _theme = Theme.of(context); |
| 41 | + late final ColorScheme _colors = _theme.colorScheme; |
| 42 | +
|
| 43 | + @override ButtonStyle? get style { |
| 44 | + return ButtonStyle( |
| 45 | + textStyle: MaterialStatePropertyAll<TextStyle?>(${textStyle('md.comp.outlined-segmented-button.label-text')}), |
| 46 | + backgroundColor: MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 47 | + if (states.contains(MaterialState.disabled)) { |
| 48 | + return ${componentColor('md.comp.outlined-segmented-button.disabled')}; |
| 49 | + } |
| 50 | + if (states.contains(MaterialState.selected)) { |
| 51 | + return ${componentColor('md.comp.outlined-segmented-button.selected.container')}; |
| 52 | + } |
| 53 | + return ${componentColor('md.comp.outlined-segmented-button.unselected.container')}; |
| 54 | + }), |
| 55 | + foregroundColor: MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 56 | + if (states.contains(MaterialState.disabled)) { |
| 57 | + return ${componentColor('md.comp.outlined-segmented-button.disabled.label-text')}; |
| 58 | + } |
| 59 | + if (states.contains(MaterialState.selected)) { |
| 60 | + if (states.contains(MaterialState.pressed)) { |
| 61 | + return ${componentColor('md.comp.outlined-segmented-button.selected.pressed.label-text')}; |
| 62 | + } |
| 63 | + if (states.contains(MaterialState.hovered)) { |
| 64 | + return ${componentColor('md.comp.outlined-segmented-button.selected.hover.label-text')}; |
| 65 | + } |
| 66 | + if (states.contains(MaterialState.focused)) { |
| 67 | + return ${componentColor('md.comp.outlined-segmented-button.selected.focus.label-text')}; |
| 68 | + } |
| 69 | + return ${componentColor('md.comp.outlined-segmented-button.selected.label-text')}; |
| 70 | + } else { |
| 71 | + if (states.contains(MaterialState.pressed)) { |
| 72 | + return ${componentColor('md.comp.outlined-segmented-button.unselected.pressed.label-text')}; |
| 73 | + } |
| 74 | + if (states.contains(MaterialState.hovered)) { |
| 75 | + return ${componentColor('md.comp.outlined-segmented-button.unselected.hover.label-text')}; |
| 76 | + } |
| 77 | + if (states.contains(MaterialState.focused)) { |
| 78 | + return ${componentColor('md.comp.outlined-segmented-button.unselected.focus.label-text')}; |
| 79 | + } |
| 80 | + return ${componentColor('md.comp.outlined-segmented-button.unselected.container')}; |
| 81 | + } |
| 82 | + }), |
| 83 | + overlayColor: MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 84 | + if (states.contains(MaterialState.selected)) { |
| 85 | + if (states.contains(MaterialState.hovered)) { |
| 86 | + return ${_stateColor('md.comp.outlined-segmented-button', 'selected', 'hover')}; |
| 87 | + } |
| 88 | + if (states.contains(MaterialState.focused)) { |
| 89 | + return ${_stateColor('md.comp.outlined-segmented-button', 'selected', 'focus')}; |
| 90 | + } |
| 91 | + if (states.contains(MaterialState.pressed)) { |
| 92 | + return ${_stateColor('md.comp.outlined-segmented-button', 'selected', 'pressed')}; |
| 93 | + } |
| 94 | + } else { |
| 95 | + if (states.contains(MaterialState.hovered)) { |
| 96 | + return ${_stateColor('md.comp.outlined-segmented-button', 'unselected', 'hover')}; |
| 97 | + } |
| 98 | + if (states.contains(MaterialState.focused)) { |
| 99 | + return ${_stateColor('md.comp.outlined-segmented-button', 'unselected', 'focus')}; |
| 100 | + } |
| 101 | + if (states.contains(MaterialState.pressed)) { |
| 102 | + return ${_stateColor('md.comp.outlined-segmented-button', 'unselected', 'pressed')}; |
| 103 | + } |
| 104 | + } |
| 105 | + return null; |
| 106 | + }), |
| 107 | + surfaceTintColor: const MaterialStatePropertyAll<Color>(Colors.transparent), |
| 108 | + elevation: const MaterialStatePropertyAll<double>(0), |
| 109 | + iconSize: const MaterialStatePropertyAll<double?>(${tokens['md.comp.outlined-segmented-button.with-icon.icon.size']}), |
| 110 | + side: MaterialStateProperty.resolveWith((Set<MaterialState> states) { |
| 111 | + if (states.contains(MaterialState.disabled)) { |
| 112 | + return ${border("md.comp.outlined-segmented-button.disabled.outline")}; |
| 113 | + } |
| 114 | + return ${border("md.comp.outlined-segmented-button.outline")}; |
| 115 | + }), |
| 116 | + shape: const MaterialStatePropertyAll<OutlinedBorder>(${shape("md.comp.outlined-segmented-button", '')}), |
| 117 | + ); |
| 118 | + } |
| 119 | +
|
| 120 | + @override |
| 121 | + Widget? get selectedIcon => const Icon(Icons.check); |
| 122 | +} |
| 123 | +'''; |
| 124 | +} |
0 commit comments