Visitar URL original
Fix sizing, drive by fix for visitchildren on some resolved node type… · flutter/packages@df786bc · GitHub
Skip to content

Commit df786bc

Browse files
authored
Fix sizing, drive by fix for visitchildren on some resolved node types (#64)
1 parent 7b8a89b commit df786bc

4 files changed

Lines changed: 93 additions & 5 deletions

File tree

‎packages/vector_graphics/lib/vector_graphics.dart‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,26 @@ class _VectorGraphicsWidgetState extends State<VectorGraphic> {
143143
if (pictureInfo == null) {
144144
return SizedBox(width: widget.width, height: widget.height);
145145
}
146+
147+
// If the caller did not specify a width or height, fall back to the
148+
// size of the graphic.
149+
// If the caller did specify a width or height, preserve the aspect ratio
150+
// of the graphic and center it within that width and height.
151+
double? width = widget.width;
152+
double? height = widget.height;
153+
154+
if (width == null && height == null) {
155+
width = pictureInfo.size.width;
156+
height = pictureInfo.size.height;
157+
} else if (height != null && !pictureInfo.size.isEmpty) {
158+
width = height / pictureInfo.size.height * pictureInfo.size.width;
159+
} else if (width != null && !pictureInfo.size.isEmpty) {
160+
height = width / pictureInfo.size.width * pictureInfo.size.height;
161+
}
162+
146163
Widget child = SizedBox(
147-
width: widget.width,
148-
height: widget.height,
164+
width: width,
165+
height: height,
149166
child: FittedBox(
150167
fit: widget.fit,
151168
alignment: widget.alignment,

‎packages/vector_graphics/test/vector_graphics_test.dart‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ void main() {
6363
expect(listener.toPicture, throwsAssertionError);
6464
});
6565

66-
testWidgets('Creates layout widgets when VectorGraphic is sized',
66+
testWidgets(
67+
'Creates layout widgets when VectorGraphic is sized (0x0 graphic)',
6768
(WidgetTester tester) async {
6869
final VectorGraphicsBuffer buffer = VectorGraphicsBuffer();
6970
await tester.pumpWidget(VectorGraphic(
@@ -82,6 +83,46 @@ void main() {
8283
expect(sizedBox.height, 100);
8384
});
8485

86+
testWidgets('Creates layout widgets when VectorGraphic is sized (1:1 ratio)',
87+
(WidgetTester tester) async {
88+
final VectorGraphicsBuffer buffer = VectorGraphicsBuffer();
89+
const VectorGraphicsCodec().writeSize(buffer, 50, 50);
90+
await tester.pumpWidget(VectorGraphic(
91+
loader: TestBytesLoader(buffer.done()),
92+
width: 100,
93+
height: 100,
94+
));
95+
await tester.pumpAndSettle();
96+
97+
expect(find.byType(SizedBox), findsNWidgets(2));
98+
99+
final SizedBox sizedBox =
100+
(find.byType(SizedBox).evaluate().first.widget as SizedBox);
101+
102+
expect(sizedBox.width, 100);
103+
expect(sizedBox.height, 100);
104+
});
105+
106+
testWidgets('Creates layout widgets when VectorGraphic is sized (3:5 ratio)',
107+
(WidgetTester tester) async {
108+
final VectorGraphicsBuffer buffer = VectorGraphicsBuffer();
109+
const VectorGraphicsCodec().writeSize(buffer, 30, 50);
110+
await tester.pumpWidget(VectorGraphic(
111+
loader: TestBytesLoader(buffer.done()),
112+
width: 100,
113+
height: 100,
114+
));
115+
await tester.pumpAndSettle();
116+
117+
expect(find.byType(SizedBox), findsNWidgets(2));
118+
119+
final SizedBox sizedBox =
120+
(find.byType(SizedBox).evaluate().first.widget as SizedBox);
121+
122+
expect(sizedBox.width, 60);
123+
expect(sizedBox.height, 100);
124+
});
125+
85126
testWidgets('Creates alignment widgets when VectorGraphic is aligned',
86127
(WidgetTester tester) async {
87128
final VectorGraphicsBuffer buffer = VectorGraphicsBuffer();

‎packages/vector_graphics_compiler/lib/src/svg/resolver.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ class ResolvedClipNode extends Node {
270270
}
271271

272272
@override
273-
void visitChildren(NodeCallback visitor) {}
273+
void visitChildren(NodeCallback visitor) {
274+
visitor(child);
275+
}
274276
}
275277

276278
/// A mask node with child and mask fully resolved.
@@ -299,5 +301,7 @@ class ResolvedMaskNode extends Node {
299301
}
300302

301303
@override
302-
void visitChildren(NodeCallback visitor) {}
304+
void visitChildren(NodeCallback visitor) {
305+
visitor(child);
306+
}
303307
}

‎packages/vector_graphics_compiler/test/resolver_test.dart‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:test/test.dart';
66
import 'package:vector_graphics_compiler/src/geometry/basic_types.dart';
77
import 'package:vector_graphics_compiler/src/geometry/matrix.dart';
88
import 'package:vector_graphics_compiler/src/geometry/path.dart';
9+
import 'package:vector_graphics_compiler/src/paint.dart';
910
import 'package:vector_graphics_compiler/src/svg/node.dart';
1011
import 'package:vector_graphics_compiler/src/svg/parser.dart';
1112
import 'package:vector_graphics_compiler/src/svg/resolver.dart';
@@ -85,4 +86,29 @@ void main() {
8586

8687
expect(nodes, isEmpty);
8788
});
89+
90+
test('visitChildren on clips and masks', () {
91+
final ResolvedClipNode clip = ResolvedClipNode(
92+
clips: <Path>[],
93+
child: Node.empty,
94+
);
95+
96+
final ResolvedMaskNode mask = ResolvedMaskNode(
97+
child: Node.empty,
98+
mask: Node.empty,
99+
blendMode: BlendMode.color,
100+
);
101+
102+
int visitCount = 0;
103+
clip.visitChildren((Node child) {
104+
visitCount += 1;
105+
expect(child, Node.empty);
106+
});
107+
mask.visitChildren((Node child) {
108+
visitCount += 1;
109+
expect(child, Node.empty);
110+
});
111+
112+
expect(visitCount, 2);
113+
});
88114
}

0 commit comments

Comments
 (0)