|
| 1 | +import 'css_length.dart'; |
| 2 | +import 'css_value.dart'; |
| 3 | + |
| 4 | +/// CSS flex shorthand property values. |
| 5 | +sealed class CssFlexShorthand implements CssValue { |
| 6 | + const CssFlexShorthand._(); |
| 7 | + |
| 8 | + static const CssFlexShorthand auto = _CssFlexShorthandKeyword('auto'); |
| 9 | + static const CssFlexShorthand initial = _CssFlexShorthandKeyword('initial'); |
| 10 | + static const CssFlexShorthand none = _CssFlexShorthandKeyword('none'); |
| 11 | + |
| 12 | + /// Creates a flex shorthand value. |
| 13 | + /// |
| 14 | + /// Supports the following combinations: |
| 15 | + /// - [grow]: flex-grow (number) |
| 16 | + /// - [basis]: flex-basis (length) |
| 17 | + /// - [grow] and [basis]: flex-grow flex-basis |
| 18 | + /// - [grow] and [shrink]: flex-grow flex-shrink |
| 19 | + /// - [grow], [shrink], and [basis]: flex-grow flex-shrink flex-basis |
| 20 | + /// |
| 21 | + /// If [shrink] is provided, [grow] must also be provided. |
| 22 | + factory CssFlexShorthand({num? grow, num? shrink, CssLength? basis}) { |
| 23 | + if (grow != null) { |
| 24 | + if (shrink != null) { |
| 25 | + if (basis != null) { |
| 26 | + return _CssFlexShorthandValues(grow, shrink, basis); |
| 27 | + } |
| 28 | + return _CssFlexShorthandValues(grow, shrink, null); |
| 29 | + } |
| 30 | + if (basis != null) { |
| 31 | + return _CssFlexShorthandValues(grow, null, basis); |
| 32 | + } |
| 33 | + return _CssFlexShorthandValues(grow, null, null); |
| 34 | + } else if (basis != null) { |
| 35 | + if (shrink != null) { |
| 36 | + throw ArgumentError('flex-shrink requires flex-grow to be specified.'); |
| 37 | + } |
| 38 | + return _CssFlexShorthandValues(null, null, basis); |
| 39 | + } else if (shrink != null) { |
| 40 | + throw ArgumentError('flex-shrink requires flex-grow to be specified.'); |
| 41 | + } |
| 42 | + |
| 43 | + throw ArgumentError('At least one of grow or basis must be provided.'); |
| 44 | + } |
| 45 | + |
| 46 | + /// CSS variable reference. |
| 47 | + factory CssFlexShorthand.variable(String varName) = _CssFlexShorthandVariable; |
| 48 | + |
| 49 | + /// Raw CSS value escape hatch. |
| 50 | + factory CssFlexShorthand.raw(String value) = _CssFlexShorthandRaw; |
| 51 | + |
| 52 | + /// Global keyword (inherit, initial, unset, revert). |
| 53 | + factory CssFlexShorthand.global(CssGlobal global) = _CssFlexShorthandGlobal; |
| 54 | +} |
| 55 | + |
| 56 | +final class _CssFlexShorthandKeyword extends CssFlexShorthand { |
| 57 | + final String keyword; |
| 58 | + const _CssFlexShorthandKeyword(this.keyword) : super._(); |
| 59 | + @override |
| 60 | + String toCss() => keyword; |
| 61 | +} |
| 62 | + |
| 63 | +final class _CssFlexShorthandValues extends CssFlexShorthand { |
| 64 | + final num? grow; |
| 65 | + final num? shrink; |
| 66 | + final CssLength? basis; |
| 67 | + |
| 68 | + const _CssFlexShorthandValues(this.grow, this.shrink, this.basis) : super._(); |
| 69 | + |
| 70 | + @override |
| 71 | + String toCss() { |
| 72 | + final parts = <String>[]; |
| 73 | + if (grow != null) parts.add(grow.toString()); |
| 74 | + if (shrink != null) parts.add(shrink.toString()); |
| 75 | + if (basis != null) parts.add(basis!.toCss()); |
| 76 | + return parts.join(' '); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +final class _CssFlexShorthandVariable extends CssFlexShorthand { |
| 81 | + final String varName; |
| 82 | + const _CssFlexShorthandVariable(this.varName) : super._(); |
| 83 | + @override |
| 84 | + String toCss() => 'var(--$varName)'; |
| 85 | +} |
| 86 | + |
| 87 | +final class _CssFlexShorthandRaw extends CssFlexShorthand { |
| 88 | + final String value; |
| 89 | + const _CssFlexShorthandRaw(this.value) : super._(); |
| 90 | + @override |
| 91 | + String toCss() => value; |
| 92 | +} |
| 93 | + |
| 94 | +final class _CssFlexShorthandGlobal extends CssFlexShorthand { |
| 95 | + final CssGlobal global; |
| 96 | + const _CssFlexShorthandGlobal(this.global) : super._(); |
| 97 | + @override |
| 98 | + String toCss() => global.toCss(); |
| 99 | +} |
0 commit comments