showUpdateDialog method

void showUpdateDialog({
  1. required BuildContext context,
  2. required VersionStatus versionStatus,
  3. String dialogTitle = 'Update Available',
  4. String? dialogText,
  5. String updateButtonText = 'Update',
  6. bool allowDismissal = true,
  7. String dismissButtonText = 'Maybe Later',
  8. VoidCallback? dismissAction,
})

Shows the user a platform-specific alert about the app update. The user can dismiss the alert or proceed to the app store.

To change the appearance and behavior of the update dialog, you can optionally provide dialogTitle, dialogText, updateButtonText, dismissButtonText, and dismissAction parameters.

Implementation

void showUpdateDialog({
  required BuildContext context,
  required VersionStatus versionStatus,
  String dialogTitle = 'Update Available',
  String? dialogText,
  String updateButtonText = 'Update',
  bool allowDismissal = true,
  String dismissButtonText = 'Maybe Later',
  VoidCallback? dismissAction,
}) async {
  final dialogTitleWidget = Text(dialogTitle);
  final dialogTextWidget = Text(
    dialogText ??
        'You can now update this app from ${versionStatus.localVersion} to ${versionStatus.storeVersion}',
  );

  final updateButtonTextWidget = Text(updateButtonText);
  final updateAction = () {
    launchAppStore(versionStatus.appStoreLink);
    if (allowDismissal) {
      Navigator.of(context, rootNavigator: true).pop();
    }
  };

  List<Widget> actions = [
    Platform.isAndroid
        ? TextButton(
            child: updateButtonTextWidget,
            onPressed: updateAction,
          )
        : CupertinoDialogAction(
            child: updateButtonTextWidget,
            onPressed: updateAction,
          ),
  ];

  if (allowDismissal) {
    final dismissButtonTextWidget = Text(dismissButtonText);
    dismissAction = dismissAction ??
        () => Navigator.of(context, rootNavigator: true).pop();
    actions.add(
      Platform.isAndroid
          ? TextButton(
              child: dismissButtonTextWidget,
              onPressed: dismissAction,
            )
          : CupertinoDialogAction(
              child: dismissButtonTextWidget,
              onPressed: dismissAction,
            ),
    );
  }

  await showDialog(
    context: context,
    barrierDismissible: allowDismissal,
    builder: (BuildContext context) {
      return WillPopScope(
          child: Platform.isAndroid
              ? AlertDialog(
                  title: dialogTitleWidget,
                  content: dialogTextWidget,
                  actions: actions,
                )
              : CupertinoAlertDialog(
                  title: dialogTitleWidget,
                  content: dialogTextWidget,
                  actions: actions,
                ),
          onWillPop: () => Future.value(allowDismissal));
    },
  );
}