-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
245 lines (217 loc) · 7.31 KB
/
main2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import flet
from flet import (
Checkbox,
Column,
FloatingActionButton,
IconButton,
OutlinedButton,
OutlinedBorder,
Page,
Row,
Tab,
Tabs,
Text,
TextField,
UserControl,
colors,
icons,
CircleBorder,
PopupMenuButton,
PopupMenuItem,
Container,
Icon,
margin
)
class Task(UserControl):
def __init__(self, task_name, task_status_change, task_delete):
super().__init__()
self.completed = False
self.task_name = task_name
self.task_status_change = task_status_change
self.task_delete = task_delete
def build(self):
self.display_task = Checkbox(
value=False, label=self.task_name, on_change=self.status_changed
)
self.edit_name = TextField(expand=1)
self.display_view = Row(
alignment="spaceBetween",
vertical_alignment="center",
controls=[
self.display_task,
Row(
spacing=0,
controls=[
IconButton(
icon=icons.CREATE_OUTLINED,
tooltip="Edit To-Do",
on_click=self.edit_clicked,
),
IconButton(
icons.DELETE_OUTLINE,
tooltip="Delete To-Do",
on_click=self.delete_clicked,
),
],
),
],
)
self.edit_view = Row(
visible=False,
alignment="spaceBetween",
vertical_alignment="center",
controls=[
self.edit_name,
IconButton(
icon=icons.DONE_OUTLINE_OUTLINED,
icon_color=colors.GREEN,
tooltip="Update To-Do",
on_click=self.save_clicked,
),
],
)
return Column(controls=[self.display_view, self.edit_view])
def edit_clicked(self, e):
self.edit_name.value = self.display_task.label
self.display_view.visible = False
self.edit_view.visible = True
self.update()
def save_clicked(self, e):
self.display_task.label = self.edit_name.value
self.display_view.visible = True
self.edit_view.visible = False
self.update()
def status_changed(self, e):
self.completed = self.display_task.value
self.task_status_change(self)
def delete_clicked(self, e):
self.task_delete(self)
class TodoApp(UserControl):
def __init__(self, page: Page):
self.page = page
self.appbar_items = [
PopupMenuItem(text="Login"),
PopupMenuItem(), # divider
PopupMenuItem(text="Settings")
]
self.appbar = TodoApp(
#leading=Icon(icons.GRID_GOLDENRATIO_ROUNDED),
#leading_width=100,
title=Text("Trolli",size=32, text_align="start"),
center_title=False,
toolbar_height=75,
bgcolor=colors.LIGHT_BLUE_ACCENT_700,
actions=[
Container(
content=PopupMenuButton(
items=self.appbar_items
),
margin=margin.only(left=50, right=25)
)
],
)
self.page.appbar = self.appbar
self.page.update()
def build(self):
self.new_task = TextField(
color= colors.GREEN_300,
bgcolor= colors.WHITE24,
hint_text="What needs to be done 123?",
on_submit=self.add_clicked,
expand=True)
self.tasks = Column()
self.filter = Tabs(
selected_index=0,
on_change=self.tabs_changed,
tabs=[Tab(text="all"), Tab(text="active"), Tab(text="completed")],
)
self.items_left = Text("0 items left")
# application's root control (i.e. "view") containing all other controls
return Column(
width=600,
controls=[
Row([Text(value="Todos", style="headlineMedium")], alignment="center"),
Row(
controls=[
self.new_task,
FloatingActionButton(icon=icons.ADD, on_click=self.add_clicked),
],
),
Row(
controls=[
self.new_task,
FloatingActionButton(icon=icons.BARCODE_READER, shape=CircleBorder(), bgcolor=colors.CYAN_400, on_click=self.add_clicked),
],
),
Row(
controls=[
self.new_task,
FloatingActionButton(icon=icons.BAR_CHART, bgcolor=colors.WHITE24, on_click=self.add_clicked),
],
),
Row(
controls=[
self.new_task,
FloatingActionButton(icon=icons.ACCOUNT_BALANCE_SHARP, bgcolor=colors.WHITE24, shape=CircleBorder(), mini=True, on_click=self.add_clicked),
],
),
Column(
spacing=25,
controls=[
self.filter,
self.tasks,
Row(
alignment="spaceBetween",
vertical_alignment="center",
controls=[
self.items_left,
OutlinedButton(
text="Clear completed", on_click=self.clear_clicked
),
],
),
],
),
],
)
def add_clicked(self, e):
if self.new_task.value:
task = Task(self.new_task.value, self.task_status_change, self.task_delete)
self.tasks.controls.append(task)
self.new_task.value = ""
self.new_task.focus()
self.update()
def task_status_change(self, task):
self.update()
def task_delete(self, task):
self.tasks.controls.remove(task)
self.update()
def tabs_changed(self, e):
self.update()
def clear_clicked(self, e):
for task in self.tasks.controls[:]:
if task.completed:
self.task_delete(task)
def update(self):
status = self.filter.tabs[self.filter.selected_index].text
count = 0
for task in self.tasks.controls:
task.visible = (
status == "all"
or (status == "active" and task.completed == False)
or (status == "completed" and task.completed)
)
if not task.completed:
count += 1
self.items_left.value = f"{count} active item(s) left"
super().update()
def main(page: Page):
page.title = "ToDo App"
page.horizontal_alignment = "center"
page.scroll = "adaptive"
page.update()
# create application instance
app = TodoApp(page)
# add application's root control to the page
page.add(app)
flet.app(target=main)