defaultDialog<T> method
Future<T?>
defaultDialog<T>({
- String title = "Alert",
- EdgeInsetsGeometry? titlePadding,
- TextStyle? titleStyle,
- Widget? content,
- String? id,
- EdgeInsetsGeometry? contentPadding,
- VoidCallback? onConfirm,
- VoidCallback? onCancel,
- VoidCallback? onCustom,
- Color? cancelTextColor,
- Color? confirmTextColor,
- String? textConfirm,
- String? textCancel,
- String? textCustom,
- Widget? confirm,
- Widget? cancel,
- Widget? custom,
- Color? backgroundColor,
- bool barrierDismissible = true,
- Color? buttonColor,
- String middleText = "\n",
- TextStyle? middleTextStyle,
- double radius = 20.0,
- List<
Widget> ? actions, - PopInvokedWithResultCallback<
T> ? onWillPop,
Custom UI Dialog.
Implementation
Future<T?> defaultDialog<T>({
String title = "Alert",
EdgeInsetsGeometry? titlePadding,
TextStyle? titleStyle,
Widget? content,
String? id,
EdgeInsetsGeometry? contentPadding,
VoidCallback? onConfirm,
VoidCallback? onCancel,
VoidCallback? onCustom,
Color? cancelTextColor,
Color? confirmTextColor,
String? textConfirm,
String? textCancel,
String? textCustom,
Widget? confirm,
Widget? cancel,
Widget? custom,
Color? backgroundColor,
bool barrierDismissible = true,
Color? buttonColor,
String middleText = "\n",
TextStyle? middleTextStyle,
double radius = 20.0,
List<Widget>? actions,
PopInvokedWithResultCallback<T>? onWillPop,
GlobalKey<NavigatorState>? navigatorKey,
}) {
// Helper function to create buttons
Widget buildButton({
required String text,
required VoidCallback? onPressed,
Color? textColor,
Color? backgroundColor,
bool isOutlined = false,
}) {
return TextButton(
style: TextButton.styleFrom(
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
backgroundColor: isOutlined ? null : backgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(radius),
side: isOutlined
? BorderSide(color: backgroundColor ?? Colors.grey, width: 2)
: BorderSide.none,
),
),
onPressed: onPressed,
child: Text(text, style: TextStyle(color: textColor)),
);
}
// Initialize actions
actions ??= [];
if (cancel != null) {
actions.add(cancel);
} else if (onCancel != null || textCancel != null) {
actions.add(buildButton(
text: textCancel ?? "Cancel",
onPressed: onCancel ?? closeAllDialogs,
textColor: cancelTextColor ?? buttonColor ?? Colors.grey,
isOutlined: true,
));
}
if (confirm != null) {
actions.add(confirm);
} else if (onConfirm != null || textConfirm != null) {
actions.add(buildButton(
text: textConfirm ?? "Ok",
onPressed: onConfirm,
textColor: confirmTextColor ?? Colors.white,
backgroundColor: buttonColor ?? context!.theme.primaryColor,
));
}
// Build the dialog
Widget baseAlertDialog = AlertDialog(
titlePadding: custom != null
? EdgeInsets.zero
: titlePadding ?? const EdgeInsets.all(8),
contentPadding: contentPadding ?? const EdgeInsets.all(8),
backgroundColor: backgroundColor ?? Colors.white,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(radius)),
title: custom != null
? const SizedBox.shrink()
: Text(title, textAlign: TextAlign.center, style: titleStyle),
content: custom ??
Column(
mainAxisSize: MainAxisSize.min,
children: [
content ??
Text(middleText,
textAlign: TextAlign.center, style: middleTextStyle),
const SizedBox(height: 16),
Wrap(
alignment: WrapAlignment.center,
spacing: 8,
runSpacing: 8,
children: actions,
),
],
),
buttonPadding: EdgeInsets.zero,
);
// Return the dialog
return dialog<T>(
onWillPop != null
? PopScope<T>(
onPopInvokedWithResult: (didPop, result) =>
onWillPop(didPop, result),
child: baseAlertDialog,
)
: baseAlertDialog,
barrierDismissible: barrierDismissible,
navigatorKey: navigatorKey,
id: id,
);
}