flet icon indicating copy to clipboard operation
flet copied to clipboard

Tabs Control doesn't appear it's content

Open Youssef3082004 opened this issue 1 year ago • 2 comments

Duplicate Check

  • [x] I have searched the opened issues and there are no duplicates

Describe the bug

  • Each tab in tabs should have its content, but the result doesn't appear, it just Text Control appears
  • Any Control like Container, Row, and Column doesn't appear

Code sample

Code
from flet import * 
from Constant.variables import Variables
from Constant.Controls import Common_Appbar 



class Calculator_interface(Column):
    def __init__(self,page:Page):
        super().__init__()
        self.page = page
        self.page.padding = 0
        self.page.window.height = 700
        self.page.window.width = 400
        self.page.scroll = ScrollMode.AUTO
        self.page.window.resizable = False
        self.page.bgcolor = Variables.PAGES_BACKGROUND_COLOR
        self.page.fonts = {"andalus": "font/andalus.ttf", "Alex": "font/Alex.ttf" , "pac":"font/Pacifico-Regular.ttf","Inter":"font/Inter.ttf"}
        self.page.theme = Theme(page_transitions=PageTransitionsTheme(windows=PageTransitionTheme.OPEN_UPWARDS,android=PageTransitionTheme.OPEN_UPWARDS))
        self.page.theme_mode = ThemeMode.LIGHT
        self.page.window.center()
        self.page.appbar = Common_Appbar("Calcultor")

        #! ==================================================== Application Logo Row ==============================================

        self.Calculations = ["Body Mass Index - BMI","Basal Metabolic Rate - BMR","Calaoreis","Macros","Target Heart Rate - THR","Training Volume","Progress","Water"]
        # self.row = Row(controls=[],alignment=MainAxisAlignment.CENTER,scroll=ScrollMode.AUTO)

        self.t = Tabs(
        indicator_padding=0,
        divider_height=0,
        selected_index=0,
        animation_duration=300,
        scrollable=True,
        padding=0,
        tabs=[Tab(
                text="Tab 1",
                content=Container(content=Row([Text("This is Tab 1")],alignment=MainAxisAlignment.CENTER), alignment=alignment.center)
            ),
            Tab(text="Tab 2",
                content=Text("This is Tab 2")
            ),
            Tab(
                text="Tab3",
                content=Row([Container(content=(Text("This is Tab 3",color=Colors.BLACK,size=100)), alignment=alignment.center)],alignment=MainAxisAlignment.CENTER)
            ),
        ])

        self.controls = [self.t]
    

if __name__ == "__main__":
    def main(page:Page):

        page.add(Calculator_interface(page))
    app(target=main)

To reproduce

  1. Run the Program
  2. The tabs text will appear normall,y but each tab content doesn't appear

Expected behavior

No response

Screenshots / Videos

Image Image

Operating System

Windows

Operating system details

Windows 10 Pro

Flet version

Flet 0.28.3

Regression

No, it isn't

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

Youssef3082004 avatar Jul 04 '25 21:07 Youssef3082004

The Core Problem: A Layout Negotiation Failure

In Flet, layout is a negotiation. A parent container tells its children how much space is available, and the children report back how much space they need. The original code failed because this negotiation broke down in a cascading failure.

There were two distinct but related issues at play:

  1. The Cascade of Shrinking: Neither the parent Column nor the child Tabs control was told to expand, so they both tried to be as small as possible.
  2. The Initialization Race Condition: The component was trying to configure the main Page at the same time it was being built, creating an unstable layout environment.

So the solution is either set an update 1st to the page to measure it's dimensions and this is not a best practice or set the config of the page level at the main func, as flet is based on single page layout and in case of creating a multi pages, should rely then on views and routers.

See example below of forcing the update of page layout before building the control.

from flet import * 

class Variables:
    APPBAR_BACKGROUND_COLOR = "#F5F5F5"
    PAGES_BACKGROUND_COLOR = "#BAFBE7"

class Calculator_interface(Column):
    def __init__(self,page:Page):
        super().__init__()

        # --- Step 1: The Column must still be expandable to fill the page ---
        self.expand = True
        self.page = page

        # --- Step 2: Configure all page-level properties ---
        self.page.padding = 0
        self.page.window.height = 700
        self.page.window.width = 400
        self.page.window.resizable = False
        self.page.bgcolor = Variables.PAGES_BACKGROUND_COLOR
        self.page.theme_mode = ThemeMode.LIGHT
        self.page.window.center()
        self.page.appbar = AppBar(
            leading=IconButton(icon=Icons.MENU),
            title=Text("Calculators",size=20,weight="bold"),
            center_title=True,
            bgcolor=Variables.APPBAR_BACKGROUND_COLOR,
            actions=[
                IconButton(icon=Icons.SEARCH),
                IconButton(icon=Icons.NOTIFICATIONS),
            ],
        )

        # --- Step 3: Force the page to update and render its new layout NOW ---
        # This commits the AppBar and other settings, establishing the content area's height.
        self.page.update()

        # --- Step 4: NOW that the page layout is stable, build the Tabs control ---
        # It will be created within a context where its parent (the Column)
        # and grandparent (the Page) have a known, fixed height.
        self.tabs_control = Tabs(
            expand=True, # This is still essential
            indicator_padding=0,
            divider_height=0,
            selected_index=0,
            animation_duration=300,
            scrollable=True,
            padding=0,
            tabs=[
                Tab(
                    text="Tab 1",
                    content=Row([Container(content=Text("This is Tab 1"), alignment=alignment.center)])
                ),
                Tab(
                    text="Tab 2",
                    content=Column([Container(content=Text("This is Tab 2"), alignment=alignment.center)])
                ),
                Tab(
                    text="Tab 3",
                    content=Container(content=Text("This is Tab 3", color=Colors.BLACK, size=30), alignment=alignment.center)
                ),
            ]
        )

        # Add the newly created tabs control to this Column's controls
        self.controls = [self.tabs_control]
    

if __name__ == "__main__":
    def main(page:Page):
        page.add(Calculator_interface(page))
    app(target=main)

MuhammedMomen avatar Jul 05 '25 22:07 MuhammedMomen

The Core Problem: A Layout Negotiation Failure

In Flet, layout is a negotiation. A parent container tells its children how much space is available, and the children report back how much space they need. The original code failed because this negotiation broke down in a cascading failure.

There were two distinct but related issues at play:

  1. The Cascade of Shrinking: Neither the parent Column nor the child Tabs control was told to expand, so they both tried to be as small as possible.
  2. The Initialization Race Condition: The component was trying to configure the main Page at the same time it was being built, creating an unstable layout environment.

So the solution is either set an update 1st to the page to measure it's dimensions and this is not a best practice or set the config of the page level at the main func, as flet is based on single page layout and in case of creating a multi pages, should rely then on views and routers.

See example below of forcing the update of page layout before building the control.

from flet import * 

class Variables:
    APPBAR_BACKGROUND_COLOR = "#F5F5F5"
    PAGES_BACKGROUND_COLOR = "#BAFBE7"

class Calculator_interface(Column):
    def __init__(self,page:Page):
        super().__init__()

        # --- Step 1: The Column must still be expandable to fill the page ---
        self.expand = True
        self.page = page

        # --- Step 2: Configure all page-level properties ---
        self.page.padding = 0
        self.page.window.height = 700
        self.page.window.width = 400
        self.page.window.resizable = False
        self.page.bgcolor = Variables.PAGES_BACKGROUND_COLOR
        self.page.theme_mode = ThemeMode.LIGHT
        self.page.window.center()
        self.page.appbar = AppBar(
            leading=IconButton(icon=Icons.MENU),
            title=Text("Calculators",size=20,weight="bold"),
            center_title=True,
            bgcolor=Variables.APPBAR_BACKGROUND_COLOR,
            actions=[
                IconButton(icon=Icons.SEARCH),
                IconButton(icon=Icons.NOTIFICATIONS),
            ],
        )

        # --- Step 3: Force the page to update and render its new layout NOW ---
        # This commits the AppBar and other settings, establishing the content area's height.
        self.page.update()

        # --- Step 4: NOW that the page layout is stable, build the Tabs control ---
        # It will be created within a context where its parent (the Column)
        # and grandparent (the Page) have a known, fixed height.
        self.tabs_control = Tabs(
            expand=True, # This is still essential
            indicator_padding=0,
            divider_height=0,
            selected_index=0,
            animation_duration=300,
            scrollable=True,
            padding=0,
            tabs=[
                Tab(
                    text="Tab 1",
                    content=Row([Container(content=Text("This is Tab 1"), alignment=alignment.center)])
                ),
                Tab(
                    text="Tab 2",
                    content=Column([Container(content=Text("This is Tab 2"), alignment=alignment.center)])
                ),
                Tab(
                    text="Tab 3",
                    content=Container(content=Text("This is Tab 3", color=Colors.BLACK, size=30), alignment=alignment.center)
                ),
            ]
        )

        # Add the newly created tabs control to this Column's controls
        self.controls = [self.tabs_control]
    

if __name__ == "__main__":
    def main(page:Page):
        page.add(Calculator_interface(page))
    app(target=main)

Now I know the mistake, so Thank You Very Much. I appreciate your solution, and it's done

Youssef3082004 avatar Jul 06 '25 22:07 Youssef3082004