Skip to content

View.can_pop and View.on_confirm_pop #5284

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

Merged
merged 2 commits into from
May 7, 2025
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
.DS_Store
*.bkp
.python-version
vendor/
vendor/
/client/android/app/.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
8 changes: 6 additions & 2 deletions packages/flet/lib/src/controls/dismissible.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class _DismissibleControlState extends State<DismissibleControl> {
parseDismissThresholds(widget.control, "dismissThresholds");

DismissDirection direction = parseDismissDirection(
widget.control.attrString("dismissDirection"), DismissDirection.horizontal)!;
widget.control.attrString("dismissDirection"),
DismissDirection.horizontal)!;

widget.backend.subscribeMethods(widget.control.id,
(methodName, args) async {
Expand Down Expand Up @@ -118,7 +119,10 @@ class _DismissibleControlState extends State<DismissibleControl> {
widget.control.state["confirm_dismiss"] = completer;
widget.backend.triggerControlEvent(
widget.control.id, "confirm_dismiss", direction.name);
return completer.future;
return completer.future.timeout(
const Duration(minutes: 5),
onTimeout: () => false,
);
}
: null,
movementDuration: Duration(
Expand Down
64 changes: 55 additions & 9 deletions packages/flet/lib/src/controls/page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:collection/collection.dart';
Expand Down Expand Up @@ -768,6 +769,7 @@ class _PageControlState extends State<PageControl> with FletStoreMixin {
widgetsDesign: _widgetsDesign,
brightness: _brightness,
themeMode: _themeMode,
isRootView: view.id == routesView.views.first.id,
);

//debugPrint("ROUTES: $_prevViewRoutes $viewRoutes");
Expand All @@ -792,10 +794,11 @@ class _PageControlState extends State<PageControl> with FletStoreMixin {
Widget nextChild = Navigator(
key: navigatorKey,
pages: pages,
onPopPage: (route, dynamic result) {
widget.backend.triggerControlEvent("page", "view_pop",
((route.settings as Page).key as ValueKey).value);
return false;
onDidRemovePage: (page) {
if (page.key != null) {
widget.backend.triggerControlEvent(
"page", "view_pop", (page.key as ValueKey).value);
}
});

// wrap navigator into non-visual offstage controls
Expand All @@ -821,9 +824,10 @@ class ViewControl extends StatefulWidget {
final PageDesign widgetsDesign;
final Brightness? brightness;
final ThemeMode? themeMode;
final bool isRootView;

const ViewControl(
{super.key,
ViewControl(
{Key? key,
required this.parent,
required this.viewId,
required this.overlayWidgets,
Expand All @@ -832,14 +836,17 @@ class ViewControl extends StatefulWidget {
required this.parentAdaptive,
required this.widgetsDesign,
required this.brightness,
required this.themeMode});
required this.themeMode,
required this.isRootView})
: super(key: ValueKey("control_$viewId"));

@override
State<ViewControl> createState() => _ViewControlState();
}

class _ViewControlState extends State<ViewControl> with FletStoreMixin {
final scaffoldKey = GlobalKey<ScaffoldState>();
Completer<bool>? _popCompleter;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -875,6 +882,16 @@ class _ViewControlState extends State<ViewControl> with FletStoreMixin {
CrossAxisAlignment.start)!;
final fabLocation = parseFloatingActionButtonLocation(
control, "floatingActionButtonLocation");
final canPop = control.attrBool("canPop", true)!;

widget.backend.subscribeMethods(control.id, (methodName, args) async {
debugPrint("View.onMethod(${control.id})");
if (methodName == "confirm_pop") {
_popCompleter?.complete(bool.tryParse(args["shouldPop"] ?? ""));
widget.backend.unsubscribeMethods(control.id);
}
return null;
});

Control? appBar;
Control? cupertinoAppBar;
Expand Down Expand Up @@ -907,7 +924,8 @@ class _ViewControlState extends State<ViewControl> with FletStoreMixin {
ctrl.name == "drawer_start") {
drawer = ctrl;
continue;
} else if (ctrl.type == "navigationdrawer" && ctrl.name == "drawer_end") {
} else if (ctrl.type == "navigationdrawer" &&
ctrl.name == "drawer_end") {
endDrawer = ctrl;
continue;
}
Expand Down Expand Up @@ -1153,13 +1171,41 @@ class _ViewControlState extends State<ViewControl> with FletStoreMixin {
child: scaffold,
);
}
var result = Directionality(
Widget result = Directionality(
textDirection: textDirection,
child: widget.loadingPage != null
? Stack(
children: [scaffold, widget.loadingPage!],
)
: scaffold);

result = PopScope(
canPop: canPop,
onPopInvokedWithResult: (didPop, result) {
if (didPop || !control.attrBool("onConfirmPop", false)!) {
return;
}
debugPrint("Page.onPopInvokedWithResult()");
_popCompleter = Completer<bool>();
widget.backend
.triggerControlEvent(widget.viewId, "confirm_pop");
_popCompleter!.future
.timeout(
const Duration(minutes: 5),
onTimeout: () => false,
)
.then((shouldPop) {
if (context.mounted && shouldPop) {
if (widget.isRootView) {
SystemNavigator.pop();
} else {
Navigator.pop(context);
}
}
});
},
child: result);

return withPageArgs((context, pageArgs) {
var backgroundDecoration = parseBoxDecoration(
Theme.of(context), control, "decoration", pageArgs);
Expand Down
10 changes: 10 additions & 0 deletions packages/flet/lib/src/utils/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ bool isMobilePlatform() {
defaultTargetPlatform == TargetPlatform.android);
}

/// Checks if the current platform is iOS
bool isiOSPlatform() {
return !kIsWeb && defaultTargetPlatform == TargetPlatform.iOS;
}

/// Checks if the current platform is Android
bool isAndroidPlatform() {
return !kIsWeb && defaultTargetPlatform == TargetPlatform.android;
}

/// Checks if the current platform is Windows desktop.
bool isWindowsDesktop() {
return !kIsWeb && (defaultTargetPlatform == TargetPlatform.windows);
Expand Down
27 changes: 27 additions & 0 deletions sdk/python/packages/flet/src/flet/core/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
FloatingActionButtonLocation,
MainAxisAlignment,
OffsetValue,
OptionalControlEventCallable,
OptionalEventCallable,
PaddingValue,
ScrollMode,
Expand Down Expand Up @@ -55,6 +56,8 @@ def __init__(
bgcolor: Optional[ColorValue] = None,
decoration: Optional[BoxDecoration] = None,
foreground_decoration: Optional[BoxDecoration] = None,
can_pop: Optional[bool] = None,
on_confirm_pop: OptionalControlEventCallable = None,
#
# ScrollableControl
#
Expand Down Expand Up @@ -99,6 +102,8 @@ def __init__(
self.fullscreen_dialog = fullscreen_dialog
self.decoration = decoration
self.foreground_decoration = foreground_decoration
self.can_pop = can_pop
self.on_confirm_pop = on_confirm_pop

def _get_control_name(self):
return "view"
Expand Down Expand Up @@ -134,6 +139,9 @@ def _get_children(self):
children.append(self.__end_drawer)
return children + self.__controls

def confirm_pop(self, shouldPop: bool):
self.invoke_method("confirm_pop", {"shouldPop": str(shouldPop).lower()})

# route
@property
def route(self):
Expand Down Expand Up @@ -303,6 +311,25 @@ def decoration(self) -> Optional[BoxDecoration]:
def decoration(self, value: Optional[BoxDecoration]):
self.__decoration = value

# can_pop
@property
def can_pop(self) -> Optional[bool]:
return self._get_attr("canPop", data_type="bool")

@can_pop.setter
def can_pop(self, value: Optional[bool]):
self._set_attr("canPop", value)

# on_confirm_pop
@property
def on_confirm_pop(self):
return self._get_event_handler("confirm_pop")

@on_confirm_pop.setter
def on_confirm_pop(self, handler: OptionalControlEventCallable):
self._add_event_handler("confirm_pop", handler)
self._set_attr("onConfirmPop", True if handler is not None else None)

# Magic methods
def __contains__(self, item: Control) -> bool:
return item in self.__controls
Loading