-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (73 loc) · 2.54 KB
/
main.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import flet as ft
class MessageContainer(ft.Container):
def __init__(self, username, message):
super().__init__()
self.username = username
self.message = message
self.bgcolor = ft.colors.PRIMARY_CONTAINER
self.border_radius = 10
self.padding = 10
self.content = ft.Column([
ft.Row(
controls=[
ft.Row([
ft.CircleAvatar(content=ft.Text(self.username[0]), bgcolor=ft.colors.random_color()),
ft.Text(self.username, theme_style=ft.TextThemeStyle.TITLE_MEDIUM)
]),
ft.PopupMenuButton(
items=[
ft.PopupMenuItem(
icon=ft.icons.STAR_BORDER,
text="Add to favorites"
)
]
)
],
alignment=ft.MainAxisAlignment.SPACE_BETWEEN
),
ft.Text(self.message)
])
def main(page: ft.Page):
def on_recieve_post(message):
page.add(message)
def makenewpost(e):
page.pubsub.send_all(MessageContainer(username=usernametextfield.value, message=posttextfield.value))
page.close(newpostdialog)
def chooseusername(e):
if usernametextfield.value == "":
usernametextfield.error_text = "Please enter your username"
page.update()
else:
page.close(usernamemodal)
def openpostdialog(e):
posttextfield.value = ""
page.open(newpostdialog)
posttextfield = ft.TextField(
hint_text="What do you want to post?",
multiline=True
)
newpostdialog = ft.AlertDialog(
modal=True,
title=ft.Text("New post"),
content=posttextfield,
actions=[
ft.TextButton("No", on_click=lambda e: page.close(newpostdialog)),
ft.TextButton("Yes", on_click=makenewpost)
]
)
usernametextfield = ft.TextField(
hint_text="Your username",
)
usernamemodal = ft.AlertDialog(
modal=True,
title=ft.Text("Username"),
content=usernametextfield,
actions=[
ft.TextButton("OK", on_click=chooseusername)
]
)
page.pubsub.subscribe(on_recieve_post)
page.floating_action_button = ft.FloatingActionButton(icon=ft.icons.ADD, on_click=openpostdialog)
page.update()
page.open(usernamemodal)
ft.app(target=main, view=ft.AppView.WEB_BROWSER)