Skip to content

Commit 4821ec2

Browse files
authored
fix: Attach layout listeners to new children (#3648)
Prior to this fix, when adding a child to the `LayoutComponent`, resizing the child wouldn't cause the `LayoutComponent` to resize. This was because the children size listeners were only attached on instantiation of `LayoutComponent` *and* during setting of `shrinkWrap`. This fix attaches the size listeners to a single child when it is added to `LayoutComponent` via `onChildrenChanged`. `setupSizeListeners` is not used because it iterates through all the children, and is unnecessary.
1 parent 97b9ba8 commit 4821ec2

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

packages/flame/lib/experimental.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export 'src/experimental/geometry/shapes/rectangle.dart' show Rectangle;
1818
export 'src/experimental/geometry/shapes/rounded_rectangle.dart'
1919
show RoundedRectangle;
2020
export 'src/experimental/geometry/shapes/shape.dart' show Shape;
21-
export 'src/experimental/layout_component.dart' show LayoutComponent;
21+
export 'src/experimental/layout_component.dart' show LayoutComponent, Direction;
2222
export 'src/experimental/row_component.dart' show RowComponent;

packages/flame/lib/src/experimental/layout_component.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ abstract class LayoutComponent extends PositionComponent {
175175

176176
@override
177177
void onChildrenChanged(Component child, ChildrenChangeType type) {
178+
if (child is! PositionComponent) {
179+
return;
180+
}
181+
// setupSizeListeners(), but for a single child
182+
if (type == ChildrenChangeType.added && shrinkWrap) {
183+
child.size.addListener(layoutChildren);
184+
} else {
185+
child.size.removeListener(layoutChildren);
186+
}
178187
layoutChildren();
179188
}
180189

packages/flame/test/experimental/layout_component_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,5 +325,36 @@ void main() {
325325
},
326326
});
327327
});
328+
group('children', () {
329+
runLayoutComponentTestRegistry(
330+
{
331+
'size responds when children are added and then resized':
332+
(game, direction) async {
333+
final circle = CircleComponent(radius: 20);
334+
final rectangle2 = RectangleComponent(size: Vector2(100, 50));
335+
// final rectangle2 = RectangleComponent(size: Vector2(200, 70));
336+
final layoutComponent = LayoutComponent.fromDirection(
337+
direction,
338+
shrinkWrap: true,
339+
crossAxisAlignment: CrossAxisAlignment.stretch,
340+
);
341+
await game.ensureAdd(layoutComponent);
342+
expect(layoutComponent.size, Vector2.zero());
343+
await layoutComponent.ensureAddAll([
344+
circle,
345+
rectangle2,
346+
]);
347+
rectangle2.size = Vector2(200, 70);
348+
expect(
349+
layoutComponent.size,
350+
switch (direction) {
351+
Direction.horizontal => Vector2(40 + 200, 70),
352+
Direction.vertical => Vector2(200, 70 + 40),
353+
},
354+
);
355+
},
356+
},
357+
);
358+
});
328359
});
329360
}

0 commit comments

Comments
 (0)