show<T> static method
- required BuildContext context,
- IconData? icon,
- Color? iconColor,
- double? iconSize,
- required String markdownText,
- BoxConstraints? textConstraints,
- TextAlign textAlignment = TextAlign.start,
- String? cancelText,
- String? confirmText,
- String? dismissText,
- VoidCallback? onCancel,
- VoidCallback? onConfirm,
- VoidCallback? onDismiss,
- bool barrierDismissible = true,
Shows an alert dialog with the specified parameters.
context
- The build context
icon
- The icon to display in the dialog title. Defaults to Icons.warning
iconColor
- The color of the icon. Defaults to Colors.amber
iconSize
- The size of the icon. Defaults to 64.0
markdownText
- The markdown text content to display
textConstraints
- Optional constraints for the text content area
textAlignment
- Alignment of the text content. Defaults to TextAlign.start
cancelText
- Text for the cancel button. If null, no cancel button is shown
confirmText
- Text for the confirm button. If null, no confirm button is shown
dismissText
- Text for the dismiss button. Used when both cancelText and confirmText are null
onCancel
- Callback when cancel button is pressed
onConfirm
- Callback when confirm button is pressed
onDismiss
- Callback when dismiss button is pressed
barrierDismissible
- Whether the dialog can be dismissed by tapping outside
Implementation
static Future<T?> show<T>({
required BuildContext context,
IconData? icon,
Color? iconColor,
double? iconSize,
required String markdownText,
BoxConstraints? textConstraints,
TextAlign textAlignment = TextAlign.start,
String? cancelText,
String? confirmText,
String? dismissText,
VoidCallback? onCancel,
VoidCallback? onConfirm,
VoidCallback? onDismiss,
bool barrierDismissible = true,
}) {
final theme = Theme.of(context);
final style = theme.textTheme.bodyMedium;
// Determine which buttons to show
final hasCancelButton = cancelText != null;
final hasConfirmButton = confirmText != null;
final hasDismissButton = !hasCancelButton && !hasConfirmButton;
// Use dismissText if provided, otherwise default to "Dismiss"
final finalDismissText = dismissText ?? 'Dismiss';
// Create the markdown widget
final markdownWidget = MarkdownBody(
styleSheet: createMarkDownStyleSheet(context).copyWith(
strong: style?.copyWith(fontWeight: FontWeight.bold),
em: style?.copyWith(fontStyle: FontStyle.italic),
h1: style?.copyWith(
fontSize: 24.0,
fontWeight: FontWeight.bold,
),
h2: style?.copyWith(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
h3: style?.copyWith(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
data: markdownText,
);
final containerWidget = Container(
alignment: textAlignment == TextAlign.center
? Alignment.center
: textAlignment == TextAlign.end
? Alignment.centerRight
: Alignment.centerLeft,
child: markdownWidget,
);
return showDialog<T>(
context: context,
barrierDismissible: barrierDismissible,
builder: (BuildContext dialogContext) {
return AlertDialog(
title: Icon(
icon ?? Icons.warning,
color: iconColor ?? Colors.amber,
size: iconSize ?? 64.0,
),
content: SingleChildScrollView(
child: textConstraints != null
? ConstrainedBox(
constraints: textConstraints,
child: containerWidget,
)
: containerWidget,
),
actions: <Widget>[
if (hasCancelButton)
TextButton(
child: Text(cancelText),
onPressed: () {
Navigator.of(dialogContext).pop();
onCancel?.call();
},
),
if (hasConfirmButton)
TextButton(
child: Text(confirmText),
onPressed: () {
Navigator.of(dialogContext).pop();
onConfirm?.call();
},
),
if (hasDismissButton)
TextButton(
child: Text(finalDismissText),
onPressed: () {
Navigator.of(dialogContext).pop();
onDismiss?.call();
},
),
],
);
},
);
}