54 lines
2.3 KiB
Python
54 lines
2.3 KiB
Python
import flet as ft
|
|
|
|
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]
|
|
|
|
if key == "logout":
|
|
from app.state import state
|
|
state.access_token = None
|
|
await page.push_route("/login") # หรือ page.go
|
|
return
|
|
|
|
await page.push_route(f"/{key}")
|
|
|
|
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=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=selected_index,
|
|
on_change=handle_change,
|
|
extended=True,
|
|
) |