Skip to content

bug: ft.Container's size monitoring produces inaccurate parameters once assigned #6458

@Qye-Leisure

Description

@Qye-Leisure

Duplicate Check

Describe the bug

Because the internal space of the ft.InteractiveViewer component must have explicit dimensions, otherwise zooming will malfunction, I wanted to create an infinite canvas by monitoring the size and then assigning that size to the content in order to avoid zoom issues. However, once I assign the monitored size data to the container, the width becomes locked. If I do not assign it, the data returns to normal. What exactly is going on? Of course, I could simply set an initial value of 2000×2000 pixels, which would also achieve the effect I want, but I am still curious about the reason behind this problem.

Code sample

Code
import flet as ft
from typing import Optional, cast
from dataclasses import field


@ft.control
class 画布(ft.Container):
    expand:bool | int | None=True
    bgcolor: ft.Colors | str = ft.Colors.BLUE_100
    #尺寸变化采样间隔
    size_change_interval: int = 16

    内容列表: list[ft.Control] = field(default_factory=list)
    
    
    """用于放置画布中的内容"""

    def init(self):
        # self.on_size_change=lambda e:self.实时获取尺寸(e)
        self.内容画布层=self.内容画布()
        self.交互层= ft.InteractiveViewer(
            # expand=True,
            content= self.内容画布层
            )
        self.content = self.交互层
        

    def 内容画布(self):

        return ft.Container(
                on_size_change=lambda e:self.实时获取尺寸(e),
                bgcolor = ft.Colors.AMBER,
                content= ft.Stack(                
                    expand=True, 
                    controls=[self.模块容器(i) for i in range(10)] 
                )
            )
        
    def 实时获取尺寸(self, e):

        print(e.width, e.height)
        # self.交互层.expand=True
        # self.内容画布层.width=e.width
        # self.内容画布层.height=e.height

        # e.control.width=e.width
        # e.control.height=e.height

        e.control.data=(e.width, e.height)
        e.control.height=e.control.data[1]
        e.control.width=e.control.data[0]


        

        e.control.update()


    def 模块容器(self, card_id):
        模块 = ft.Container(
            bgcolor=ft.Colors.GREEN, 
            width=70,

            content=ft.Column([
                ft.Container(width=70, content=ft.Text(f"项目 {j}"))
                for j in range(10) 
            ])
        )

        return ft.GestureDetector(
            mouse_cursor=ft.MouseCursor.MOVE,
            drag_interval=5,
            # on_pan_update=self.模块拖拽,
            left=card_id * 15,  
            top=card_id * 15,  
            content=模块
        )



if __name__ == "__main__":
    def main(page: ft.Page):
        page.add(
            画布()
        )
    ft.run(main)

To reproduce

直接运行

Expected behavior

No response

Screenshots / Videos

Captures

[Upload media here]

Operating System

Windows

Operating system details

win11

Flet version

0.84

Regression

No, it isn't

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions