|
| 1 | +import 'css_value.dart'; |
| 2 | +import 'css_color.dart'; |
| 3 | +import 'css_length.dart'; |
| 4 | +import 'css_gradient_direction.dart'; |
| 5 | +import 'css_radial_shape.dart'; |
| 6 | +import 'css_radial_size.dart'; |
| 7 | +import 'css_background_position.dart'; |
| 8 | + |
| 9 | +/// Represents a color stop in a gradient. |
| 10 | +class CssGradientStop { |
| 11 | + /// The color at this stop. |
| 12 | + final CssColor color; |
| 13 | + |
| 14 | + /// The position of the stop along the gradient line. |
| 15 | + final CssLength? offset; |
| 16 | + |
| 17 | + /// Creates a new gradient color stop. |
| 18 | + const CssGradientStop(this.color, [this.offset]); |
| 19 | + |
| 20 | + /// Converts the stop to its CSS string representation. |
| 21 | + String toCss() { |
| 22 | + if (offset != null) { |
| 23 | + return '${color.toCss()} ${offset!.toCss()}'; |
| 24 | + } |
| 25 | + return color.toCss(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// CSS background-image property values. |
| 30 | +sealed class CssBackgroundImage implements CssValue { |
| 31 | + const CssBackgroundImage._(); |
| 32 | + |
| 33 | + /// `none` keyword. |
| 34 | + static const CssBackgroundImage none = _CssBackgroundImageKeyword('none'); |
| 35 | + |
| 36 | + /// URL image. |
| 37 | + factory CssBackgroundImage.url(String url) = _CssBackgroundImageUrl; |
| 38 | + |
| 39 | + /// Linear gradient. |
| 40 | + /// |
| 41 | + /// [direction] can be an angle (e.g. '45deg') or side/corner (e.g. 'to bottom right'). |
| 42 | + factory CssBackgroundImage.linearGradient({ |
| 43 | + CssGradientDirection? direction, |
| 44 | + required List<CssGradientStop> stops, |
| 45 | + bool repeating, |
| 46 | + }) = _CssBackgroundImageLinearGradient; |
| 47 | + |
| 48 | + /// Radial gradient. |
| 49 | + /// |
| 50 | + /// [shape] can be 'circle' or 'ellipse'. |
| 51 | + /// [size] can be 'closest-side', 'farthest-corner', etc. or a length. |
| 52 | + /// [position] is the center position (e.g. 'center', '50% 50%'). |
| 53 | + factory CssBackgroundImage.radialGradient({ |
| 54 | + CssRadialShape? shape, |
| 55 | + CssRadialSize? size, |
| 56 | + CssBackgroundPosition? position, |
| 57 | + required List<CssGradientStop> stops, |
| 58 | + bool repeating, |
| 59 | + }) = _CssBackgroundImageRadialGradient; |
| 60 | + |
| 61 | + /// Multiple background images. |
| 62 | + factory CssBackgroundImage.list(List<CssBackgroundImage> images) = |
| 63 | + _CssBackgroundImageList; |
| 64 | + |
| 65 | + /// Raw CSS value escape hatch. |
| 66 | + factory CssBackgroundImage.raw(String value) = _CssBackgroundImageRaw; |
| 67 | + |
| 68 | + /// Global keyword (inherit, initial, unset, revert). |
| 69 | + factory CssBackgroundImage.global(CssGlobal global) = |
| 70 | + _CssBackgroundImageGlobal; |
| 71 | +} |
| 72 | + |
| 73 | +final class _CssBackgroundImageKeyword extends CssBackgroundImage { |
| 74 | + final String keyword; |
| 75 | + const _CssBackgroundImageKeyword(this.keyword) : super._(); |
| 76 | + |
| 77 | + @override |
| 78 | + String toCss() => keyword; |
| 79 | +} |
| 80 | + |
| 81 | +final class _CssBackgroundImageUrl extends CssBackgroundImage { |
| 82 | + final String url; |
| 83 | + const _CssBackgroundImageUrl(this.url) : super._(); |
| 84 | + |
| 85 | + @override |
| 86 | + String toCss() => 'url($url)'; |
| 87 | +} |
| 88 | + |
| 89 | +final class _CssBackgroundImageLinearGradient extends CssBackgroundImage { |
| 90 | + final CssGradientDirection? direction; |
| 91 | + final List<CssGradientStop> stops; |
| 92 | + final bool repeating; |
| 93 | + |
| 94 | + const _CssBackgroundImageLinearGradient({ |
| 95 | + this.direction, |
| 96 | + required this.stops, |
| 97 | + this.repeating = false, |
| 98 | + }) : super._(); |
| 99 | + |
| 100 | + @override |
| 101 | + String toCss() { |
| 102 | + final buffer = StringBuffer( |
| 103 | + repeating ? 'repeating-linear-gradient(' : 'linear-gradient(', |
| 104 | + ); |
| 105 | + if (direction != null) { |
| 106 | + buffer.write('${direction!.toCss()}, '); |
| 107 | + } |
| 108 | + buffer.write(stops.map((s) => s.toCss()).join(', ')); |
| 109 | + buffer.write(')'); |
| 110 | + return buffer.toString(); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +final class _CssBackgroundImageRadialGradient extends CssBackgroundImage { |
| 115 | + final CssRadialShape? shape; |
| 116 | + final CssRadialSize? size; |
| 117 | + final CssBackgroundPosition? position; |
| 118 | + final List<CssGradientStop> stops; |
| 119 | + final bool repeating; |
| 120 | + |
| 121 | + const _CssBackgroundImageRadialGradient({ |
| 122 | + this.shape, |
| 123 | + this.size, |
| 124 | + this.position, |
| 125 | + required this.stops, |
| 126 | + this.repeating = false, |
| 127 | + }) : super._(); |
| 128 | + |
| 129 | + @override |
| 130 | + String toCss() { |
| 131 | + final buffer = StringBuffer( |
| 132 | + repeating ? 'repeating-radial-gradient(' : 'radial-gradient(', |
| 133 | + ); |
| 134 | + |
| 135 | + final hasShapeOrSize = shape != null || size != null; |
| 136 | + final hasPosition = position != null; |
| 137 | + |
| 138 | + if (hasShapeOrSize || hasPosition) { |
| 139 | + if (shape != null) { |
| 140 | + buffer.write(shape!.toCss()); |
| 141 | + if (size != null) buffer.write(' '); |
| 142 | + } |
| 143 | + if (size != null) buffer.write(size!.toCss()); |
| 144 | + |
| 145 | + if (hasPosition) { |
| 146 | + if (hasShapeOrSize) buffer.write(' '); |
| 147 | + buffer.write('at ${position!.toCss()}'); |
| 148 | + } |
| 149 | + buffer.write(', '); |
| 150 | + } |
| 151 | + |
| 152 | + buffer.write(stops.map((s) => s.toCss()).join(', ')); |
| 153 | + buffer.write(')'); |
| 154 | + return buffer.toString(); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +final class _CssBackgroundImageList extends CssBackgroundImage { |
| 159 | + final List<CssBackgroundImage> images; |
| 160 | + const _CssBackgroundImageList(this.images) : super._(); |
| 161 | + |
| 162 | + @override |
| 163 | + String toCss() => images.map((i) => i.toCss()).join(', '); |
| 164 | +} |
| 165 | + |
| 166 | +final class _CssBackgroundImageRaw extends CssBackgroundImage { |
| 167 | + final String value; |
| 168 | + const _CssBackgroundImageRaw(this.value) : super._(); |
| 169 | + |
| 170 | + @override |
| 171 | + String toCss() => value; |
| 172 | +} |
| 173 | + |
| 174 | +final class _CssBackgroundImageGlobal extends CssBackgroundImage { |
| 175 | + final CssGlobal global; |
| 176 | + const _CssBackgroundImageGlobal(this.global) : super._(); |
| 177 | + |
| 178 | + @override |
| 179 | + String toCss() => global.toCss(); |
| 180 | +} |
0 commit comments