showMessage static method

void showMessage({
  1. required BuildContext context,
  2. required String message,
  3. String buttonText = "OK",
  4. TextStyle? buttonTextStyle,
  5. TextStyle? textStyle,
  6. bool barrierDismissible = false,
})

Displays a simple alert dialog.

  • context: The build context of the current widget.
  • message: The title/message to display inside the dialog.
  • buttonText: The text for the action button (default: "OK").
  • buttonTextStyle: Custom style for the button text.
  • textStyle: Custom style for the message text.
  • barrierDismissible: Determines whether the dialog can be dismissed by tapping outside (default: false).

Implementation

static void showMessage({
  required BuildContext context,
  required String message,
  String buttonText = "OK", // Default value for the button text
  TextStyle? buttonTextStyle, // Optional custom button text style
  TextStyle? textStyle, // Optional custom title/message text style
  bool barrierDismissible =
      false, // Whether tapping outside should dismiss the dialog
}) {
  showDialog(
    context: context,
    barrierDismissible:
        barrierDismissible, // Ensures user must tap the button to close (unless true)
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text(
          message,
          style: textStyle ??
              const TextStyle(
                  fontSize: 18,
                  fontWeight:
                      FontWeight.bold), // Default text style if none provided
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context)
                .pop(), // Closes the dialog on button press
            child: Text(
              buttonText,
              style: buttonTextStyle ??
                  const TextStyle(
                      fontSize:
                          16), // Default button text style if none provided
            ),
          ),
        ],
      );
    },
  );
}