Hw83jd commited on
Commit
9e17a63
·
verified ·
1 Parent(s): 7dca9da

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +69 -0
main.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import flet as ft
2
+
3
+ class Message:
4
+ def __init__(self, user: str, text: str, type: str):
5
+ self.user = user
6
+ self.text = text
7
+ self.type = type
8
+
9
+ def main(page: ft.Page):
10
+ page.title = "دردشة تقنية"
11
+ page.rtl = True
12
+ page.theme_mode = ft.ThemeMode.DARK
13
+ page.bgcolor = "#111b21" # لون خلفية واتساب غامق
14
+
15
+ chat_list = ft.Column(expand=True, scroll=ft.ScrollMode.ALWAYS, spacing=10)
16
+
17
+ def on_message(msg: Message):
18
+ if msg.type == "chat":
19
+ alignment = ft.MainAxisAlignment.START
20
+ color = "#202c33"
21
+ if msg.user == page.session.get("username"):
22
+ alignment = ft.MainAxisAlignment.END
23
+ color = "#005c4b"
24
+
25
+ chat_list.controls.append(
26
+ ft.Row(
27
+ [ft.Container(
28
+ content=ft.Text(f"{msg.user}: {msg.text}", color="white"),
29
+ padding=10, border_radius=10, bgcolor=color
30
+ )],
31
+ alignment=alignment
32
+ )
33
+ )
34
+ else:
35
+ chat_list.controls.append(ft.Text(msg.text, italic=True, color="grey", size=12, text_align="center"))
36
+ page.update()
37
+
38
+ page.pubsub.subscribe(on_message)
39
+ msg_input = ft.TextField(hint_text="اكتب رسالة...", expand=True, border_radius=20)
40
+
41
+ def send_click(e):
42
+ if msg_input.value:
43
+ page.pubsub.send_all(Message(page.session.get("username"), msg_input.value, "chat"))
44
+ msg_input.value = ""
45
+ page.update()
46
+
47
+ def join_click(e):
48
+ if name_input.value:
49
+ page.session.set("username", name_input.value)
50
+ page.dialog.open = False
51
+ page.pubsub.send_all(Message(name_input.value, f"⚡ {name_input.value} دخل الدردشة الآن", "info"))
52
+ page.add(
53
+ ft.AppBar(title=ft.Text("دردشة تقنية"), bgcolor="#202c33"),
54
+ ft.Container(content=chat_list, expand=True, padding=20),
55
+ ft.Row([msg_input, ft.FloatingActionButton(icon=ft.icons.SEND, on_click=send_click)], padding=10)
56
+ )
57
+ page.update()
58
+
59
+ name_input = ft.TextField(label="اكتب اسمك المستعار للدخول", border_radius=10)
60
+ page.dialog = ft.AlertDialog(
61
+ title=ft.Text("مرحباً بك!"),
62
+ content=name_input,
63
+ actions=[ft.ElevatedButton("دخول", on_click=join_click)],
64
+ modal=True
65
+ )
66
+ page.dialog.open = True
67
+ page.update()
68
+
69
+ ft.app(target=main, view=ft.AppView.WEB_BROWSER, host="0.0.0.0", port=7860)