Skip to content

Commit 8792338

Browse files
authored
flutter: skip gen Container with empty props (#192)
1 parent 0132452 commit 8792338

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

packages/backend/src/flutter/flutterContainer.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,27 @@ export const flutterContainer = (node: SceneNode, child: string): string => {
5353
}
5454
}
5555

56-
properties.child = child;
57-
5856
if (width || height || propBoxDecoration || clipBehavior) {
57+
properties.width = skipDefaultProperty(width, "0");
58+
properties.height = skipDefaultProperty(height, "0");
59+
properties.padding = propPadding;
60+
properties.clipBehavior = clipBehavior;
61+
5962
const parsedDecoration = skipDefaultProperty(
6063
propBoxDecoration,
6164
"BoxDecoration()",
6265
);
63-
result = generateWidgetCode("Container", {
64-
width: skipDefaultProperty(width, "0"),
65-
height: skipDefaultProperty(height, "0"),
66-
padding: propPadding,
67-
clipBehavior: clipBehavior,
68-
decoration: clipBehavior ? propBoxDecoration : parsedDecoration,
69-
...properties,
70-
});
66+
properties.decoration = clipBehavior ? propBoxDecoration : parsedDecoration;
67+
68+
const isEmptyProps = hasEmptyProps(properties);
69+
if (isEmptyProps) {
70+
result = child;
71+
} else {
72+
properties.child = child;
73+
result = generateWidgetCode("Container", {
74+
...properties,
75+
});
76+
}
7177
} else if (propPadding) {
7278
// if there is just a padding, add Padding
7379
result = generateWidgetCode("Padding", {
@@ -96,6 +102,16 @@ export const flutterContainer = (node: SceneNode, child: string): string => {
96102
return result;
97103
};
98104

105+
const hasEmptyProps = (props: Record<string, string>): boolean => {
106+
let isEmpty = true;
107+
for (const key in props) {
108+
const value = props[key];
109+
const defValue = value.length > 0 ? "0" : "";
110+
isEmpty = isEmpty && skipDefaultProperty(value, defValue).length == 0;
111+
}
112+
return isEmpty;
113+
}
114+
99115
const getDecoration = (node: SceneNode): string => {
100116
if (!("fills" in node)) {
101117
return "";

0 commit comments

Comments
 (0)