Skip to content

Commit 5b797a3

Browse files
committed
Enhance SegmentedButton icon support and tests
Updated SegmentedButton to support both IconData and Control for selected_icon. Improved integration tests for segmented button, added golden images for macOS, and documented SegmentedButtonTheme options. Also clarified NavigationBarTheme test comments.
1 parent 9580fe3 commit 5b797a3

7 files changed

Lines changed: 80 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class _SegmentedButtonControlState extends State<SegmentedButtonControl>
8484
selected: selected,
8585
showSelectedIcon: widget.control.getBool("show_selected_icon", true)!,
8686
style: style,
87-
selectedIcon: widget.control.buildWidget("selected_icon"),
87+
selectedIcon: widget.control.buildIconOrWidget("selected_icon"),
8888
onSelectionChanged: !widget.control.disabled
8989
? (newSelection) => onChange(newSelection)
9090
: null,
5 KB
Loading
2.9 KB
Loading

sdk/python/packages/flet/integration_tests/controls/test_navigation_bar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ async def test_theme(flet_app: ftt.FletTestApp):
3434
flet_app.page.theme = ft.Theme(
3535
navigation_bar_theme=ft.NavigationBarTheme(
3636
bgcolor=ft.Colors.GREEN_200,
37-
shadow_color=ft.Colors.ORANGE_500,
38-
elevation=50,
37+
shadow_color=ft.Colors.ORANGE_500, # not shows in the screenshot
38+
elevation=50, # not shows in the screenshot
3939
indicator_color=ft.Colors.GREEN,
4040
overlay_color=ft.Colors.YELLOW_300,
4141
height=200,
Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import pytest
2+
import pytest_asyncio
23

34
import flet as ft
45
import flet.testing as ftt
56

67

7-
@pytest.mark.asyncio(loop_scope="module")
8-
async def test_segmented_button_basic(flet_app: ftt.FletTestApp, request):
8+
# Create a new flet_app instance for each test method
9+
@pytest_asyncio.fixture(scope="function", autouse=True)
10+
def flet_app(flet_app_function):
11+
return flet_app_function
12+
13+
14+
@pytest.mark.asyncio(loop_scope="function")
15+
async def test_basic(flet_app: ftt.FletTestApp, request):
916
await flet_app.assert_control_screenshot(
1017
request.node.name,
1118
ft.SegmentedButton(
12-
selected_icon=ft.Icon(ft.Icons.CHECK_SHARP),
1319
selected=["1"],
14-
allow_empty_selection=True,
15-
allow_multiple_selection=True,
1620
segments=[
1721
ft.Segment(
1822
value="1",
19-
label=ft.Text("1"),
20-
icon=ft.Icon(ft.Icons.LOOKS_ONE),
23+
label="1",
24+
icon=ft.Icons.LOOKS_ONE,
2125
),
2226
ft.Segment(
2327
value="2",
@@ -27,13 +31,61 @@ async def test_segmented_button_basic(flet_app: ftt.FletTestApp, request):
2731
ft.Segment(
2832
value="3",
2933
label=ft.Text("3"),
30-
icon=ft.Icon(ft.Icons.LOOKS_3),
3134
),
3235
ft.Segment(
3336
value="4",
34-
label=ft.Text("4"),
35-
icon=ft.Icon(ft.Icons.LOOKS_4),
37+
icon=ft.Icons.LOOKS_4,
3638
),
3739
],
3840
),
3941
)
42+
43+
44+
@pytest.mark.asyncio(loop_scope="function")
45+
async def test_theme(flet_app: ftt.FletTestApp):
46+
flet_app.page.theme = ft.Theme(
47+
segmented_button_theme=ft.SegmentedButtonTheme(
48+
selected_icon=ft.Icons.HOME,
49+
style=ft.ButtonStyle(
50+
bgcolor=ft.Colors.BLUE, shape=ft.BeveledRectangleBorder()
51+
),
52+
)
53+
)
54+
flet_app.page.enable_screenshots = True
55+
flet_app.page.window.width = 400
56+
flet_app.page.window.height = 600
57+
58+
scr_1 = ft.Screenshot(
59+
ft.SegmentedButton(
60+
selected=["1"],
61+
segments=[
62+
ft.Segment(
63+
value="1",
64+
label="1",
65+
icon=ft.Icons.LOOKS_ONE,
66+
),
67+
ft.Segment(
68+
value="2",
69+
label=ft.Text("2"),
70+
icon=ft.Icon(ft.Icons.LOOKS_TWO),
71+
),
72+
ft.Segment(
73+
value="3",
74+
label=ft.Text("3"),
75+
),
76+
ft.Segment(
77+
value="4",
78+
icon=ft.Icons.LOOKS_4,
79+
),
80+
],
81+
),
82+
key="sb",
83+
)
84+
flet_app.page.add(scr_1)
85+
flet_app.page.update()
86+
await flet_app.tester.pump_and_settle()
87+
88+
flet_app.assert_screenshot(
89+
"theme_1",
90+
await scr_1.capture(pixel_ratio=flet_app.screenshots_pixel_ratio),
91+
)

sdk/python/packages/flet/src/flet/controls/material/segmented_button.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class SegmentedButton(ConstrainedControl):
105105
when the user (un)selects a segment.
106106
"""
107107

108-
selected_icon: Optional[Control] = None
108+
selected_icon: Optional[IconDataOrControl] = None
109109
"""
110110
An `Icon` control that is used to indicate a segment is selected.
111111

sdk/python/packages/flet/src/flet/controls/theme.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,8 +3152,22 @@ class NavigationBarTheme:
31523152

31533153
@dataclass
31543154
class SegmentedButtonTheme:
3155+
"""
3156+
Customizes the appearance of descendant [`SegmentedButton`][flet.SegmentedButton]
3157+
controls.
3158+
"""
3159+
31553160
selected_icon: Optional[IconData] = None
3161+
"""
3162+
Overrides the default value for
3163+
[`SegmentedButton.selected_icon`][flet.SegmentedButton.selected_icon].
3164+
"""
3165+
31563166
style: Optional[ButtonStyle] = None
3167+
"""
3168+
Overrides the default value for
3169+
[`SegmentedButton.style`][flet.SegmentedButton.style].
3170+
"""
31573171

31583172

31593173
@dataclass

0 commit comments

Comments
 (0)