| import flet as ft |
|
|
| class Message: |
| def __init__(self, user: str, text: str, type: str): |
| self.user = user |
| self.text = text |
| self.type = type |
|
|
| def main(page: ft.Page): |
| page.title = "دردشة تقنية" |
| page.rtl = True |
| page.theme_mode = ft.ThemeMode.DARK |
| page.bgcolor = "#111b21" |
| |
| chat_list = ft.Column(expand=True, scroll=ft.ScrollMode.ALWAYS, spacing=10) |
| |
| def on_message(msg: Message): |
| if msg.type == "chat": |
| alignment = ft.MainAxisAlignment.START |
| color = "#202c33" |
| if msg.user == page.session.get("username"): |
| alignment = ft.MainAxisAlignment.END |
| color = "#005c4b" |
| |
| chat_list.controls.append( |
| ft.Row( |
| [ft.Container( |
| content=ft.Text(f"{msg.user}: {msg.text}", color="white"), |
| padding=10, border_radius=10, bgcolor=color |
| )], |
| alignment=alignment |
| ) |
| ) |
| else: |
| chat_list.controls.append(ft.Text(msg.text, italic=True, color="grey", size=12, text_align="center")) |
| page.update() |
|
|
| page.pubsub.subscribe(on_message) |
| msg_input = ft.TextField(hint_text="اكتب رسالة...", expand=True, border_radius=20) |
|
|
| def send_click(e): |
| if msg_input.value: |
| page.pubsub.send_all(Message(page.session.get("username"), msg_input.value, "chat")) |
| msg_input.value = "" |
| page.update() |
|
|
| def join_click(e): |
| if name_input.value: |
| page.session.set("username", name_input.value) |
| page.dialog.open = False |
| page.pubsub.send_all(Message(name_input.value, f"⚡ {name_input.value} دخل الدردشة الآن", "info")) |
| page.add( |
| ft.AppBar(title=ft.Text("دردشة تقنية"), bgcolor="#202c33"), |
| ft.Container(content=chat_list, expand=True, padding=20), |
| ft.Row([msg_input, ft.FloatingActionButton(icon=ft.icons.SEND, on_click=send_click)], padding=10) |
| ) |
| page.update() |
|
|
| name_input = ft.TextField(label="اكتب اسمك المستعار للدخول", border_radius=10) |
| page.dialog = ft.AlertDialog( |
| title=ft.Text("مرحباً بك!"), |
| content=name_input, |
| actions=[ft.ElevatedButton("دخول", on_click=join_click)], |
| modal=True |
| ) |
| page.dialog.open = True |
| page.update() |
|
|
| ft.app(target=main, view=ft.AppView.WEB_BROWSER, host="0.0.0.0", port=7860) |
|
|