Thanks to visit codestin.com
Credit goes to github.com

Skip to content

v1: Fix component updates on Dart side#5756

Merged
FeodorFitsner merged 1 commit intomainfrom
v1-reactive-fixes
Oct 28, 2025
Merged

v1: Fix component updates on Dart side#5756
FeodorFitsner merged 1 commit intomainfrom
v1-reactive-fixes

Conversation

@FeodorFitsner
Copy link
Contributor

@FeodorFitsner FeodorFitsner commented Oct 28, 2025

Fix #5750

Working example of a "Shape drawer":

from dataclasses import dataclass, field

import flet as ft
from flet import canvas

POINT_RADIUS = 4
PAINT = ft.Paint(
    style=ft.PaintingStyle.STROKE,
    color=ft.Colors.RED,
    stroke_width=2,
)


@dataclass
class Point:
    x: float = 0
    y: float = 0


@dataclass
@ft.observable
class Polygon:
    points: list[Point] = field(default_factory=list)


@dataclass
@ft.observable
class State:
    polygons: list[Polygon] = field(default_factory=list[Polygon])


@ft.component
def PolygonView(polygon: Polygon) -> canvas.Path:
    return canvas.Path(
        elements=[
            canvas.Path.MoveTo(
                point.x,
                point.y,
            )
            if i == 0
            else canvas.Path.LineTo(
                point.x,
                point.y,
            )
            for i, point in enumerate(polygon.points)
        ]
        + [canvas.Path.Close()],
        paint=PAINT,
    )


@ft.component
def App():
    state, _ = ft.use_state(
        State(
            polygons=[
                Polygon([Point(50, 50), Point(150, 50), Point(100, 150)]),
            ]
        )
    )
    # state, _ = ft.use_state(State(polygons=[Polygon()]))

    def handle_tap_down(e: ft.TapEvent):
        # add point to the top polygon
        if e.local_position is not None:
            state.polygons[-1].points.append(
                Point(x=e.local_position.x, y=e.local_position.y)
            )
            if len(state.polygons[-1].points) == 1:
                # add a temporary point to be updated on hover
                state.polygons[-1].points.append(
                    Point(x=e.local_position.x, y=e.local_position.y)
                )

    def handle_hover(e: ft.HoverEvent):
        # update position of the last point in the top polygon
        if e.local_position is not None and len(state.polygons[-1].points) > 0:
            state.polygons[-1].points[-1].x = e.local_position.x
            state.polygons[-1].points[-1].y = e.local_position.y
            state.notify()

    def handle_secondary_tap_down(e: ft.TapEvent):
        # add new polygon
        state.polygons.append(Polygon())

    return ft.GestureDetector(
        on_tap_down=handle_tap_down,
        on_secondary_tap_down=handle_secondary_tap_down,
        on_hover=handle_hover,
        content=ft.Container(
            content=canvas.Canvas(
                width=float("inf"),
                height=float("inf"),
                shapes=[PolygonView(polygon) for polygon in state.polygons],
            ),
            width=500,
            height=500,
            bgcolor=ft.Colors.GREY_300,
            alignment=ft.Alignment.TOP_LEFT,
        ),
    )


ft.run(lambda page: page.render(App))

Summary by Sourcery

Refine Control._transformIfControl to traverse nested maps and lists and instantiate controls wherever "_c" keys appear

Bug Fixes:

  • Fix missed control transformations in lists and nested map entries

Enhancements:

  • Recursively process all map entries and list elements instead of only top-level structures

Refactored _transformIfControl to recursively process nested Maps and Lists, ensuring all control objects are properly transformed. This enhances support for complex data structures containing controls.
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine

@FeodorFitsner FeodorFitsner merged commit 95f1172 into main Oct 28, 2025
29 of 43 checks passed
@FeodorFitsner FeodorFitsner deleted the v1-reactive-fixes branch October 28, 2025 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Under certain circumstances, the interface will not be updated

1 participant