Skip to content

Commit 62322d3

Browse files
committed
Update todo tutorial for improved filtering logic
Refactored the filtering section in the To-Do app tutorial to clarify the use of Tabs and TabBar controls, update code examples for task creation and status change, and improve explanations for filtering tasks by status. Also fixed minor formatting and wording for better readability.
1 parent 4040468 commit 62322d3

File tree

1 file changed

+28
-35
lines changed
  • sdk/python/packages/flet/docs/tutorials

1 file changed

+28
-35
lines changed

sdk/python/packages/flet/docs/tutorials/todo.md

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Run the app and try to edit and delete tasks:
138138
We already have a functional To-Do app where we can create, edit, and delete tasks.
139139
To be even more productive, we want to be able to filter tasks by their status.
140140

141-
Copy the entire code for this step from [here](https://github.com/flet-dev/flet/blob/main/sdk/python/examples/tutorials/todo/to-do-5.py).
141+
Copy the entire code for this step from [here](https://github.com/flet-dev/flet/blob/main/sdk/python/examples/tutorials/todo/todo.py).
142142
Below we will explain the changes we've done to implement filtering.
143143

144144
`Tabs` control is used to display filter:
@@ -148,18 +148,10 @@ Below we will explain the changes we've done to implement filtering.
148148

149149
class TodoApp(ft.Column):
150150
# application's root control is a Column containing all other controls
151-
def __init__(self):
152-
super().__init__()
153-
self.new_task = ft.TextField(hint_text="What's needs to be done?", expand=True)
151+
def init(self):
152+
self.new_task = ft.TextField(hint_text="Whats needs to be done?", expand=True)
154153
self.tasks = ft.Column()
155154

156-
self.filter_tabs = ft.Tabs(
157-
length=3,
158-
selected_index=0,
159-
on_change=lambda e: self.update(),
160-
content=self.filter,
161-
)
162-
163155
self.filter = ft.TabBar(
164156
scrollable=False,
165157
tabs=[
@@ -169,16 +161,19 @@ class TodoApp(ft.Column):
169161
],
170162
)
171163

164+
self.filter_tabs = ft.Tabs(
165+
length=3,
166+
selected_index=0,
167+
on_change=lambda e: self.update(),
168+
content=self.filter,
169+
)
170+
172171
# ...
173172
```
174173

175-
To display different lists of tasks depending on their statuses, we could maintain three
176-
lists with "All", "Active" and "Completed" tasks. We, however, chose an easier approach
177-
where we maintain the same list and only change a task's visibility depending on its status.
174+
To display different lists of tasks depending on their statuses, we could maintain three lists with "All", "Active" and "Completed" tasks. We, however, chose an easier approach where we maintain the same list and only change a task's visibility depending on its status.
178175

179-
In `TodoApp` class we overrided [`before_update()`](../cookbook/custom-controls.md#before_update)
180-
method alled every time when the control is being updated. It iterates through all the tasks and updates their `visible`
181-
property depending on the status of the task:
176+
In `TodoApp` class we overrided [`before_update()`](../cookbook/custom-controls.md#before_update) method alled every time when the control is being updated. It iterates through all the tasks and updates their `visible` property depending on the status of the task:
182177

183178
```python title="todo.py"
184179
class TodoApp(ft.Column):
@@ -195,8 +190,7 @@ class TodoApp(ft.Column):
195190
)
196191
```
197192

198-
Filtering should occur when we click on a tab or change a task status. `TodoApp.before_update()` method
199-
is called when Tabs selected value is changed or Task item checkbox is clicked:
193+
Filtering should occur when we click on a tab or change a task status. `TodoApp.before_update()` method is called when Tabs selected value is changed or Task item checkbox is clicked:
200194

201195
```python title="todo.py"
202196
class TodoApp(ft.Column):
@@ -209,21 +203,24 @@ class TodoApp(ft.Column):
209203
def task_status_change(self, e):
210204
self.update()
211205

206+
212207
def add_clicked(self, e):
213-
task = Task(self.new_task.value, self.task_status_change, self.task_delete)
208+
task = Task(
209+
task_name=self.new_task.value,
210+
on_status_change=self.task_status_change,
211+
on_delete=self.task_delete,
212+
)
213+
self.tasks.controls.append(task)
214+
self.new_task.value = ""
215+
self.update()
214216
# ...
215217

216218
class Task(ft.Column):
217-
def __init__(self, task_name, task_status_change, task_delete):
218-
super().__init__()
219-
self.completed = False
220-
self.task_name = task_name
221-
self.task_status_change = task_status_change
222-
self.task_delete = task_delete
223-
self.display_task = ft.Checkbox(
224-
value=False, label=self.task_name, on_change=self.status_changed
225-
)
226-
# ...
219+
task_name: str = ""
220+
on_status_change: Callable[[], None] = field(default=lambda: None)
221+
on_delete: Callable[["Task"], None] = field(default=lambda task: None)
222+
223+
# ...
227224

228225
def status_changed(self, e):
229226
self.completed = self.display_task.value
@@ -234,11 +231,7 @@ Run the app and try filtering tasks by clicking on the tabs:
234231

235232
{{ image("../examples/tutorials/todo/media/filtering.gif", alt="filtering", width="80%") }}
236233

237-
238-
## Final touches
239-
240-
Our Todo app is almost complete now. As a final touch, we will add a footer (`Column` control)
241-
displaying the number of incomplete tasks (`Text` control) and a "Clear completed" button.
234+
Our Todo app is almost complete now. As a final touch, we will add a footer (`Column` control) displaying the number of incomplete tasks (`Text` control) and a "Clear completed" button.
242235

243236
/// details | Full code
244237
type: example

0 commit comments

Comments
 (0)