Skip to content

Commit 6258127

Browse files
committed
Conditions #19
- Implement color type variable. - Add support for set value of color type in conditions builder. - Fix conditions builder creating duplicated variables when created with create new option. - Add variable property button for fill tile. - Fix recents manager not disposing properly.
1 parent 4bd5a35 commit 6258127

File tree

4 files changed

+145
-22
lines changed

4 files changed

+145
-22
lines changed

lib/src/api/models/action/set_value_action.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ enum ValueType {
9090

9191
/// Represents a boolean value.
9292
bool,
93+
94+
/// Represents a color value.
95+
color,
96+
97+
/// Represents a paint value which can be color, gradient, etc.
98+
paint,
9399
}
94100

95101
/// Represents a value to set in a node.
@@ -134,6 +140,10 @@ abstract class ValueModel<T> with SerializableMixin {
134140
return DoubleValue.fromJson(json);
135141
case ValueType.string:
136142
return StringValue.fromJson(json);
143+
case ValueType.color:
144+
return ColorValue.fromJson(json);
145+
case ValueType.paint:
146+
return PaintValue.fromJson(json);
137147
}
138148
} else {
139149
// backward compatibility
@@ -291,3 +301,73 @@ class StringValue extends ValueModel<String> with SerializableMixin {
291301
@override
292302
Map toJson() => _$StringValueToJson(this);
293303
}
304+
305+
/// Value of boolean type.
306+
@JsonSerializable()
307+
class ColorValue extends ValueModel<ColorRGBA?> with SerializableMixin {
308+
/// Whether this property is nullable.
309+
final bool nullable;
310+
311+
/// Creates a new [ColorValue].
312+
const ColorValue({
313+
required super.name,
314+
super.mode,
315+
super.value,
316+
this.nullable = false,
317+
}) : super(type: ValueType.color);
318+
319+
@override
320+
ColorValue copyWith({
321+
SetValueMode? mode,
322+
ColorRGBA? value,
323+
bool isNull = false,
324+
bool? nullable,
325+
}) =>
326+
ColorValue(
327+
name: name,
328+
mode: mode ?? this.mode,
329+
value: isNull ? null : (value ?? this.value),
330+
nullable: nullable ?? this.nullable,
331+
);
332+
333+
/// Creates a new [BoolValue] instance from a JSON data.
334+
factory ColorValue.fromJson(Map json) => _$ColorValueFromJson(json);
335+
336+
@override
337+
Map toJson() => _$ColorValueToJson(this);
338+
}
339+
340+
/// Value of boolean type.
341+
@JsonSerializable()
342+
class PaintValue extends ValueModel<PaintModel?> with SerializableMixin {
343+
/// Whether this property is nullable.
344+
final bool nullable;
345+
346+
/// Creates a new [PaintValue].
347+
const PaintValue({
348+
required super.name,
349+
super.mode,
350+
super.value,
351+
this.nullable = false,
352+
}) : super(type: ValueType.paint);
353+
354+
@override
355+
PaintValue copyWith({
356+
SetValueMode? mode,
357+
PaintModel? value,
358+
bool isNull = false,
359+
bool? nullable,
360+
}) =>
361+
PaintValue(
362+
name: name,
363+
mode: mode ?? this.mode,
364+
value: isNull ? null : (value ?? this.value),
365+
nullable: nullable ?? this.nullable,
366+
);
367+
368+
/// Creates a new [BoolValue] instance from a JSON data.
369+
factory PaintValue.fromJson(Map json) => _$PaintValueFromJson(json);
370+
371+
@override
372+
Map toJson() => _$PaintValueToJson(this);
373+
}

lib/src/api/models/action/set_value_action.g.dart

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/api/models/variables_model.dart

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ enum VariableType {
2020
boolean,
2121

2222
/// List type. Represents a list of values.
23-
list;
23+
list,
24+
25+
/// Color type. Represents a color value.
26+
color;
2427

2528
/// Returns a string representation of the variable type.
2629
String get label {
@@ -35,6 +38,8 @@ enum VariableType {
3538
return 'Boolean';
3639
case VariableType.list:
3740
return 'List';
41+
case VariableType.color:
42+
return 'Color';
3843
}
3944
}
4045
}
@@ -143,27 +148,6 @@ class VariableData
143148
throw Exception('Unsupported type: $T');
144149
}
145150
}
146-
147-
/// Converts [value] to given [type]. [null] means it can't be converted.
148-
dynamic toTyped() {
149-
switch (type) {
150-
case VariableType.integer:
151-
return int.tryParse(value);
152-
case VariableType.text:
153-
return value;
154-
case VariableType.decimal:
155-
return double.tryParse(value);
156-
case VariableType.boolean:
157-
return value.toLowerCase() == 'true'
158-
? true
159-
: value.toLowerCase() == 'false'
160-
? false
161-
: null;
162-
case VariableType.list:
163-
// TODO: implement type conversion.
164-
throw UnimplementedError('Not Implemented');
165-
}
166-
}
167151
}
168152

169153
/// Contains all the variables associated with a canvas inside a page.

lib/src/api/models/variables_model.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)