showConfirmDialog static method

dynamic showConfirmDialog({
  1. required BuildContext context,
  2. String? title,
  3. String? message,
  4. Widget? content,
  5. required String buttonLeft,
  6. String? buttonRight,
  7. bool autoPop = true,
  8. dynamic onPressed(
    1. int
    )?,
  9. bool barrierDismissible = true,
})

Shwos a confirmation dialog

title title for the dialog

message Text message to be diplayed

content can be used to override message. If content is set, message won't be used

buttonLeft this is the primary button. This cannot be null. If you have multiple buttons, then this will be displayed on left side

buttonRight an optional button to be displayed on right side

autoPop default to true. If false, Navigator.pop(context) won't be called

onPressed will be called when we press any button with button index as parameter Index 0 => Left button and 1 => Right button

Implementation

static showConfirmDialog({
  required BuildContext context,
  final String? title,
  final String? message,
  final Widget? content,
  required final String buttonLeft,
  final String? buttonRight,
  final bool autoPop = true,
  final Function(int)? onPressed,
  final bool barrierDismissible = true,
}) {
  return showDialog(
    context: context,
    useRootNavigator: false,
    barrierDismissible: barrierDismissible,
    useSafeArea: true,
    barrierColor: ESTheme.barrierColor(context),
    builder: (BuildContext context) => RawKeyboardListener(
      autofocus: true,
      focusNode: FocusNode(),
      onKey: (event) {
        if (event.isKeyPressed(LogicalKeyboardKey.enter) ||
            event.isKeyPressed(LogicalKeyboardKey.numpadEnter)) {
          if (autoPop) Navigator.pop(context);
          if (onPressed != null) onPressed(1);
        }
      },
      child: CupertinoAlertDialog(
        title: Column(
          children: <Widget>[
            Text(title ?? ''),
            const SizedBox(height: 10),
          ],
        ),
        content: content ?? Text(message ?? ''),
        actions: [
          Tooltip(
            message: '[Esc]',
            child: CupertinoDialogAction(
              onPressed: () {
                if (autoPop) Navigator.pop(context);
                if (onPressed != null) onPressed(0);
              },
              child: Text(buttonLeft,
                  style: TextStyle(color: ESTheme.textColor(context))),
            ),
          ),
          if (buttonRight != null)
            Tooltip(
              message: '[Press Enter]',
              child: CupertinoDialogAction(
                onPressed: () {
                  if (autoPop) Navigator.pop(context);
                  if (onPressed != null) onPressed(1);
                },
                child: Text(buttonRight,
                    style: TextStyle(
                        fontWeight: FontWeight.w600,
                        color: ESTheme.textColor(context))),
              ),
            ),
        ],
      ),
    ),
  );
}