Skip to content

Commit e8430b2

Browse files
committed
RouteChangeEvent, ViewPopEvent
1 parent 454be8f commit e8430b2

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

sdk/python/flet/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from flet.markdown import Markdown
3030
from flet.navigation_rail import NavigationRail, NavigationRailDestination
3131
from flet.outlined_button import OutlinedButton
32-
from flet.page import KeyboardEvent, Page
32+
from flet.page import KeyboardEvent, Page, RouteChangeEvent, ViewPopEvent
3333
from flet.popup_menu_button import PopupMenuButton, PopupMenuItem
3434
from flet.progress_bar import ProgressBar
3535
from flet.progress_ring import ProgressRing

sdk/python/flet/event_handler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ def handler(self, e):
77
for h in self.__handlers.keys():
88
if self.__result_converter != None:
99
r = self.__result_converter(e)
10-
r.target = e.target
11-
r.name = e.name
12-
r.data = e.data
13-
r.control = e.control
14-
r.page = e.page
15-
h(r)
10+
if r != None:
11+
r.target = e.target
12+
r.name = e.name
13+
r.data = e.data
14+
r.control = e.control
15+
r.page = e.page
16+
h(r)
1617
else:
1718
h(e)
1819

sdk/python/flet/page.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,28 @@ def __init__(self, conn: Connection, session_id):
7070
self.__on_resize = EventHandler()
7171
self._add_event_handler("resize", self.__on_resize.handler)
7272

73-
self.__on_route_change = EventHandler(lambda e: e.data)
73+
self.__last_route = None
74+
75+
def convert_route_change_event(e):
76+
if self.__last_route == e.data:
77+
return None # avoid duplicate calls
78+
self.__last_route = e.data
79+
return RouteChangeEvent(route=e.data)
80+
81+
self.__on_route_change = EventHandler(convert_route_change_event)
7482
self._add_event_handler("route_change", self.__on_route_change.handler)
75-
self.__on_view_pop = EventHandler(lambda e: self.get_control(e.data))
83+
84+
def convert_view_pop_event(e):
85+
return ViewPopEvent(view=self.get_control(e.data))
86+
87+
self.__on_view_pop = EventHandler(convert_view_pop_event)
7688
self._add_event_handler("view_pop", self.__on_view_pop.handler)
7789

78-
def convert_keyboard_event_data(e):
90+
def convert_keyboard_event(e):
7991
d = json.loads(e.data)
8092
return KeyboardEvent(**d)
8193

82-
self.__on_keyboard_event = EventHandler(convert_keyboard_event_data)
94+
self.__on_keyboard_event = EventHandler(convert_keyboard_event)
8395
self._add_event_handler("keyboard_event", self.__on_keyboard_event.handler)
8496

8597
self.__on_window_event = EventHandler()
@@ -267,7 +279,15 @@ def show_signin(self, auth_providers="*", auth_groups=False, allow_dismiss=False
267279

268280
def go(self, route):
269281
self.route = route
270-
self.__on_route_change.handler(Event("page", "route_change", self.route))
282+
self.__on_route_change.handler(
283+
ControlEvent(
284+
target="page",
285+
name="route_change",
286+
data=self.route,
287+
page=self,
288+
control=self,
289+
)
290+
)
271291
self.update()
272292

273293
def signout(self):
@@ -952,6 +972,16 @@ def dialog(self, value: Optional[Control]):
952972
self.__dialog = value
953973

954974

975+
@dataclass
976+
class RouteChangeEvent(ControlEvent):
977+
route: str
978+
979+
980+
@dataclass
981+
class ViewPopEvent(ControlEvent):
982+
view: View
983+
984+
955985
@dataclass
956986
class KeyboardEvent(ControlEvent):
957987
key: str

0 commit comments

Comments
 (0)