|
| 1 | +import 'dart:convert'; |
| 2 | + |
1 | 3 | import 'package:equatable/equatable.dart'; |
2 | 4 | import 'package:json_annotation/json_annotation.dart'; |
3 | 5 |
|
@@ -76,7 +78,7 @@ class VariableData |
76 | 78 | VariableData copyWith({ |
77 | 79 | String? id, |
78 | 80 | String? name, |
79 | | - String? value, |
| 81 | + Object? value, |
80 | 82 | VariableType? type, |
81 | 83 | bool? isUsed, |
82 | 84 | Set<String>? nodes, |
@@ -128,24 +130,43 @@ class VariableData |
128 | 130 | } |
129 | 131 |
|
130 | 132 | /// Returns the value converted to the appropriate type according to [type]. |
131 | | -String? sanitizeValueForVariableType(String? value, VariableType type) { |
| 133 | +/// Supported color values: |
| 134 | +/// - ColorRGBA |
| 135 | +/// - ColorRGB |
| 136 | +/// - Hex color string |
| 137 | +/// - Flutter Color object |
| 138 | +String? sanitizeValueForVariableType(Object? value, VariableType type) { |
132 | 139 | if (value == null) return null; |
133 | | - if (value.isEmpty) return ''; |
134 | | - return switch (type) { |
135 | | - VariableType.text => value, |
136 | | - VariableType.integer => num.tryParse(value).toInt()?.toString(), |
137 | | - VariableType.decimal => num.tryParse(value).toDouble()?.toString(), |
138 | | - VariableType.boolean => |
139 | | - bool.tryParse(value, caseSensitive: false)?.toString(), |
140 | | - VariableType.color => |
141 | | - RegExp(r'^#[0-9a-fA-F]{2,8}$', caseSensitive: false).hasMatch(value) |
142 | | - ? value.toUpperCase() |
143 | | - : null, |
144 | | - // TODO: this could be a bit expensive. Maybe enable only when required! |
145 | | - // VariableType.map => tryJsonDecode(value), |
146 | | - // VariableType.list => value.toList(), |
147 | | - _ => value, |
148 | | - }; |
| 140 | + // if (value.isEmpty) return ''; |
| 141 | + switch (type) { |
| 142 | + case VariableType.text: |
| 143 | + return value.toString(); |
| 144 | + case VariableType.integer: |
| 145 | + return num.tryParse(value.toString()).toInt()?.toString(); |
| 146 | + case VariableType.decimal: |
| 147 | + return num.tryParse(value.toString()).toDouble()?.toString(); |
| 148 | + case VariableType.boolean: |
| 149 | + return bool.tryParse(value.toString(), caseSensitive: false)?.toString(); |
| 150 | + case VariableType.color: |
| 151 | + if (value is ColorRGBA) return value.toHex(); |
| 152 | + if (value is ColorRGB) return value.toColorRGBA()!.toHex(); |
| 153 | + final colorMatch = flutterColorRegex.firstMatch(value.toString()); |
| 154 | + if (colorMatch != null) { |
| 155 | + return ColorRGBA.fromHex(colorMatch.namedGroup('hex')!)?.toHex(); |
| 156 | + } |
| 157 | + final hexMatch = hexColorRegex.firstMatch(value.toString()); |
| 158 | + if (hexMatch != null) return value.toString().toUpperCase(); |
| 159 | + return null; |
| 160 | + // This could be a bit expensive. Maybe enable only when required! |
| 161 | + case VariableType.map: |
| 162 | + if (value is Map) return jsonEncode(value); |
| 163 | + final map = value.toMap(); |
| 164 | + return map != null ? jsonEncode(map) : null; |
| 165 | + case VariableType.list: |
| 166 | + if (value is List) return jsonEncode(value); |
| 167 | + final list = value.toList(); |
| 168 | + return list != null ? jsonEncode(list) : null; |
| 169 | + } |
149 | 170 | } |
150 | 171 |
|
151 | 172 | /// Contains all the variables associated with a canvas inside a page. |
@@ -242,20 +263,24 @@ class CanvasVariableData extends VariableData { |
242 | 263 | @override |
243 | 264 | CanvasVariableData copyWith({ |
244 | 265 | String? name, |
245 | | - String? value, |
| 266 | + Object? value, |
246 | 267 | VariableType? type, |
247 | 268 | String? id, |
248 | 269 | bool? isUsed, |
249 | 270 | String? canvasId, |
250 | 271 | Set<String>? nodes, |
251 | | - }) => |
252 | | - CanvasVariableData( |
253 | | - name: name ?? this.name, |
254 | | - value: value ?? this.value, |
255 | | - type: type ?? this.type, |
256 | | - id: id ?? this.id, |
257 | | - canvasId: canvasId ?? this.canvasId, |
258 | | - ); |
| 272 | + }) { |
| 273 | + final String? sanitizedValue = value == null |
| 274 | + ? null |
| 275 | + : sanitizeValueForVariableType(value, type ?? this.type); |
| 276 | + return CanvasVariableData( |
| 277 | + name: name ?? this.name, |
| 278 | + value: sanitizedValue ?? this.value, |
| 279 | + type: type ?? this.type, |
| 280 | + id: id ?? this.id, |
| 281 | + canvasId: canvasId ?? this.canvasId, |
| 282 | + ); |
| 283 | + } |
259 | 284 |
|
260 | 285 | @override |
261 | 286 | List<Object?> get props => [...super.props, canvasId]; |
|
0 commit comments