58 lines
2.5 KiB
Python
58 lines
2.5 KiB
Python
import flet as ft
|
|
|
|
def adaptive_menu(page: ft.Page):
|
|
is_mobile = page.width < 600
|
|
|
|
menu_keys = ['articles', 'courses', 'my_courses', 'progress', 'profile', 'logout']
|
|
|
|
async def handle_change(e):
|
|
index = int(e.control.selected_index)
|
|
key = menu_keys[index]
|
|
|
|
if key == "logout":
|
|
from app.state import state
|
|
state.access_token = None
|
|
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)
|
|
|
|
if is_mobile:
|
|
return ft.NavigationBar(
|
|
destinations=[
|
|
ft.NavigationBarDestination(icon=ft.Icons.ARTICLE, label='บทความ'),
|
|
ft.NavigationBarDestination(icon=ft.Icons.SCHOOL, label='หลักสูตร'),
|
|
ft.NavigationBarDestination(icon=ft.Icons.BOOK, label='คอร์สของฉัน'),
|
|
ft.NavigationBarDestination(icon=ft.Icons.TRENDING_UP, label='ความคืบหน้า'),
|
|
ft.NavigationBarDestination(icon=ft.Icons.PERSON, label='โปรไฟล์'),
|
|
ft.NavigationBarDestination(icon=ft.Icons.LOGOUT, label="ออกจากระบบ"
|
|
),
|
|
],
|
|
selected_index=current_selected_index,
|
|
on_change=handle_change,
|
|
)
|
|
else:
|
|
return ft.NavigationRail(
|
|
destinations=[
|
|
ft.NavigationRailDestination(icon=ft.Icons.ARTICLE, label='บทความ'),
|
|
ft.NavigationRailDestination(icon=ft.Icons.SCHOOL, label='หลักสูตร'),
|
|
ft.NavigationRailDestination(icon=ft.Icons.BOOK, label='คอร์สของฉัน'),
|
|
ft.NavigationRailDestination(icon=ft.Icons.TRENDING_UP, label='ความคืบหน้า'),
|
|
ft.NavigationRailDestination(icon=ft.Icons.PERSON, label='โปรไฟล์'),
|
|
ft.NavigationRailDestination(icon=ft.Icons.LOGOUT, label="Logout"
|
|
),
|
|
],
|
|
selected_index=current_selected_index,
|
|
on_change=handle_change,
|
|
extended=True,
|
|
) |