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

Added support for custom dialog #469

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions example/lib/main_custom_alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ class MyUpgradeAlertState extends UpgradeAlertState {
required String? releaseNotes,
required bool barrierDismissible,
required UpgraderMessages messages,
Widget Function(
Key? key,
String title,
String message,
String? releaseNotes,
BuildContext context,
bool cupertino,
UpgraderMessages messages,
Function() onIgnore,
Function() onUpdate,
Function() onCancel,
)? customDialog,
}) {
showDialog(
context: context,
Expand Down
92 changes: 58 additions & 34 deletions lib/src/upgrade_alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class UpgradeAlert extends StatefulWidget {
this.dialogKey,
this.navigatorKey,
this.child,
this.customDialog,
}) : upgrader = upgrader ?? Upgrader.sharedInstance;

/// The upgraders used to configure the upgrade dialog.
Expand Down Expand Up @@ -81,6 +82,20 @@ class UpgradeAlert extends StatefulWidget {
/// The [child] contained by the widget.
final Widget? child;

/// The custom dialog to display. If null, the default dialog will be displayed.
final Widget Function(
Key? key,
String title,
String message,
String? releaseNotes,
BuildContext context,
bool cupertino,
UpgraderMessages messages,
Function() onIgnore,
Function() onUpdate,
Function() onCancel,
)? customDialog;

@override
UpgradeAlertState createState() => UpgradeAlertState();
}
Expand Down Expand Up @@ -117,8 +132,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
}

if (!displayed) {
final checkContext = widget.navigatorKey != null &&
widget.navigatorKey!.currentContext != null
final checkContext = widget.navigatorKey != null && widget.navigatorKey!.currentContext != null
? widget.navigatorKey!.currentContext!
: context;
checkVersion(context: checkContext);
Expand Down Expand Up @@ -147,10 +161,10 @@ class UpgradeAlertState extends State<UpgradeAlert> {
context: context,
title: appMessages.message(UpgraderMessage.title),
message: widget.upgrader.body(appMessages),
releaseNotes:
shouldDisplayReleaseNotes ? widget.upgrader.releaseNotes : null,
releaseNotes: shouldDisplayReleaseNotes ? widget.upgrader.releaseNotes : null,
barrierDismissible: widget.barrierDismissible,
messages: appMessages,
customDialog: widget.customDialog,
);
});
}
Expand Down Expand Up @@ -208,9 +222,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
displayed = false;
}

bool get shouldDisplayReleaseNotes =>
widget.showReleaseNotes &&
(widget.upgrader.releaseNotes?.isNotEmpty ?? false);
bool get shouldDisplayReleaseNotes => widget.showReleaseNotes && (widget.upgrader.releaseNotes?.isNotEmpty ?? false);

/// Show the alert dialog.
void showTheDialog({
Expand All @@ -221,6 +233,18 @@ class UpgradeAlertState extends State<UpgradeAlert> {
required String? releaseNotes,
required bool barrierDismissible,
required UpgraderMessages messages,
Widget Function(
Key? key,
String title,
String message,
String? releaseNotes,
BuildContext context,
bool cupertino,
UpgraderMessages messages,
Function() onIgnore,
Function() onUpdate,
Function() onCancel,
)? customDialog,
}) {
if (widget.upgrader.state.debugLogging) {
print('upgrader: showTheDialog title: $title');
Expand All @@ -242,15 +266,27 @@ class UpgradeAlertState extends State<UpgradeAlert> {
print('upgrader: showTheDialog onPopInvoked: $didPop');
}
},
child: alertDialog(
key,
title ?? '',
message,
releaseNotes,
context,
widget.dialogStyle == UpgradeDialogStyle.cupertino,
messages,
),
child: customDialog?.call(
key,
title ?? '',
message,
releaseNotes,
context,
widget.dialogStyle == UpgradeDialogStyle.cupertino,
messages,
() => onUserIgnored(context, true),
() => onUserUpdated(context, true),
() => onUserLater(context, true),
) ??
alertDialog(
key,
title ?? '',
message,
releaseNotes,
context,
widget.dialogStyle == UpgradeDialogStyle.cupertino,
messages,
),
);
},
);
Expand All @@ -273,13 +309,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
return false;
}

Widget alertDialog(
Key? key,
String title,
String message,
String? releaseNotes,
BuildContext context,
bool cupertino,
Widget alertDialog(Key? key, String title, String message, String? releaseNotes, BuildContext context, bool cupertino,
UpgraderMessages messages) {
// If installed version is below minimum app version, or is a critical update,
// disable ignore and later buttons.
Expand All @@ -293,9 +323,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
padding: const EdgeInsets.only(top: 15.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: cupertino
? CrossAxisAlignment.center
: CrossAxisAlignment.start,
crossAxisAlignment: cupertino ? CrossAxisAlignment.center : CrossAxisAlignment.start,
children: <Widget>[
Text(messages.message(UpgraderMessage.releaseNotes) ?? '',
style: const TextStyle(fontWeight: FontWeight.bold)),
Expand All @@ -308,14 +336,12 @@ class UpgradeAlertState extends State<UpgradeAlert> {
constraints: const BoxConstraints(maxHeight: 400),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment:
cupertino ? CrossAxisAlignment.center : CrossAxisAlignment.start,
crossAxisAlignment: cupertino ? CrossAxisAlignment.center : CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(message),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(messages.message(UpgraderMessage.prompt) ?? '')),
padding: const EdgeInsets.only(top: 15.0), child: Text(messages.message(UpgraderMessage.prompt) ?? '')),
if (notes != null) notes,
],
)));
Expand Down Expand Up @@ -346,10 +372,8 @@ class UpgradeAlertState extends State<UpgradeAlert> {
];

return cupertino
? CupertinoAlertDialog(
key: key, title: textTitle, content: content, actions: actions)
: AlertDialog(
key: key, title: textTitle, content: content, actions: actions);
? CupertinoAlertDialog(key: key, title: textTitle, content: content, actions: actions)
: AlertDialog(key: key, title: textTitle, content: content, actions: actions);
}

Widget button({
Expand Down
Loading