@@ -6,7 +6,7 @@ import 'package:equatable/equatable.dart';
66import 'package:json_annotation/json_annotation.dart' ;
77
88import '../../mixins.dart' ;
9- import 'action .dart' ;
9+ import '../models .dart' ;
1010
1111part 'set_value_action.g.dart' ;
1212
@@ -77,6 +77,21 @@ enum SetValueMode {
7777 }
7878}
7979
80+ /// Describes the type of the value.
81+ enum ValueType {
82+ /// Represents a text value.
83+ string,
84+
85+ /// Represents a number without a decimal point.
86+ int ,
87+
88+ /// Represents a number with a decimal point.
89+ double ,
90+
91+ /// Represents a boolean value.
92+ bool ,
93+ }
94+
8095/// Represents a value to set in a node.
8196abstract class ValueModel <T > with SerializableMixin {
8297 /// The name of the property to set the value of.
@@ -88,11 +103,16 @@ abstract class ValueModel<T> with SerializableMixin {
88103 /// The value to set.
89104 final T value;
90105
106+ /// The type of the value.
107+ @JsonKey (includeToJson: true )
108+ final ValueType type;
109+
91110 /// Creates a new [ValueModel] .
92111 const ValueModel ({
93112 required this .name,
94113 this .mode = SetValueMode .discrete,
95114 required this .value,
115+ required this .type,
96116 });
97117
98118 /// Duplicate this [ValueModel] with given data overrides.
@@ -103,12 +123,27 @@ abstract class ValueModel<T> with SerializableMixin {
103123
104124 /// Creates a new [ValueModel] instance from a JSON data.
105125 static ValueModel fromJson (Map json) {
106- final value = json['value' ];
107- if (value is bool ? ) return BoolValue .fromJson (json);
108- if (value is int ) return IntValue .fromJson (json);
109- if (value is double ) return DoubleValue .fromJson (json);
110- if (value is String ) return StringValue .fromJson (json);
111- return IntValue .fromJson (json);
126+ if (json['type' ] != null ) {
127+ final type = ValueType .values.byName (json['type' ]);
128+ switch (type) {
129+ case ValueType .bool :
130+ return BoolValue .fromJson (json);
131+ case ValueType .int :
132+ return IntValue .fromJson (json);
133+ case ValueType .double :
134+ return DoubleValue .fromJson (json);
135+ case ValueType .string:
136+ return StringValue .fromJson (json);
137+ }
138+ } else {
139+ // backward compatibility
140+ final value = json['value' ];
141+ if (value is bool ? ) return BoolValue .fromJson (json);
142+ if (value is int ) return IntValue .fromJson (json);
143+ if (value is double ) return DoubleValue .fromJson (json);
144+ if (value is String ) return StringValue .fromJson (json);
145+ return IntValue .fromJson (json);
146+ }
112147 }
113148}
114149
@@ -128,7 +163,7 @@ class BoolValue extends ValueModel<bool?> with SerializableMixin {
128163 super .mode,
129164 super .value = false ,
130165 this .nullable = false ,
131- });
166+ }) : super (type : ValueType . bool ) ;
132167
133168 @override
134169 BoolValue copyWith ({
@@ -159,7 +194,7 @@ class IntValue extends ValueModel<int> with SerializableMixin {
159194 required super .name,
160195 super .mode,
161196 super .value = 0 ,
162- }) {
197+ }) : super (type : ValueType . int ) {
163198 assert (mode != SetValueMode .toggle, '${mode .prettify } mode not supported.' );
164199 }
165200
@@ -169,7 +204,7 @@ class IntValue extends ValueModel<int> with SerializableMixin {
169204 required super .name,
170205 super .value = 0 ,
171206 super .mode,
172- });
207+ }) : super (type : ValueType . int ) ;
173208
174209 @override
175210 IntValue copyWith ({
@@ -197,7 +232,7 @@ class DoubleValue extends ValueModel<double> with SerializableMixin {
197232 required super .name,
198233 super .mode,
199234 super .value = 0 ,
200- }) {
235+ }) : super (type : ValueType . double ) {
201236 assert (mode != SetValueMode .toggle, '${mode .prettify } mode not supported.' );
202237 }
203238
@@ -207,7 +242,7 @@ class DoubleValue extends ValueModel<double> with SerializableMixin {
207242 required super .name,
208243 super .value = 0 ,
209244 super .mode,
210- });
245+ }) : super (type : ValueType . double ) ;
211246
212247 @override
213248 DoubleValue copyWith ({
@@ -235,7 +270,7 @@ class StringValue extends ValueModel<String> with SerializableMixin {
235270 required super .name,
236271 super .mode,
237272 super .value = '' ,
238- }) {
273+ }) : super (type : ValueType .string) {
239274 assert (mode != SetValueMode .toggle, '${mode .prettify } mode not supported.' );
240275 }
241276
0 commit comments