1+ import 'package:dogs_core/dogs_converter_utils.dart' ;
12import 'package:dogs_core/dogs_core.dart' ;
23import 'package:flutter/painting.dart' ;
34
4- List <double > _parseDoubleTuple4 (dynamic value, {required String typeName}) {
5- if (value is ! List ) {
6- throw DogSerializerException (message: "Invalid $typeName value, expected a list" );
7- }
8- if (value.length != 4 ) {
9- throw DogSerializerException (message: "Invalid $typeName value, expected 4 values" );
10- }
11- final [a, b, c, d] = value;
12- if (a is num && b is num && c is num && d is num ) {
13- return [a.toDouble (), b.toDouble (), c.toDouble (), d.toDouble ()];
5+ List <double > _parseDoubleTuple4 (dynamic value, DogEngine engine, DogConverter converter) {
6+ var list = converter.expects <List >(value, engine);
7+ if (list.length != 4 ) {
8+ throw DogSerializerException (
9+ message: "Expected list of 4 numeric values" ,
10+ converter: converter,
11+ );
1412 }
15- throw DogSerializerException (message: "Invalid $typeName value, expected numeric values" );
13+ return [
14+ converter.readAs <double >(list[0 ], engine),
15+ converter.readAs <double >(list[1 ], engine),
16+ converter.readAs <double >(list[2 ], engine),
17+ converter.readAs <double >(list[3 ], engine),
18+ ];
1619}
1720
1821@linkSerializer
@@ -21,14 +24,8 @@ class FlutterOffsetConverter extends SimpleDogConverter<Offset> {
2124
2225 @override
2326 Offset deserialize (value, DogEngine engine) {
24- if (value is Map <String , dynamic >) {
25- var dx = value["dx" ];
26- var dy = value["dy" ];
27- if (dx is num && dy is num ) {
28- return Offset (dx.toDouble (), dy.toDouble ());
29- }
30- }
31- throw DogSerializerException (message: "Invalid offset value" , converter: this );
27+ var map = readAsMap (value, engine);
28+ return Offset (map.read <double >("dx" ), map.read <double >("dy" ));
3229 }
3330
3431 @override
@@ -43,14 +40,8 @@ class FlutterSizeConverter extends SimpleDogConverter<Size> {
4340
4441 @override
4542 Size deserialize (value, DogEngine engine) {
46- if (value is Map <String , dynamic >) {
47- var width = value["width" ];
48- var height = value["height" ];
49- if (width is num && height is num ) {
50- return Size (width.toDouble (), height.toDouble ());
51- }
52- }
53- throw DogSerializerException (message: "Invalid size value" , converter: this );
43+ var map = readAsMap (value, engine);
44+ return Size (map.read <double >("width" ), map.read <double >("height" ));
5445 }
5546
5647 @override
@@ -65,18 +56,14 @@ class FlutterRectConverter extends SimpleDogConverter<Rect> {
6556
6657 @override
6758 Rect deserialize (value, DogEngine engine) {
68- return fromValue (value);
59+ final [left, top, right, bottom] = _parseDoubleTuple4 (value, engine, this );
60+ return Rect .fromLTRB (left, top, right, bottom);
6961 }
7062
7163 @override
7264 serialize (Rect value, DogEngine engine) {
7365 return [value.left, value.top, value.right, value.bottom];
7466 }
75-
76- static Rect fromValue (dynamic fieldValue) {
77- final [left, top, right, bottom] = _parseDoubleTuple4 (fieldValue, typeName: "rect" );
78- return Rect .fromLTRB (left, top, right, bottom);
79- }
8067}
8168
8269@linkSerializer
@@ -85,18 +72,14 @@ class FlutterEdgeInsetsConverter extends SimpleDogConverter<EdgeInsets> {
8572
8673 @override
8774 EdgeInsets deserialize (value, DogEngine engine) {
88- return fromValue (value);
75+ final [left, top, right, bottom] = _parseDoubleTuple4 (value, engine, this );
76+ return EdgeInsets .fromLTRB (left, top, right, bottom);
8977 }
9078
9179 @override
9280 serialize (EdgeInsets value, DogEngine engine) {
9381 return [value.left, value.top, value.right, value.bottom];
9482 }
95-
96- static EdgeInsets fromValue (dynamic fieldValue) {
97- final [left, top, right, bottom] = _parseDoubleTuple4 (fieldValue, typeName: "edge insets" );
98- return EdgeInsets .fromLTRB (left, top, right, bottom);
99- }
10083}
10184
10285@linkSerializer
@@ -130,26 +113,19 @@ class FlutterBorderRadiusConverter extends SimpleDogConverter<BorderRadius> {
130113
131114 @override
132115 BorderRadius deserialize (value, DogEngine engine) {
133- return fromValue (value);
134- }
135-
136- @override
137- serialize (BorderRadius value, DogEngine engine) {
138- return [value.topLeft.x, value.topRight.x, value.bottomLeft.x, value.bottomRight.x];
139- }
140-
141- static BorderRadius fromValue (dynamic fieldValue) {
142- final [topLeft, topRight, bottomLeft, bottomRight] = _parseDoubleTuple4 (
143- fieldValue,
144- typeName: "border radius" ,
145- );
116+ final [topLeft, topRight, bottomLeft, bottomRight] = _parseDoubleTuple4 (value, engine, this );
146117 return BorderRadius .only (
147118 topLeft: Radius .circular (topLeft),
148119 topRight: Radius .circular (topRight),
149120 bottomLeft: Radius .circular (bottomLeft),
150121 bottomRight: Radius .circular (bottomRight),
151122 );
152123 }
124+
125+ @override
126+ serialize (BorderRadius value, DogEngine engine) {
127+ return [value.topLeft.x, value.topRight.x, value.bottomLeft.x, value.bottomRight.x];
128+ }
153129}
154130
155131@linkSerializer
@@ -158,25 +134,30 @@ class FlutterRRectConverter extends SimpleDogConverter<RRect> {
158134
159135 @override
160136 RRect deserialize (value, DogEngine engine) {
161- if (value is Map <String , dynamic >) {
162- final rect = FlutterRectConverter .fromValue (value["dimensions" ]);
163- final borderRadius = FlutterBorderRadiusConverter .fromValue (value["radii" ]);
164- return RRect .fromRectAndCorners (
165- rect,
166- topLeft: borderRadius.topLeft,
167- topRight: borderRadius.topRight,
168- bottomLeft: borderRadius.bottomLeft,
169- bottomRight: borderRadius.bottomRight,
170- );
171- }
172- throw DogSerializerException (message: "Invalid RRect value" , converter: this );
137+ var map = readAsMap (value, engine);
138+ final rect = map.read <Rect >("dimensions" );
139+ final borderRadius = map.read <BorderRadius >("radii" );
140+ return RRect .fromRectAndCorners (
141+ rect,
142+ topLeft: borderRadius.topLeft,
143+ topRight: borderRadius.topRight,
144+ bottomLeft: borderRadius.bottomLeft,
145+ bottomRight: borderRadius.bottomRight,
146+ );
173147 }
174148
175149 @override
176150 serialize (RRect value, DogEngine engine) {
177151 return < String , dynamic > {
178- "dimensions" : [value.left, value.top, value.right, value.bottom],
179- "radii" : [value.tlRadius.x, value.trRadius.x, value.blRadius.x, value.brRadius.x],
152+ "dimensions" : engine.toNative <Rect >(value.outerRect),
153+ "radii" : engine.toNative <BorderRadius >(
154+ BorderRadius .only (
155+ topLeft: value.tlRadius,
156+ topRight: value.trRadius,
157+ bottomLeft: value.blRadius,
158+ bottomRight: value.brRadius,
159+ ),
160+ ),
180161 };
181162 }
182163}
0 commit comments