Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dismissible.confirmDismiss prop #2359

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package/lib/src/controls/create_control.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import 'dart:math';

import 'package:flutter/material.dart';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';

import '../flet_app_services.dart';
import '../flet_server.dart';
import '../models/app_state.dart';
import '../models/control.dart';
import '../models/control_view_model.dart';
import '../models/page_media_view_model.dart';
import '../utils/animations.dart';
import '../utils/theme.dart';
import '../utils/transforms.dart';

import 'alert_dialog.dart';
import 'animated_switcher.dart';
import 'audio.dart';
Expand Down Expand Up @@ -131,8 +130,8 @@ Widget createControl(Control? parent, String id, bool parentDisabled,
}

// create control widget
var widget = createWidget(
controlKey, controlView, parent, parentDisabled, nextChild);
var widget = createWidget(controlKey, controlView, parent, parentDisabled,
nextChild, FletAppServices.of(context).server);

// no theme defined? return widget!
if (id == "page" || controlView.control.attrString("theme") == null) {
Expand Down Expand Up @@ -170,7 +169,7 @@ Widget createControl(Control? parent, String id, bool parentDisabled,
}

Widget createWidget(Key? key, ControlViewModel controlView, Control? parent,
bool parentDisabled, Widget? nextChild) {
bool parentDisabled, Widget? nextChild, FletServer server) {
switch (controlView.control.type) {
case "page":
return PageControl(
Expand Down Expand Up @@ -611,7 +610,8 @@ Widget createWidget(Key? key, ControlViewModel controlView, Control? parent,
parent: parent,
control: controlView.control,
children: controlView.children,
parentDisabled: parentDisabled);
parentDisabled: parentDisabled,
server: server);
case "alertdialog":
return AlertDialogControl(
parent: parent,
Expand Down
97 changes: 80 additions & 17 deletions package/lib/src/controls/dismissible.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:flet/src/utils/dismissible.dart';
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';

import '../flet_app_services.dart';
import '../flet_server.dart';
import '../models/control.dart';
import '../utils/dismissible.dart';
import 'create_control.dart';
import 'error.dart';

Expand All @@ -11,17 +14,18 @@ class DismissibleControl extends StatelessWidget {
final Control control;
final List<Control> children;
final bool parentDisabled;
final FletServer server;

const DismissibleControl(
{super.key,
this.parent,
required this.control,
required this.children,
required this.parentDisabled});
required this.parentDisabled,
required this.server});

@override
Widget build(BuildContext context) {
var server = FletAppServices.of(context).server;
debugPrint("Dismissible build: ${control.id}");

bool disabled = control.isDisabled || parentDisabled;
Expand All @@ -45,6 +49,17 @@ class DismissibleControl extends StatelessWidget {
control.attrString("dismissDirection", "")!.toLowerCase(),
orElse: () => DismissDirection.horizontal);

server.controlInvokeMethods[control.id] = (methodName, args) async {
debugPrint("Dismissible.onMethod(${control.id})");
if (methodName == "confirm_dismiss") {
control.state["confirm_dismiss"]
?.complete(bool.tryParse(args["dismiss"] ?? ""));
server.controlInvokeMethods.remove(control.id);
}

return null;
};

return constrainedControl(
context,
Dismissible(
Expand All @@ -57,19 +72,47 @@ class DismissibleControl extends StatelessWidget {
? createControl(
control, secondaryBackgroundCtrls.first.id, disabled)
: Container(color: Colors.transparent),
onDismissed: (DismissDirection d) {
server.sendPageEvent(
eventTarget: control.id, eventName: "dismiss", eventData: d.name);
},
onResize: () {
server.sendPageEvent(
eventTarget: control.id, eventName: "resize", eventData: "");
},
onUpdate: (DismissUpdateDetails d) {
server.sendPageEvent(
eventTarget: control.id, eventName: "update", eventData: "");
},
// confirmDismiss: // TODO: implement
onDismissed: control.attrBool("onDismiss", false)!
? (DismissDirection direction) {
server.sendPageEvent(
eventTarget: control.id,
eventName: "dismiss",
eventData: direction.name);
}
: null,
onResize: control.attrBool("onResize", false)!
? () {
server.sendPageEvent(
eventTarget: control.id,
eventName: "resize",
eventData: "");
}
: null,
onUpdate: control.attrBool("onUpdate", false)!
? (DismissUpdateDetails details) {
server.sendPageEvent(
eventTarget: control.id,
eventName: "update",
eventData: json.encode(DismissibleUpdateEvent(
direction: details.direction.name,
previousReached: details.previousReached,
progress: details.progress,
reached: details.reached)
.toJson()));
}
: null,
confirmDismiss: control.attrBool("onConfirmDismiss", false)!
? (DismissDirection direction) {
debugPrint("Dismissible.confirmDismiss(${control.id})");
var completer = Completer<bool?>();
control.state["confirm_dismiss"] = completer;
server.sendPageEvent(
eventTarget: control.id,
eventName: "confirm_dismiss",
eventData: direction.name);
return completer.future;
}
: null,
movementDuration:
Duration(milliseconds: control.attrInt("duration", 200)!),
resizeDuration:
Expand All @@ -81,3 +124,23 @@ class DismissibleControl extends StatelessWidget {
control);
}
}

class DismissibleUpdateEvent {
final String direction;
final bool previousReached;
final double progress;
final bool reached;

DismissibleUpdateEvent(
{required this.direction,
required this.progress,
required this.previousReached,
required this.reached});

Map<String, dynamic> toJson() => <String, dynamic>{
'direction': direction,
'progress': progress,
'reached': reached,
'previous_reached': previousReached
};
}
2 changes: 1 addition & 1 deletion sdk/python/packages/flet-core/src/flet_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
DataTable,
)
from flet_core.date_picker import DatePicker, DatePickerEntryMode, DatePickerMode
from flet_core.dismissible import Dismissible, DismissibleDismissEvent
from flet_core.dismissible import Dismissible, DismissibleDismissEvent, DismissibleUpdateEvent
from flet_core.divider import Divider
from flet_core.drag_target import DragTarget, DragTargetAcceptEvent
from flet_core.draggable import Draggable
Expand Down
60 changes: 52 additions & 8 deletions sdk/python/packages/flet-core/src/flet_core/dismissible.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Optional, Dict, Union
import json
from typing import Any, Dict, Optional, Union

from flet_core.constrained_control import ConstrainedControl
from flet_core.control import Control, OptionalNumber
Expand All @@ -7,11 +8,11 @@
from flet_core.ref import Ref
from flet_core.snack_bar import DismissDirection
from flet_core.types import (
ResponsiveNumber,
ScaleValue,
AnimationValue,
RotateValue,
OffsetValue,
ResponsiveNumber,
RotateValue,
ScaleValue,
)


Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(
cross_axis_end_offset: OptionalNumber = None,
on_update=None,
on_dismiss=None,
on_confirm_dismiss=None,
on_resize=None,
):
ConstrainedControl.__init__(
Expand Down Expand Up @@ -103,7 +105,16 @@ def __init__(
)

self.__on_dismiss = EventHandler(lambda e: DismissibleDismissEvent(e.data))
self.__on_update = EventHandler(lambda e: DismissibleUpdateEvent(**json.loads(e.data)))
self.__on_confirm_dismiss = EventHandler(
lambda e: DismissibleDismissEvent(e.data)
)

self._add_event_handler("dismiss", self.__on_dismiss.get_handler())
self._add_event_handler("update", self.__on_update.get_handler())
self._add_event_handler(
"confirm_dismiss", self.__on_confirm_dismiss.get_handler()
)

self.content = content
self.background = background
Expand All @@ -115,6 +126,7 @@ def __init__(
self.cross_axis_end_offset = cross_axis_end_offset
self.on_update = on_update
self.on_dismiss = on_dismiss
self.on_confirm_dismiss = on_confirm_dismiss
self.on_resize = on_resize

def _get_control_name(self):
Expand All @@ -137,6 +149,16 @@ def _before_build_command(self):
super()._before_build_command()
self._set_attr_json("dismissThresholds", self.__dismiss_thresholds)

def confirm_dismiss(self, dismiss: bool):
self.page.invoke_method(
"confirm_dismiss", {"dismiss": str(dismiss).lower()}, control_id=self.uid
)

async def confirm_dismiss_async(self, dismiss: bool):
await self.page.invoke_method_async(
"confirm_dismiss", {"dismiss": str(dismiss).lower()}, control_id=self.uid
)

# content
@property
def content(self) -> Control:
Expand Down Expand Up @@ -223,7 +245,17 @@ def on_dismiss(self):
@on_dismiss.setter
def on_dismiss(self, handler):
self.__on_dismiss.subscribe(handler)
self._set_attr("dismiss", True if handler is not None else None)
self._set_attr("onDismiss", True if handler is not None else None)

# on_confirm_dismiss
@property
def on_confirm_dismiss(self):
return self._get_event_handler("confirm_dismiss")

@on_confirm_dismiss.setter
def on_confirm_dismiss(self, handler):
self.__on_confirm_dismiss.subscribe(handler)
self._set_attr("onConfirmDismiss", True if handler is not None else None)

# on_update
@property
Expand All @@ -232,7 +264,8 @@ def on_update(self):

@on_update.setter
def on_update(self, handler):
self._add_event_handler("update", handler)
self.__on_update.subscribe(handler)
self._set_attr("onUpdate", True if handler is not None else None)

# on_resize
@property
Expand All @@ -242,8 +275,19 @@ def on_resize(self):
@on_resize.setter
def on_resize(self, handler):
self._add_event_handler("resize", handler)
self._set_attr("onResize", True if handler is not None else None)


class DismissibleDismissEvent(ControlEvent):
def __init__(self, d: str) -> None:
self.direction: DismissDirection = DismissDirection(d)
def __init__(self, direction: str) -> None:
self.direction: DismissDirection = DismissDirection(direction)


class DismissibleUpdateEvent(ControlEvent):
def __init__(
self, direction: str, progress: float, reached: bool, previous_reached: bool
) -> None:
self.direction: DismissDirection = DismissDirection(direction)
self.progress = progress
self.reached = reached
self.previous_reached = previous_reached