feat: implement adaptive layout and sync navigation state

This commit is contained in:
Flook
2026-05-07 04:59:47 +07:00
parent 2d9cbb3376
commit 5ac9799b5d
3 changed files with 57 additions and 25 deletions
+13 -17
View File
@@ -1,13 +1,19 @@
import flet as ft
def adaptive_menu(page: ft.Page):
is_mobile = page.width < 600
MENU_KEYS = ['articles', 'courses', 'my_courses', 'progress', 'profile', 'logout']
menu_keys = ['articles', 'courses', 'my_courses', 'progress', 'profile', 'logout']
def get_selected_index_from_route(route: str):
key = route.replace("/", "").split("/")[0]
if key in MENU_KEYS:
return MENU_KEYS.index(key)
return 0
def adaptive_menu(page: ft.Page, selected_index: int):
is_mobile = page.width < 600
async def handle_change(e):
index = int(e.control.selected_index)
key = menu_keys[index]
key = MENU_KEYS[index]
if key == "logout":
from app.state import state
@@ -15,17 +21,7 @@ def adaptive_menu(page: ft.Page):
await page.push_route("/login") # หรือ page.go
return
route = f"/{menu_keys[index]}"
print(f">>> Navigating to: {route}")
await page.push_route(route)
# sync selected index กับ route
current_route = page.route.replace("/", "") or "articles"
if current_route not in menu_keys:
current_route = "articles"
current_selected_index = menu_keys.index(current_route)
await page.push_route(f"/{key}")
if is_mobile:
return ft.NavigationBar(
@@ -38,7 +34,7 @@ def adaptive_menu(page: ft.Page):
ft.NavigationBarDestination(icon=ft.Icons.LOGOUT, label="ออกจากระบบ"
),
],
selected_index=current_selected_index,
selected_index=selected_index,
on_change=handle_change,
)
else:
@@ -52,7 +48,7 @@ def adaptive_menu(page: ft.Page):
ft.NavigationRailDestination(icon=ft.Icons.LOGOUT, label="Logout"
),
],
selected_index=current_selected_index,
selected_index=selected_index,
on_change=handle_change,
extended=True,
)