diff --git a/example/lib/main_custom_alert.dart b/example/lib/main_custom_alert.dart index 8c69d4ea..881c4ccc 100644 --- a/example/lib/main_custom_alert.dart +++ b/example/lib/main_custom_alert.dart @@ -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, diff --git a/lib/src/upgrade_alert.dart b/lib/src/upgrade_alert.dart index 2ea597c3..4a8ffc81 100644 --- a/lib/src/upgrade_alert.dart +++ b/lib/src/upgrade_alert.dart @@ -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. @@ -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(); } @@ -117,8 +132,7 @@ class UpgradeAlertState extends State { } 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); @@ -147,10 +161,10 @@ class UpgradeAlertState extends State { 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, ); }); } @@ -208,9 +222,7 @@ class UpgradeAlertState extends State { 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({ @@ -221,6 +233,18 @@ class UpgradeAlertState extends State { 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'); @@ -242,15 +266,27 @@ class UpgradeAlertState extends State { 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, + ), ); }, ); @@ -273,13 +309,7 @@ class UpgradeAlertState extends State { 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. @@ -293,9 +323,7 @@ class UpgradeAlertState extends State { 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: [ Text(messages.message(UpgraderMessage.releaseNotes) ?? '', style: const TextStyle(fontWeight: FontWeight.bold)), @@ -308,14 +336,12 @@ class UpgradeAlertState extends State { 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: [ 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, ], ))); @@ -346,10 +372,8 @@ class UpgradeAlertState extends State { ]; 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({