The Talent500 Blog
Understanding Flutter's Rendering Model 1

Understanding Flutter’s Rendering Model

Flutter is a well-designed Google UI toolkit for building natively compiled applications, be it for mobile, web, or desktop. Having a strong rendering engine and a flexible, extensible suite of widgets, it’s redefining the very paradigm of developing cross-platform apps. This article looks deep into how Flutter makes UI render on the screen, touching base with distinct phases of the process and the underlying architecture.

Four Phases of Flutter Rendering

1.Plan Phase

The layout phase is the phase in which every widget is provided with allocated space and a size. This is the most important phase in ensuring that each and every widget is sized and located properly on the screen.

Parent-to-Child Constraints: This would mean that constraints associated with parent widgets, such as minimums and maximums for both width and height, are passed down to children widgets, so that the rendering goes on knowing what is applicable.

Child-to-Parent Sizes: Child widgets size themselves based on incoming constraints and bubble that information back up to their parents. Iterations of this process work out the full widget tree to its final, correct state.

2.Stage of Painting

In the drawing phase, each of the widgets gets its canvas on which it paints itself. This phase is responsible for the realization of how the widgets would appear on the screen.

Drawing on Canvas: Widgets draw on the canvas to render how they should look. This could be text or even an image of a graphical element.

Create Layers: Any new widget put in will add in a new layer to overlay the previous ones and depict the final output.

3.Composition Stage

Composition involves assembling different layers to make a single complete scene. This stage ensures that all the visual elements are combined correctly to form the final UI.

Cluster Formation: Here, individual widgets are arranged based on the results of the layout phase.

Comprehensive Unit: The complete UI is fully embodied in the comprehensive unit. At this stage, layers acquired at the painting phase are merged by Flutter into a single scene. It may mean fusing different visual elements, such as text, images, and shapes, into one unit.

4.Rasterizing Phase

Rasterization converts the composed scene into a GPU-friendly format. This stage is important for the effective rendering of the UI on the screen.

Raster Image Creation: The layers are composed to form an image.

GPU rendering: A raster image of the display is displayed on the screen at a high frequency, typically 60 Hz. During rasterization, the composed scene is converted into pixels that the GPU can render. This is the processing stage that helps in efficiently and effectively presenting the UI.

The Three Trees within Flutter

Widget Tree

The widget tree would describe the configuration of the UI elements. It resembles a blueprint for what the UI would look like and behave like. Immutable Descriptions: Widgets are immutable and they describe a part of the UI, so immutability in them ensures that the UI stays predictable and coherent. Logical Structure: The tree describes the logical structure of the user interface, such as parent-child relationships and layout constraints.

1. Element Tree

Flutter provides a widget life cycle manager in the element tree. It guarantees the proper creation, update, and disposal of widgets.

2.Widget Instantiation: Each widget in the widget tree is expected to have an element responsible for its creation and lifecycle management.

Lifecycle Management: Manages the state of the widget and its lifecycle, i.e., its creation, updating, and disposal.

3.Render Object Tree

The Render Object tree is concerned with layout and painting, and the calculations of size. In fact, it performs the rendering of the UI to the screen.

Size and layout information: This is determined by the render objects for the widgets. Through this, the widgets are sized and laid out.

Painting Protocol: Render objects are responsible for painting widgets on the screen.

Making Flow with Flutter

The Creation of the Widget and Embedding It

  1. Instantiation: The Flutter framework starts with the point at which objects have to be created. This is nothing but what we call creating objects. These are following the code in Dart.
  2. Immutability: The nature of widgets is that they are immutable. So, widgets will not be modified but will be created. In this case, they merely describe how the UI should look.
  3. Configuration: Properties of widgets, e.g., the size, color, layout constraints.

Element Tree Made

  1. Element Creation: An element is created for every new widget, with an element tree being used to handle the widgets’ life cycle.
  2. State Management: Elements hold the state, hence a necessity to refresh the widget tree in case a change appears within the state.
  3. Link: Links the widget tree and the render object tree.

Object Instantiation

  1. Corresponding Render Objects: Each of the elements will have a corresponding render object.
  2. Responsibilities: Layout and painting are the tasks of render objects.

iii. Size and Position: The render object tree stores the size and position of all the widgets.

Design Phase

  1. Calculation: Rendering objects calculate their size and position using the constraints from their parent objects.
  2. Recursion: The whole process of layout is rooted at the root, and it is top-down and recursive.

Painting Phase

  1. Painting: Translating objects in such a way that their representation can be visualized on a piece of canvas.
  2. Recursive Process: A painting is a recursive process, which means that child objects are painted over parents.

Compositing Phase

  1. Combine: Flutter Engine combines the painted layers to create the final scene.
  2. Translated: The scene is then translated into a form that is well-suited for transmission to the GPU.

Rasterization Phase

  1. Pixel Conversion: The scripted scene is converted to pixels.
  2. GPU Rendering: In simple terms, the GPU is working on drawing the pixels at a very high frequency on the screen—usually 60 Hz. This way, it makes animations and user interactions look smooth while not chewing up your CPU in the process. 

Conclusion

Understanding Flutter’s rendering model reveals the meticulous processes behind its powerful and efficient UI toolkit. By breaking down the rendering into distinct phases—planning, painting, composition, and rasterization—Flutter ensures each widget is accurately displayed with optimal performance. The three trees within Flutter (widget, element, and render object) play crucial roles in managing the lifecycle, layout, and painting of widgets, providing a seamless development experience.

 

0
Taniya Pan

Taniya Pan

Add comment