showConfirmationDialog method

void showConfirmationDialog(
  1. BuildContext context
)

Shows an AlertDialog to have the user confirm that they want their account deleted

Implementation

void showConfirmationDialog(BuildContext context) {
  final localization = ApptiveGridUserManagementLocalization.of(context);
  final client = ApptiveGridUserManagement.maybeOf(context)?.client;
  final loadingKey = GlobalKey<_LoadingStateWidgetState>();
  showDialog(
    barrierDismissible: false,
    context: context,
    builder: (BuildContext dialogContext) {
      return _LoadingStateWidget(
        key: loadingKey,
        child: Builder(
          builder: (context) {
            final error = _LoadingStateWidget.error(context);
            return AlertDialog(
              title: Text(localization?.deleteAccount ?? 'Delete Account'),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Text(
                    localization?.deleteAccountConfirmation ??
                        'Are you sure you want to delete your account?\n This can not be undone.',
                  ),
                  if (error != null)
                    Text(
                      error is http.Response
                          ? '${error.statusCode}: ${error.body}'
                          : '${localization?.errorUnknown}\n${error!}',
                      style: TextStyle(
                        color: Theme.of(context).colorScheme.error,
                      ),
                    ),
                ],
              ),
              actions: <Widget>[
                if (!_LoadingStateWidget.isLoading(context))
                  TextButton(
                    child: Text(localization?.actionCancel ?? 'Cancel'),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                _LoadingTextButton(
                  loadingKey: loadingKey,
                  onPressed: () async {
                    final deleted = await client
                        ?.deleteAccount()
                        .onError((error, stackTrace) {
                      loadingKey.currentState?.error = error;
                      loadingKey.currentState?.loading = false;
                      return false;
                    });
                    if (deleted == true) {
                      widget.onAccountDeleted();
                      if (mounted) {
                        Navigator.of(context).pop();
                      }
                    }
                  },
                  child: Text(
                    localization?.actionDelete ?? 'Delete',
                    style:
                        TextStyle(color: Theme.of(context).colorScheme.error),
                  ),
                ),
              ],
            );
          },
        ),
      );
    },
  );
}