confirmAction method

dynamic confirmAction(
  1. dynamic action(), {
  2. required String title,
  3. String dismissText = "Cancel",
  4. CupertinoThemeData? cupertinoThemeData,
  5. ThemeData? themeData,
  6. Color barrierColor = kCupertinoModalBarrierColor,
  7. bool barrierDismissible = true,
  8. bool useRootNavigator = true,
  9. bool semanticsDismissible = false,
  10. RouteSettings? routeSettings,
  11. Offset? anchorPoint,
})

Allow the user to confirm an action. Provide a title for the confirm button. You can also provide a dismissText for the cancel button. E.g. confirmAction(() { ... perform action }, title: "Confirm Action", dismissText: "Cancel");

Implementation

confirmAction(
  Function() action, {
  required String title,
  String dismissText = "Cancel",
  CupertinoThemeData? cupertinoThemeData,
  ThemeData? themeData,
  Color barrierColor = kCupertinoModalBarrierColor,
  bool barrierDismissible = true,
  bool useRootNavigator = true,
  bool semanticsDismissible = false,
  RouteSettings? routeSettings,
  Offset? anchorPoint,
}) {
  if (!kIsWeb && Platform.isIOS) {
    showCupertinoModalPopup(
        barrierColor: barrierColor,
        barrierDismissible: barrierDismissible,
        useRootNavigator: useRootNavigator,
        semanticsDismissible: semanticsDismissible,
        routeSettings: routeSettings,
        anchorPoint: anchorPoint,
        context: context,
        builder: (context) {
          CupertinoActionSheet cupertinoActionSheet = CupertinoActionSheet(
            actions: [
              CupertinoActionSheetAction(
                isDefaultAction: true,
                onPressed: () {
                  Navigator.pop(context);
                  action();
                },
                child: Text(
                  title,
                ),
              ),
            ],
            cancelButton: CupertinoActionSheetAction(
              child: Text(
                dismissText.tr(),
              ),
              onPressed: () => Navigator.pop(context),
            ),
          );
          if (cupertinoThemeData != null) {
            return CupertinoTheme(
                data: cupertinoThemeData, child: cupertinoActionSheet);
          }
          return cupertinoActionSheet;
        });
    return;
  }

  showDialog(
    context: context,
    builder: (context) {
      AlertDialog alertDialog = AlertDialog(
        title: Text(title),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              action();
            },
            child: Text(
              title,
            ),
          ),
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text(
              dismissText.tr(),
            ),
          ),
        ],
      );
      if (themeData != null) {
        return Theme(data: themeData, child: alertDialog);
      }
      return alertDialog;
    },
  );
}