Skip to content

Commit

Permalink
Fix container.dart for issue flet-dev#2628 (flet-dev#2701)
Browse files Browse the repository at this point in the history
* initial commit

* add mouseCursor and delete not needed event

* Update ink_well.py

* Fix container.dart

The Ink or InkWell widget must have a Material widget as an ancestor.

* Revert change,duplicate control InkWell

Flet already use InkWell on container.dart

* Add on_release (Callback after click)

* Add ink_color
  • Loading branch information
isaffathir authored and zrr1999 committed Jul 17, 2024
1 parent 2024e5d commit fd9daa4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/flet/lib/src/controls/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
children.where((c) => c.name == "content" && c.isVisible);
bool ink = control.attrBool("ink", false)!;
bool onClick = control.attrBool("onclick", false)!;
bool onTap = control.attrBool("ontap", false)!;
String url = control.attrString("url", "")!;
String? urlTarget = control.attrString("urlTarget");
bool onLongPress = control.attrBool("onLongPress", false)!;
Expand Down Expand Up @@ -145,16 +146,22 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {

Widget? result;

if ((onClick || url != "" || onLongPress || onHover) &&
if ((onTap || onClick || url != "" || onLongPress || onHover) &&
ink &&
!disabled) {
var ink = Ink(
decoration: boxDecor,
var ink = Material(
color: Colors.transparent,
borderRadius: boxDecor.borderRadius,
child: InkWell(
// Dummy callback to enable widget
// see https://github.com/flutter/flutter/issues/50116#issuecomment-582047374
// and https://github.com/flutter/flutter/blob/eed80afe2c641fb14b82a22279d2d78c19661787/packages/flutter/lib/src/material/ink_well.dart#L1125-L1129
onTap: onHover ? () {} : null,
onTap: onTap
? () {
debugPrint("Container ${control.id} Tap!");
backend.triggerControlEvent(control.id, "tap", "");
}
: null,
onTapDown: onClick || url != ""
? (details) {
debugPrint("Container ${control.id} clicked!");
Expand Down Expand Up @@ -188,6 +195,8 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
}
: null,
borderRadius: borderRadius,
splashColor: HexColor.fromString(
Theme.of(context), control.attrString("inkColor", "")!),
child: Container(
padding: parseEdgeInsets(control, "padding"),
alignment: parseAlignment(control, "alignment"),
Expand All @@ -202,6 +211,7 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
height: control.attrDouble("height"),
margin: parseEdgeInsets(control, "margin"),
clipBehavior: Clip.none,
decoration: boxDecor,
child: ink,
)
: AnimatedContainer(
Expand Down Expand Up @@ -247,9 +257,10 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
: null,
child: child);

if ((onClick || onLongPress || onHover || url != "") && !disabled) {
if ((onTap || onClick || onLongPress || onHover || url != "") &&
!disabled) {
result = MouseRegion(
cursor: onClick || url != ""
cursor: onTap || onClick || url != ""
? SystemMouseCursors.click
: MouseCursor.defer,
onEnter: onHover
Expand All @@ -267,6 +278,12 @@ class ContainerControl extends StatelessWidget with FletStoreMixin {
}
: null,
child: GestureDetector(
onTap: onTap
? () {
debugPrint("Container ${control.id} onTap!");
backend.triggerControlEvent(control.id, "ontap", "");
}
: null,
onTapDown: onClick || url != ""
? (details) {
debugPrint("Container ${control.id} clicked!");
Expand Down
25 changes: 25 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(
shape: Optional[BoxShape] = None,
clip_behavior: Optional[ClipBehavior] = None,
ink: Optional[bool] = None,
ink_color: Optional[str] = None,
animate: AnimationValue = None,
blur: Union[
None, float, int, Tuple[Union[float, int], Union[float, int]], Blur
Expand All @@ -121,6 +122,7 @@ def __init__(
url_target: Optional[str] = None,
theme: Optional[Theme] = None,
theme_mode: Optional[ThemeMode] = None,
on_release=None,
on_click=None,
on_long_press=None,
on_hover=None,
Expand Down Expand Up @@ -167,6 +169,8 @@ def convert_container_tap_event_data(e):
d = json.loads(e.data)
return ContainerTapEvent(**d)

self.__on_release = EventHandler(convert_container_tap_event_data)
self._add_event_handler("tap", self.__on_release.get_handler())
self.__on_click = EventHandler(convert_container_tap_event_data)
self._add_event_handler("click", self.__on_click.get_handler())

Expand All @@ -187,13 +191,15 @@ def convert_container_tap_event_data(e):
self.shape = shape
self.clip_behavior = clip_behavior
self.ink = ink
self.ink_color = ink_color
self.animate = animate
self.blur = blur
self.shadow = shadow
self.url = url
self.url_target = url_target
self.theme = theme
self.theme_mode = theme_mode
self.on_release = on_release
self.on_click = on_click
self.on_long_press = on_long_press
self.on_hover = on_hover
Expand Down Expand Up @@ -430,6 +436,15 @@ def ink(self) -> Optional[bool]:
def ink(self, value: Optional[bool]):
self._set_attr("ink", value)

# ink color
@property
def ink_color(self):
return self._get_attr("inkColor")

@ink_color.setter
def ink_color(self, value):
self._set_attr("inkColor", value)

# animate
@property
def animate(self) -> AnimationValue:
Expand Down Expand Up @@ -476,6 +491,16 @@ def theme_mode(self, value: Optional[ThemeMode]):
self.__theme_mode = value
self._set_attr("themeMode", value.value if value is not None else None)

# on_release
@property
def on_release(self):
return self._get_event_handler("tap")

@on_release.setter
def on_release(self, handler):
self._add_event_handler("tap", handler)
self._set_attr("onTap", True if handler is not None else None)

# on_click
@property
def on_click(self):
Expand Down

0 comments on commit fd9daa4

Please sign in to comment.