Skip to content

Commit 3146e60

Browse files
committed
Add variable colors to Flutter
1 parent 8449fb2 commit 3146e60

3 files changed

Lines changed: 34 additions & 16 deletions

File tree

packages/backend/src/flutter/builderImpl/flutterColor.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,24 @@ export const flutterColorFromDirectFills = (
3535
const fill = retrieveTopFill(fills);
3636

3737
if (fill && fill.type === "SOLID") {
38-
return flutterColor(fill.color, fill.opacity ?? 1.0);
38+
return flutterColor(
39+
fill.color,
40+
fill.opacity ?? 1.0,
41+
(fill as any).variableColorName
42+
);
3943
} else if (
4044
fill &&
4145
(fill.type === "GRADIENT_LINEAR" ||
4246
fill.type === "GRADIENT_ANGULAR" ||
4347
fill.type === "GRADIENT_RADIAL")
4448
) {
4549
if (fill.gradientStops.length > 0) {
46-
return flutterColor(fill.gradientStops[0].color, fill.opacity ?? 1.0);
50+
const stop = fill.gradientStops[0];
51+
return flutterColor(
52+
stop.color,
53+
fill.opacity ?? 1.0,
54+
(stop as any).variableColorName
55+
);
4756
}
4857
}
4958

@@ -66,7 +75,7 @@ export const flutterBoxDecorationColor = (
6675

6776
if (fill && fill.type === "SOLID") {
6877
const opacity = fill.opacity ?? 1.0;
69-
return { color: flutterColor(fill.color, opacity) };
78+
return { color: flutterColor(fill.color, opacity, (fill as any).variableColorName) };
7079
} else if (
7180
fill?.type === "GRADIENT_LINEAR" ||
7281
fill?.type === "GRADIENT_RADIAL" ||
@@ -126,7 +135,7 @@ const gradientDirection = (angle: number): string => {
126135

127136
const flutterRadialGradient = (fill: GradientPaint): string => {
128137
const colors = fill.gradientStops
129-
.map((d) => flutterColor(d.color, d.color.a))
138+
.map((d) => flutterColor(d.color, d.color.a, (d as any).variableColorName))
130139
.join(", ");
131140

132141
const x = numberToFixedString(fill.gradientTransform[0][2]);
@@ -144,7 +153,7 @@ const flutterRadialGradient = (fill: GradientPaint): string => {
144153

145154
const flutterAngularGradient = (fill: GradientPaint): string => {
146155
const colors = fill.gradientStops
147-
.map((d) => flutterColor(d.color, d.color.a))
156+
.map((d) => flutterColor(d.color, d.color.a, (d as any).variableColorName))
148157
.join(", ");
149158

150159
const x = numberToFixedString(fill.gradientTransform[0][2]);
@@ -166,7 +175,7 @@ const flutterLinearGradient = (fill: GradientPaint): string => {
166175
const y = Math.sin(radians).toFixed(2);
167176

168177
const colors = fill.gradientStops
169-
.map((d) => flutterColor(d.color, d.color.a))
178+
.map((d) => flutterColor(d.color, d.color.a, (d as any).variableColorName))
170179
.join(", ");
171180

172181
return generateWidgetCode("LinearGradient", {
@@ -205,21 +214,31 @@ const opacityToAlpha = (opacity: number): number => {
205214
return Math.round(opacity * 255);
206215
};
207216

208-
export const flutterColor = (color: RGB, opacity: number): string => {
217+
export const flutterColor = (
218+
color: RGB,
219+
opacity: number,
220+
variableColorName?: string
221+
): string => {
209222
const sum = color.r + color.g + color.b;
223+
let colorCode = "";
210224

211225
if (sum === 0) {
212-
return opacity === 1
226+
colorCode = opacity === 1
213227
? "Colors.black"
214228
: `Colors.black.withValues(alpha: ${opacityToAlpha(opacity)})`;
215-
}
216-
217-
if (sum === 3) {
218-
return opacity === 1
229+
} else if (sum === 3) {
230+
colorCode = opacity === 1
219231
? "Colors.white"
220232
: `Colors.white.withValues(alpha: ${opacityToAlpha(opacity)})`;
233+
} else {
234+
// Always use full 8-digit hex which includes alpha channel
235+
colorCode = `Color(0x${rgbTo8hex(color, opacity).toUpperCase()})`;
221236
}
222237

223-
// Always use full 8-digit hex which includes alpha channel
224-
return `Color(0x${rgbTo8hex(color, opacity).toUpperCase()})`;
238+
// Add variable name as a comment if it exists
239+
if (variableColorName) {
240+
return `${colorCode} /* ${variableColorName} */`;
241+
}
242+
243+
return colorCode;
225244
};

packages/plugin-ui/src/codegenPreferenceOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const preferenceOptions: LocalCodegenPreferenceOptions[] = [
4242
description:
4343
"Export code using Figma variables as colors. Example: 'bg-background' instead of 'bg-white'.",
4444
isDefault: false,
45-
includedLanguages: ["HTML", "Tailwind"],
45+
includedLanguages: ["HTML", "Tailwind", "Flutter"],
4646
},
4747
{
4848
itemType: "individual_select",

packages/plugin-ui/src/components/CodePanel.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ const CodePanel = (props: CodePanelProps) => {
129129
stylingPreferences: frameworkPreferences.filter((p) =>
130130
stylingPropertyNames.includes(p.propertyName),
131131
),
132-
133132
selectableSettingsFiltered: selectPreferenceOptions.filter((p) =>
134133
p.includedLanguages?.includes(selectedFramework),
135134
),

0 commit comments

Comments
 (0)