File size: 2,638 Bytes
21a6cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)