Mobile Development 11 min read

Translating UI DSL to Flutter Code: Architecture and Algorithms

The UI2CODE pipeline converts image‑derived JSON DSL layouts into Flutter code by constructing a hierarchical DSL tree, partitioning it into rows, columns or stacks, applying unsupervised text‑alignment and padding heuristics, generating templated widget code with separate data models, cutting development effort by roughly two‑thirds and now used internally at Xianyu.

Xianyu Technology
Xianyu Technology
Xianyu Technology
Translating UI DSL to Flutter Code: Architecture and Algorithms

Background: After image analysis, UI2CODE produces a DSL layout description in JSON, which is then used by an IntelliJ plugin to generate corresponding Flutter code.

DSL Definition: The DSL is a domain‑specific language that abstracts image and layout recognition results into a structured JSON, describing each layer’s frame, type, style, and value.

Core Idea: Convert the DSL tree into a Flutter widget tree by first establishing relationships among layers (containment, overlap, grouping) and then mapping those relationships to Flutter widgets.

Step 1 – DSL Tree Construction: Layers are grouped based on containment (Shape), block components (Block), repeated layouts (Repeat), and other custom tags. Overlapping and nested relationships are resolved to form a hierarchical DSL tree.

Step 2 – Flutter Tree Construction: The algorithm removes outer padding, then performs horizontal (Row) and vertical (Column) cuts to partition the layout. When a cut is not possible, an absolute layout (Stack) is used, with special handling for text layers via an original frame (oriFrame) to avoid incorrect stacking.

Text Handling: Text widgets are examined for multi‑line or fixed‑width content, padding distribution, and stretchability. A priority system determines which widget receives a Flex property, and alignment (left, center, right) is inferred using an unsupervised distance‑based algorithm.

Alignment Optimization: For Containers, Rows, and Columns, internal padding is analyzed to set appropriate crossAxisAlignment and mainAxisAlignment, improving the aesthetic of the generated code.

Code Generation: Two node types – View and Layout – are defined. Each node provides a code template; for example:

new Column(
    #{alignment},
    children:[
        #{children},
    ]
),

The tree is traversed from the root, assembling the final Flutter widget code.

Data Separation: Generated code and data models are split into separate sections, allowing dynamic data injection and reducing code complexity.

Overall Architecture: The system combines static image analysis, DSL parsing, Flutter tree building, and a factory pattern for post‑processing optimizations.

Business Integration: Deployed internally at Xianyu, UI2CODE automates UI development, reducing effort by about two‑thirds and promoting code consistency across teams.

Future Plans: Continue improving accuracy, explore NLP and AST techniques, and open the tool to the broader Flutter community.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

FlutterMobile DevelopmentCode GenerationDSLlayoutUI automation
Xianyu Technology
Written by

Xianyu Technology

Official account of the Xianyu technology team

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.