Skip to content

Commit 568ac6f

Browse files
Add SnackBar action examples and improve action handling (#5813)
Introduces a new Python example demonstrating SnackBar actions, updates documentation with new examples and images, and enhances integration tests for SnackBar actions. The Dart SnackBar control is updated to better distinguish between string and control-based actions. Minor doc corrections and test refactoring are also included.
1 parent c83a595 commit 568ac6f

File tree

13 files changed

+133
-19
lines changed

13 files changed

+133
-19
lines changed

packages/flet/lib/src/controls/snack_bar.dart

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class _SnackBarControlState extends State<SnackBarControl> {
4646
"SnackBar.content must be provided and visible");
4747
}
4848

49-
final actionControl = widget.control.child("action");
49+
final actionControl = widget.control.get("action");
5050
SnackBarAction? action;
51-
if (actionControl != null) {
51+
if (actionControl is Control) {
5252
action = SnackBarAction(
5353
label: actionControl.getString("label", "Action")!,
5454
backgroundColor: actionControl.getColor("bgcolor", context),
@@ -59,14 +59,11 @@ class _SnackBarControlState extends State<SnackBarControl> {
5959
actionControl.getColor("disabled_text_color", context),
6060
onPressed: () => actionControl.triggerEvent("click"),
6161
);
62-
} else {
63-
var label = widget.control.getString("action");
64-
action = label != null
65-
? SnackBarAction(
66-
label: label,
67-
onPressed: () {},
68-
)
69-
: null;
62+
} else if (actionControl is String) {
63+
action = SnackBarAction(
64+
label: actionControl,
65+
onPressed: () => widget.control.triggerEvent("action"),
66+
);
7067
}
7168

7269
var width = widget.control.getDouble("width");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import flet as ft
2+
3+
4+
def main(page: ft.Page):
5+
def open_simple_action(e: ft.Event[ft.Button]):
6+
page.show_dialog(
7+
ft.SnackBar(
8+
ft.Text("The file has been deleted."),
9+
action="Undo",
10+
on_action=lambda e: print("Simple Undo clicked"),
11+
)
12+
)
13+
14+
def open_custom_action(e: ft.Event[ft.Button]):
15+
page.show_dialog(
16+
ft.SnackBar(
17+
ft.Text("The directory has been deleted."),
18+
action=ft.SnackBarAction(
19+
label="Undo delete",
20+
text_color=ft.Colors.YELLOW,
21+
bgcolor=ft.Colors.BLUE,
22+
on_click=lambda e: print("Custom Undo clicked"),
23+
),
24+
)
25+
)
26+
27+
page.add(
28+
ft.Button("Open SnackBar with a Simple action", on_click=open_simple_action),
29+
ft.Button("Open SnackBar with a Custom action", on_click=open_custom_action),
30+
)
31+
32+
33+
if __name__ == "__main__":
34+
ft.run(main)

sdk/python/packages/flet/docs/controls/snackbar.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ snack_bar_action_class_name: flet.SnackBarAction
2828

2929
{{ image(example_images + "/snack_bar_flow.gif", alt="Snack bar with counter", caption="Snack bar with counter",width="50%") }}
3030

31+
### Action
32+
33+
```python
34+
--8<-- "{{ examples }}/action.py"
35+
```
36+
37+
{{ image(example_images + "/action_simple.png", alt="Snack bar with a simple action", caption="Snack bar with a simple action", width="50%") }}
38+
39+
{{ image(example_images + "/action_custom.png", alt="Snack bar with a custom action", caption="Snack bar with a custom action", width="50%") }}
3140

3241
{{ class_members(class_name) }}
3342

7.45 KB
Loading
12 KB
Loading
10.5 KB
Loading

sdk/python/packages/flet/integration_tests/controls/material/test_snack_bar.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,56 @@
55

66

77
@pytest.mark.asyncio(loop_scope="module")
8-
async def test_snack_bar_basic(flet_app: ftt.FletTestApp, request):
8+
async def test_basic(flet_app: ftt.FletTestApp, request):
99
flet_app.page.enable_screenshots = True
10-
flet_app.resize_page(400, 600)
10+
flet_app.resize_page(500, 300)
1111
flet_app.page.show_dialog(ft.SnackBar(ft.Text("Hello, world!")))
1212
flet_app.page.update()
1313
await flet_app.tester.pump_and_settle()
1414

1515
flet_app.assert_screenshot(
16-
"snack_bar_basic",
16+
request.node.name,
17+
await flet_app.page.take_screenshot(
18+
pixel_ratio=flet_app.screenshots_pixel_ratio
19+
),
20+
)
21+
22+
23+
@pytest.mark.asyncio(loop_scope="module")
24+
async def test_simple_action(flet_app: ftt.FletTestApp, request):
25+
flet_app.page.enable_screenshots = True
26+
flet_app.resize_page(500, 300)
27+
flet_app.page.show_dialog(ft.SnackBar(ft.Text("Directory deleted."), action="Undo"))
28+
flet_app.page.update()
29+
await flet_app.tester.pump_and_settle()
30+
31+
flet_app.assert_screenshot(
32+
request.node.name,
33+
await flet_app.page.take_screenshot(
34+
pixel_ratio=flet_app.screenshots_pixel_ratio
35+
),
36+
)
37+
38+
39+
@pytest.mark.asyncio(loop_scope="module")
40+
async def test_custom_action(flet_app: ftt.FletTestApp, request):
41+
flet_app.page.enable_screenshots = True
42+
flet_app.resize_page(500, 300)
43+
flet_app.page.show_dialog(
44+
ft.SnackBar(
45+
ft.Text("File deleted."),
46+
action=ft.SnackBarAction(
47+
label="Undo delete",
48+
text_color=ft.Colors.YELLOW,
49+
bgcolor=ft.Colors.BLUE,
50+
),
51+
)
52+
)
53+
flet_app.page.update()
54+
await flet_app.tester.pump_and_settle()
55+
56+
flet_app.assert_screenshot(
57+
request.node.name,
1758
await flet_app.page.take_screenshot(
1859
pixel_ratio=flet_app.screenshots_pixel_ratio
1960
),
39 KB
Loading
33.3 KB
Loading

0 commit comments

Comments
 (0)