forked from flet-dev/flet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CupertinoAlertDialog, CupertinoDialogAction, adaptive property for Al…
…ertDialog (flet-dev#2365) * initial commit * adaptive property for AlertDialog * CupertinoDialogAction control * update cupertino_dialog_action * Update __init__.py removed duplicate imports * is_default_active, is_destructive_action properties * updated properties * Update cupertino_dialog_action.dart refactor * Update cupertino_dialog_action.dart refactor * text_style property * Update cupertino_alert_dialog.dart refactor * Update cupertino_alert_dialog.py refactor * added code examples * changed action from constrainedControl to baseControl * text_style property * refactor * ignore Podfile.lock * sorted imports * include CupertinoAlertDialog in dialog types * changed to CupertinoDialogActionControl to StatelessWidget --------- Co-authored-by: ndonkoHenri <[email protected]>
- Loading branch information
Showing
11 changed files
with
517 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# Flutter-related | ||
**/Flutter/ephemeral/ | ||
**/Pods/ | ||
Podfile.lock | ||
|
||
# Xcode-related | ||
**/dgph | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_redux/flutter_redux.dart'; | ||
|
||
import '../actions.dart'; | ||
import '../flet_app_services.dart'; | ||
import '../models/app_state.dart'; | ||
import '../models/control.dart'; | ||
import '../protocol/update_control_props_payload.dart'; | ||
import 'create_control.dart'; | ||
import 'error.dart'; | ||
|
||
class CupertinoAlertDialogControl extends StatefulWidget { | ||
final Control? parent; | ||
final Control control; | ||
final List<Control> children; | ||
final bool parentDisabled; | ||
final Widget? nextChild; | ||
|
||
const CupertinoAlertDialogControl( | ||
{super.key, | ||
this.parent, | ||
required this.control, | ||
required this.children, | ||
required this.parentDisabled, | ||
required this.nextChild}); | ||
|
||
@override | ||
State<CupertinoAlertDialogControl> createState() => | ||
_CupertinoAlertDialogControlState(); | ||
} | ||
|
||
class _CupertinoAlertDialogControlState | ||
extends State<CupertinoAlertDialogControl> { | ||
Widget _createCupertinoAlertDialog() { | ||
bool disabled = widget.control.isDisabled || widget.parentDisabled; | ||
var titleCtrls = | ||
widget.children.where((c) => c.name == "title" && c.isVisible); | ||
var contentCtrls = | ||
widget.children.where((c) => c.name == "content" && c.isVisible); | ||
var actionCtrls = | ||
widget.children.where((c) => c.name == "action" && c.isVisible); | ||
if (titleCtrls.isEmpty && contentCtrls.isEmpty && actionCtrls.isEmpty) { | ||
return const ErrorControl( | ||
"CupertinoAlertDialog does not have any content."); | ||
} | ||
|
||
return CupertinoAlertDialog( | ||
title: titleCtrls.isNotEmpty | ||
? createControl(widget.control, titleCtrls.first.id, disabled) | ||
: null, | ||
content: contentCtrls.isNotEmpty | ||
? createControl(widget.control, contentCtrls.first.id, disabled) | ||
: null, | ||
actions: actionCtrls | ||
.map((c) => createControl(widget.control, c.id, disabled)) | ||
.toList(), | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
debugPrint("CupertinoAlertDialog build ($hashCode): ${widget.control.id}"); | ||
|
||
var server = FletAppServices.of(context).server; | ||
|
||
bool lastOpen = widget.control.state["open"] ?? false; | ||
|
||
return StoreConnector<AppState, Function>( | ||
distinct: true, | ||
converter: (store) => store.dispatch, | ||
builder: (context, dispatch) { | ||
debugPrint( | ||
"CupertinoAlertDialog StoreConnector build: ${widget.control.id}"); | ||
|
||
var open = widget.control.attrBool("open", false)!; | ||
var modal = widget.control.attrBool("modal", false)!; | ||
|
||
debugPrint("Current open state: $lastOpen"); | ||
debugPrint("New open state: $open"); | ||
|
||
if (open && (open != lastOpen)) { | ||
var dialog = _createCupertinoAlertDialog(); | ||
if (dialog is ErrorControl) { | ||
return dialog; | ||
} | ||
|
||
// close previous dialog | ||
if (ModalRoute.of(context)?.isCurrent != true) { | ||
Navigator.of(context).pop(); | ||
} | ||
|
||
widget.control.state["open"] = open; | ||
|
||
WidgetsBinding.instance.addPostFrameCallback((_) { | ||
showDialog( | ||
barrierDismissible: !modal, | ||
useRootNavigator: false, | ||
context: context, | ||
builder: (context) => dialog).then((value) { | ||
lastOpen = widget.control.state["open"] ?? false; | ||
debugPrint("Dialog should be dismissed ($hashCode): $lastOpen"); | ||
bool shouldDismiss = lastOpen; | ||
widget.control.state["open"] = false; | ||
|
||
if (shouldDismiss) { | ||
List<Map<String, String>> props = [ | ||
{"i": widget.control.id, "open": "false"} | ||
]; | ||
dispatch(UpdateControlPropsAction( | ||
UpdateControlPropsPayload(props: props))); | ||
server.updateControlProps(props: props); | ||
server.sendPageEvent( | ||
eventTarget: widget.control.id, | ||
eventName: "dismiss", | ||
eventData: ""); | ||
} | ||
}); | ||
}); | ||
} else if (open != lastOpen && lastOpen) { | ||
Navigator.of(context).pop(); | ||
} | ||
|
||
return widget.nextChild ?? const SizedBox.shrink(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
import '../flet_app_services.dart'; | ||
import '../models/control.dart'; | ||
import '../utils/text.dart'; | ||
import 'create_control.dart'; | ||
|
||
class CupertinoDialogActionControl extends StatelessWidget { | ||
final Control? parent; | ||
final Control control; | ||
final List<Control> children; | ||
final bool parentDisabled; | ||
|
||
const CupertinoDialogActionControl( | ||
{Key? key, | ||
this.parent, | ||
required this.control, | ||
required this.children, | ||
required this.parentDisabled}) | ||
: super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
debugPrint("CupertinoDialogAction build: ${control.id}"); | ||
|
||
final server = FletAppServices.of(context).server; | ||
|
||
String text = control.attrString("text", "")!; | ||
var contentCtrls = children.where((c) => c.name == "content"); | ||
bool isDefaultAction = control.attrBool("isDefaultAction", false)!; | ||
bool isDestructiveAction = control.attrBool("isDestructiveAction", false)!; | ||
bool disabled = control.isDisabled || parentDisabled; | ||
|
||
Function()? onPressed = !disabled | ||
? () { | ||
debugPrint("CupertinoDialogAction ${control.id} clicked!"); | ||
server.sendPageEvent( | ||
eventTarget: control.id, eventName: "click", eventData: ""); | ||
} | ||
: null; | ||
|
||
CupertinoDialogAction? cupertinoDialogAction; | ||
|
||
cupertinoDialogAction = CupertinoDialogAction( | ||
onPressed: onPressed, | ||
isDefaultAction: isDefaultAction, | ||
isDestructiveAction: isDestructiveAction, | ||
textStyle: parseTextStyle(Theme.of(context), control, "textStyle"), | ||
child: contentCtrls.isNotEmpty | ||
? createControl(control, contentCtrls.first.id, disabled) | ||
: Text(text)); | ||
|
||
return baseControl(context, cupertinoDialogAction, parent, control); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.