Skip to content

Commit bbea284

Browse files
Added Container.on_tap_down event for backward compatibilty with old on_click behavior (#2980)
1 parent 9081df5 commit bbea284

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
6464
children.where((c) => c.name == "content" && c.isVisible);
6565
bool ink = control.attrBool("ink", false)!;
6666
bool onClick = control.attrBool("onclick", false)!;
67+
bool onTapDown = control.attrBool("onTapDown", false)!;
6768
String url = control.attrString("url", "")!;
6869
String? urlTarget = control.attrString("urlTarget");
6970
bool onLongPress = control.attrBool("onLongPress", false)!;
@@ -160,12 +161,23 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
160161
openWebBrowser(url, webWindowName: urlTarget);
161162
}
162163
if (onClick) {
163-
backend.triggerControlEvent(
164-
control.id,
165-
"click");
164+
backend.triggerControlEvent(control.id, "click");
166165
}
167166
}
168167
: null,
168+
onTapDown: onTapDown
169+
? (details) {
170+
backend.triggerControlEvent(
171+
control.id,
172+
"tap_down",
173+
json.encode(ContainerTapEvent(
174+
localX: details.localPosition.dx,
175+
localY: details.localPosition.dy,
176+
globalX: details.globalPosition.dx,
177+
globalY: details.globalPosition.dy)
178+
.toJson()));
179+
}
180+
: null,
169181
onLongPress: onLongPress
170182
? () {
171183
debugPrint("Container ${control.id} long pressed!");
@@ -241,8 +253,7 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
241253
: null,
242254
child: child);
243255

244-
if ((onClick || onLongPress || onHover || url != "") &&
245-
!disabled) {
256+
if ((onClick || onLongPress || onHover || url != "") && !disabled) {
246257
result = MouseRegion(
247258
cursor: onClick || url != ""
248259
? SystemMouseCursors.click
@@ -269,12 +280,23 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
269280
openWebBrowser(url, webWindowName: urlTarget);
270281
}
271282
if (onClick) {
272-
backend.triggerControlEvent(
273-
control.id,
274-
"click");
283+
backend.triggerControlEvent(control.id, "click");
275284
}
276285
}
277286
: null,
287+
onTapDown: onTapDown
288+
? (details) {
289+
backend.triggerControlEvent(
290+
control.id,
291+
"tap_down",
292+
json.encode(ContainerTapEvent(
293+
localX: details.localPosition.dx,
294+
localY: details.localPosition.dy,
295+
globalX: details.globalPosition.dx,
296+
globalY: details.globalPosition.dy)
297+
.toJson()));
298+
}
299+
: null,
278300
onLongPress: onLongPress
279301
? () {
280302
debugPrint("Container ${control.id} clicked!");

sdk/python/packages/flet-core/src/flet_core/container.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def __init__(
8989
theme: Optional[Theme] = None,
9090
theme_mode: Optional[ThemeMode] = None,
9191
on_click=None,
92+
on_tap_down=None,
9293
on_long_press=None,
9394
on_hover=None,
9495
#
@@ -162,8 +163,8 @@ def convert_container_tap_event_data(e):
162163
d = json.loads(e.data)
163164
return ContainerTapEvent(**d)
164165

165-
self.__on_click = EventHandler(convert_container_tap_event_data)
166-
self._add_event_handler("click", self.__on_click.get_handler())
166+
self.__on_tap_down = EventHandler(convert_container_tap_event_data)
167+
self._add_event_handler("tap_down", self.__on_tap_down.get_handler())
167168

168169
self.content = content
169170
self.padding = padding
@@ -191,6 +192,7 @@ def convert_container_tap_event_data(e):
191192
self.theme = theme
192193
self.theme_mode = theme_mode
193194
self.on_click = on_click
195+
self.on_tap_down = on_tap_down
194196
self.on_long_press = on_long_press
195197
self.on_hover = on_hover
196198

@@ -481,6 +483,16 @@ def on_click(self, handler):
481483
self._add_event_handler("click", handler)
482484
self._set_attr("onClick", True if handler is not None else None)
483485

486+
# on_tap_down
487+
@property
488+
def on_tap_down(self):
489+
return self.__on_tap_down
490+
491+
@on_tap_down.setter
492+
def on_tap_down(self, handler):
493+
self.__on_tap_down.subscribe(handler)
494+
self._set_attr("onTapDown", True if handler is not None else None)
495+
484496
# on_long_press
485497
@property
486498
def on_long_press(self):

0 commit comments

Comments
 (0)