Skip to content

Commit 9ac53a6

Browse files
authored
feat: Implement padding component inflateChild (#3785)
- Exposes `inflateChild` as a field of `SingleLayoutComponent` - Implements `inflateChild` for `PaddingComponent` - Refactors `ExpandedComponent`
1 parent 79a5c20 commit 9ac53a6

File tree

7 files changed

+94
-11
lines changed

7 files changed

+94
-11
lines changed

examples/lib/stories/experimental/experimental.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ void addExperimentalStories(Dashbook dashbook) {
5454
'Wrap with ExpandedComponent',
5555
false,
5656
),
57+
paddingInflateChild: context.boolProperty(
58+
'Padding Component inflates child',
59+
false,
60+
),
5761
),
5862
);
5963
},

examples/lib/stories/experimental/layout_component_example_1.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class LayoutComponentExample1 extends FlameGame with DragCallbacks {
1616
required this.demoSize,
1717
required this.padding,
1818
required this.expandedMode,
19+
required this.paddingInflateChild,
1920
});
2021

2122
static const String description = '''
@@ -31,6 +32,7 @@ the various ways you can change this layout.
3132
final LayoutComponentExampleSize demoSize;
3233
final EdgeInsets padding;
3334
final bool expandedMode;
35+
final bool paddingInflateChild;
3436

3537
@override
3638
FutureOr<void> onLoad() {
@@ -54,6 +56,7 @@ the various ways you can change this layout.
5456
position: Vector2.zero(),
5557
padding: padding,
5658
expandedMode: expandedMode,
59+
paddingInflateChild: paddingInflateChild,
5760
size: demoSize.toVector2(),
5861
),
5962
],
@@ -83,6 +86,7 @@ class LayoutDemo1 extends LinearLayoutComponent {
8386
required super.position,
8487
required EdgeInsets padding,
8588
required bool expandedMode,
89+
required this.paddingInflateChild,
8690
super.size,
8791
super.key,
8892
}) : _padding = padding,
@@ -96,7 +100,13 @@ class LayoutDemo1 extends LinearLayoutComponent {
96100
set expandedMode(bool value) {
97101
_expandedMode = value;
98102
removeAll(children.toList());
99-
addAll(createComponentList(expandedMode: expandedMode, padding: padding));
103+
addAll(
104+
createComponentList(
105+
expandedMode: expandedMode,
106+
padding: padding,
107+
inflateChild: paddingInflateChild,
108+
),
109+
);
100110
}
101111

102112
EdgeInsets _padding = EdgeInsets.zero;
@@ -108,10 +118,18 @@ class LayoutDemo1 extends LinearLayoutComponent {
108118
paddingComponent?.padding = padding;
109119
}
110120

121+
final bool paddingInflateChild;
122+
111123
@override
112124
FutureOr<void> onLoad() {
113125
super.onLoad();
114-
addAll(createComponentList(expandedMode: expandedMode, padding: padding));
126+
addAll(
127+
createComponentList(
128+
expandedMode: expandedMode,
129+
padding: padding,
130+
inflateChild: paddingInflateChild,
131+
),
132+
);
115133
}
116134

117135
PaddingComponent? get paddingComponent {
@@ -124,6 +142,7 @@ class LayoutDemo1 extends LinearLayoutComponent {
124142
static List<Component> createComponentList({
125143
required bool expandedMode,
126144
required EdgeInsets padding,
145+
required bool inflateChild,
127146
}) {
128147
return [
129148
TextComponent(text: 'Some short text'),
@@ -143,6 +162,7 @@ class LayoutDemo1 extends LinearLayoutComponent {
143162
ExpandedComponent(
144163
child: PaddingComponent(
145164
padding: padding,
165+
inflateChild: inflateChild,
146166
child: RectangleComponent(
147167
size: Vector2(96, 96),
148168
paint: Paint()..color = Colors.blue,
@@ -152,6 +172,7 @@ class LayoutDemo1 extends LinearLayoutComponent {
152172
else
153173
PaddingComponent(
154174
padding: padding,
175+
inflateChild: inflateChild,
155176
child: RectangleComponent(
156177
size: Vector2(96, 96),
157178
paint: Paint()..color = Colors.blue,

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ class ExpandedComponent extends SingleLayoutComponent
3838
super.position,
3939
super.anchor,
4040
super.priority,
41-
this.inflateChild = true,
41+
super.inflateChild = true,
4242
super.child,
4343
}) : super(size: null);
4444

45-
/// Whether or not this [ExpandedComponent] will set [child]'s size to its own
46-
final bool inflateChild;
47-
4845
@override
4946
void setLayoutAxisLength(LayoutAxis axis, double? value) {
5047
super.setLayoutAxisLength(axis, value);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ abstract class LayoutComponent extends PositionComponent {
3434
/// vector components, and subsequently selective setting of the
3535
/// size components.
3636
void setLayoutSize(double? layoutSizeX, double? layoutSizeY) {
37-
_layoutSizeX = layoutSizeX;
38-
_layoutSizeY = layoutSizeY;
39-
resetSize();
37+
setLayoutAxisLength(LayoutAxis.x, layoutSizeX);
38+
setLayoutAxisLength(LayoutAxis.y, layoutSizeY);
4039
}
4140

4241
/// A helper function to set the appropriate layout dimension based on

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import 'package:flutter/rendering.dart';
1616
/// You may set [padding] as well as the [child] after the fact, and it will
1717
/// cause the layout to refresh.
1818
///
19+
/// If [inflateChild] is true, [resetSize] sets the child's size to fill up
20+
/// available space via [syncChildSize]. If the child is a [LayoutComponent]
21+
/// descendant, then [resetSize] uses the [LayoutComponent.setLayoutSize].
22+
///
1923
/// Example usage:
2024
/// ```dart
2125
/// PaddingComponent(
@@ -31,6 +35,7 @@ class PaddingComponent extends SingleLayoutComponent {
3135
super.position,
3236
super.priority,
3337
super.size,
38+
super.inflateChild = false,
3439
PositionComponent? child,
3540
}) : _padding = padding ?? EdgeInsets.zero,
3641
super(child: null) {
@@ -48,8 +53,6 @@ class PaddingComponent extends SingleLayoutComponent {
4853

4954
@override
5055
void layoutChildren() {
51-
// Only resets to null if it's already null. This way, we avoid overwriting
52-
// an explicit width/height.
5356
resetSize();
5457
final child = this.child;
5558
if (child == null) {
@@ -59,6 +62,11 @@ class PaddingComponent extends SingleLayoutComponent {
5962
child.topLeftPosition.setFrom(padding.topLeft.toVector2());
6063
}
6164

65+
@override
66+
Vector2 get availableSize {
67+
return padding.deflateSize(size.toSize()).toVector2();
68+
}
69+
6270
@override
6371
Vector2 get intrinsicSize {
6472
final childWidth = child?.size.x ?? 0;

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import 'package:flame/experimental.dart';
66
/// [ExpandedComponent] and [PaddingComponent], and can possibly be used to
77
/// refactor AlignComponent.
88
///
9+
/// [inflateChild] is simply a flag that signals whether the underlying layout
10+
/// machinery should alter its child's size. It's up to this class's subclasses
11+
/// to make use of this flag.
12+
///
913
/// Setting [child] automatically manages removing the old child from this
1014
/// component, as well as adding the new child to this component.
1115
abstract class SingleLayoutComponent extends LayoutComponent {
@@ -15,11 +19,14 @@ abstract class SingleLayoutComponent extends LayoutComponent {
1519
required super.anchor,
1620
required super.priority,
1721
required super.size,
22+
required this.inflateChild,
1823
required PositionComponent? child,
1924
}) {
2025
this.child = child;
2126
}
2227

28+
final bool inflateChild;
29+
2330
PositionComponent? _child;
2431

2532
/// The component that will be positioned by this component. The [child] will
@@ -37,6 +44,32 @@ abstract class SingleLayoutComponent extends LayoutComponent {
3744
}
3845
}
3946

47+
void syncChildSize() {
48+
if (!inflateChild) {
49+
return;
50+
}
51+
final child = this.child;
52+
if (child == null) {
53+
return;
54+
}
55+
if (child.size == availableSize) {
56+
return;
57+
}
58+
if (child is LayoutComponent) {
59+
child.setLayoutSize(availableSize.x, availableSize.y);
60+
} else {
61+
child.size = availableSize;
62+
}
63+
}
64+
65+
@override
66+
void resetSize() {
67+
super.resetSize();
68+
syncChildSize();
69+
}
70+
71+
Vector2 get availableSize => size;
72+
4073
@override
4174
Vector2 get intrinsicSize => child?.size ?? Vector2.zero();
4275
}

packages/flame/test/experimental/padding_component_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,26 @@ void main() {
8585
);
8686
},
8787
);
88+
testWithFlameGame(
89+
'properly sets child size when inflateChild is true',
90+
(game) async {
91+
final rectangle = RectangleComponent();
92+
const padding = EdgeInsets.symmetric(
93+
vertical: 16,
94+
horizontal: 24,
95+
);
96+
final paddingComponent = PaddingComponent(
97+
padding: padding,
98+
size: Vector2(100, 200),
99+
inflateChild: true,
100+
child: rectangle,
101+
);
102+
await game.ensureAdd(paddingComponent);
103+
expect(
104+
rectangle.size,
105+
Vector2(100 - padding.horizontal, 200 - padding.vertical),
106+
);
107+
},
108+
);
88109
});
89110
}

0 commit comments

Comments
 (0)