Skip to content

Upgrading from 0.7.4 to another upper version is crashing my GUI: Navigation/pages #2050

@tobsome

Description

@tobsome

Description
When I update my flet version from 0.7.4 to a higher version my pages are / navigation on the left side missing. Did you issues like that? Do you know how I could fix it? :)

Code example to reproduce the issue:

# main.py
import flet
import webbrowser
from flet import (
    AppBar,
    Column,
    Container,
    Row,
    Page,
    Text,
    Card,
    NavigationRail,
    NavigationRailDestination,
    icons,
    PopupMenuButton,
    PopupMenuItem,
    UserControl
)
from modules.welcome_sheet_creator.gui import WelcomeSheetCreator

class DesktopAppLayout(Row):
    """
    Core GUI layout
    """

    def __init__(
        self,
        title,
        page,
        pages,
        *args,
        window_size=(800, 600),
        **kwargs,
    ):
        super().__init__(*args, **kwargs)

        self.page = page
        self.pages = pages

        self.expand = True

        self.navigation_items = [navigation_item for navigation_item, _ in pages]
        self.navigation_rail = self.build_navigation_rail()
        self.update_destinations()
        self._menu_extended = True
        self.navigation_rail.extended = True

        self.menu_panel = Row(
            controls=[
                self.navigation_rail,
            ],
            spacing=0,
            tight=True,
        )

        page_contents = [page_content for _, page_content in pages]
        self.content_area = Column(page_contents, expand=True)

        self._was_portrait = self.is_portrait()
        self._panel_visible = self.is_landscape()

        self.set_content()

        self._change_displayed_page()

        self.page.on_resize = self.handle_resize

        self.page.appbar = self.create_appbar()

        self.window_size = window_size
        self.page.window_width, self.page.window_height = self.window_size

        self.page.title = title

    def select_page(self, page_number):
        self.navigation_rail.selected_index = page_number
        self._change_displayed_page()

    def _navigation_change(self, e):
        self._change_displayed_page()
        self.page.update()

    def _change_displayed_page(self):
        page_number = self.navigation_rail.selected_index
        for i, content_page in enumerate(self.content_area.controls):
            content_page.visible = page_number == i

    def build_navigation_rail(self):
        return NavigationRail(
            selected_index=0,
            label_type="none",
            on_change=self._navigation_change,
        )

    def update_destinations(self):
        self.navigation_rail.destinations = self.navigation_items
        self.navigation_rail.label_type = "all"

    def handle_resize(self, e):
        pass

    def set_content(self):
        self.controls = [self.menu_panel, self.content_area]
        self.update_destinations()
        self.navigation_rail.extended = self._menu_extended
        self.menu_panel.visible = self._panel_visible

    def is_portrait(self) -> bool:
        return self.page.height >= self.page.width

    def is_landscape(self) -> bool:
        return self.page.width > self.page.height

    def create_appbar(self) -> AppBar:
        appbar = AppBar(
            title=Text("Test Tool"),
            center_title=True,
            elevation=8,
        )

        appbar.actions = [
            Row(
                [
                    PopupMenuButton(
                        icon=icons.HELP,
                        items=[
                            PopupMenuItem(
                                icon=icons.CONTACT_SUPPORT,
                                text="Documentation",
                                on_click=lambda e: webbrowser.open_new_tab(
                                    "https://google.com"
                                ),
                            ),
                            PopupMenuItem(
                                icon=icons.BUG_REPORT,
                                text="Report a bug",
                                on_click=lambda e: webbrowser.open_new_tab(
                                    "https://google.com"
                                ),
                            ),
                        ],
                    ),
                    PopupMenuButton(
                        icon=icons.PERSON,
                        items=[
                            PopupMenuItem(
                                icon=icons.LOGIN,
                                text="Login",
                            ),
                            PopupMenuItem(
                                icon=icons.SETTINGS,
                                text="Settings",
                            ),
                            PopupMenuItem(
                                icon=icons.LOGOUT,
                                text="Logout",
                            ),
                        ],
                    ),
                ]
            )
        ]
        return appbar

def create_page(title: str, body: str):
    return Row(
        controls=[
            Column(
                horizontal_alignment="stretch",
                controls=[
                    Card(
                        content=Container(Text(title, weight="bold"), padding=8),
                    ),
                    Text(body),
                ],
                expand=True,
            ),
        ],
        expand=True,
    )

def main(page: Page):
    theme_mode = "light"  # Assuming you have a theme_mode variable in settings

    page.theme_mode = theme_mode

    pages = [
        (
            NavigationRailDestination(
                icon=icons.INFO,
                selected_icon=icons.INFO,
                label="About this tool",
            ),
            create_page(
                "About this tool",
                "Nunc elementum lol elit id arcu sagittis, id molestie libero efficitur. Aliquam eget erat posuere, tempor enim sed, varius nibh. Suspendisse potenti. Sed interdum nunc rhoncus justo gravida, ac sodales mi malesuada. ",
            ),
        ),
        (
            NavigationRailDestination(
                icon=icons.EDIT_DOCUMENT,
                selected_icon=icons.EDIT_DOCUMENT,
                label="Welcome Sheet Creator",
            ),
            WelcomeSheetCreator(),
        ),
        (
            NavigationRailDestination(
                icon=icons.SUPERVISED_USER_CIRCLE,
                selected_icon=icons.SUPERVISED_USER_CIRCLE,
                label="User Onboarding",
            ),
            create_page(
                "User Onboarding",
                "Morbi ac ipsum felis. In hac habitasse platea dictumst. Nunc euismod posuere suscipit.",
            ),
        ),
    ]

    menu_layout = DesktopAppLayout(
        page=page,
        pages=pages,
        title="Test Tool",
        window_size=(1280, 720),
    )

    page.add(menu_layout)

if __name__ == "__main__":
    flet.app(main, assets_dir="asset", port=8081)

Working version 0.7.4 and below:
Bildschirmfoto 2023-11-08 um 22 23 06

Broken Version 0.8.0 and all upper:
Bildschirmfoto 2023-11-08 um 22 23 24

Flet version (pip show flet):

Working version: 0.7.4 and below
Broken version: 0.8.0 and all upper

Operating system:
macOS 14

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions