-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchat_message.py
50 lines (44 loc) · 1.48 KB
/
chat_message.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
import flet as ft
class Message():
def __init__(self,user:str,text:str,message_type:str):
self.user=user
self.text=text
self.message_type=message_type
class ChatMessage(ft.Row):
def __init__(self, message: Message):
super().__init__()
self.vertical_alignment="start"
self.controls=[
ft.CircleAvatar(
content=ft.Text(self.get_initials(message.user)),
color=ft.colors.WHITE,
bgcolor=self.get_avatar_color(message.user),
),
ft.Column(
[
ft.Text(message.user, weight="bold"),
ft.Text(message.text, selectable=True),
],
tight=True,
spacing=5,
),
]
def get_initials(self, user: str):
return user[:1].capitalize()
def get_avatar_color(self, user: str):
colors_lookup = [
ft.colors.AMBER,
ft.colors.BLUE,
ft.colors.BROWN,
ft.colors.CYAN,
ft.colors.GREEN,
ft.colors.INDIGO,
ft.colors.LIME,
ft.colors.ORANGE,
ft.colors.PINK,
ft.colors.PURPLE,
ft.colors.RED,
ft.colors.TEAL,
ft.colors.YELLOW,
]
return colors_lookup[hash(user) % len(colors_lookup)]