showYesNoAlertDialog function
Future<bool?>
showYesNoAlertDialog({
- required BuildContext context,
- required Widget? title,
- required Widget? content,
- required Widget? yesText,
- required Widget? noText,
- bool reverseYesNoOrder = false,
- bool barrierDismissible = true,
- Color? barrierColor = Colors.black54,
- String? barrierLabel,
- bool useSafeArea = true,
- RouteSettings? routeSettings,
- EdgeInsetsGeometry? titlePadding,
- TextStyle? titleTextStyle,
- EdgeInsetsGeometry contentPadding = kAlertDialogDefaultContentPadding,
- TextStyle? contentTextStyle,
- List<
Widget> ? actions, - EdgeInsetsGeometry actionsPadding = EdgeInsets.zero,
- MainAxisAlignment? actionsAlignment,
- VerticalDirection? actionsOverflowDirection,
- double? actionsOverflowButtonSpacing,
- EdgeInsetsGeometry? buttonPadding,
- Color? backgroundColor,
- double? elevation,
- String? semanticLabel,
- EdgeInsets insetPadding = kDialogDefaultInsetPadding,
- Clip clipBehavior = Clip.none,
- ShapeBorder? shape,
- AlignmentGeometry? alignment,
- bool scrollable = false,
- void yesOnPressed()?,
- void yesOnLongPress()?,
- void yesOnHover()?,
- void yesOnFocusChange()?,
- ButtonStyle? yesStyle,
- FocusNode? yesFocusNode,
- bool yesAutoFocus = false,
- Clip yesClipBehavior = Clip.none,
- void noOnPressed()?,
- void noOnLongPress()?,
- void noOnHover()?,
- void noOnFocusChange()?,
- ButtonStyle? noStyle,
- FocusNode? noFocusNode,
- bool noAutoFocus = false,
- Clip noClipBehavior = Clip.none,
This is a convenient function to show AlertDialog with given title
, content
and two action buttons.
Note that you can set yesText
or noText
to null to hide the specific button. Also note that yesOnPressed
and noOnPressed
can be used to override the default onPressed callback, but Navigator.pop must be called manually.
Implementation
Future<bool?> showYesNoAlertDialog({
required BuildContext context,
required Widget? title,
required Widget? content,
required Widget? yesText,
required Widget? noText,
bool reverseYesNoOrder = false,
// showDialog parameters
bool barrierDismissible = true,
Color? barrierColor = Colors.black54,
String? barrierLabel,
bool useSafeArea = true,
bool useRootNavigator = true,
RouteSettings? routeSettings,
// AlertDialog parameters
EdgeInsetsGeometry? titlePadding,
TextStyle? titleTextStyle,
EdgeInsetsGeometry contentPadding = kAlertDialogDefaultContentPadding,
TextStyle? contentTextStyle,
List<Widget>? actions,
EdgeInsetsGeometry actionsPadding = EdgeInsets.zero,
MainAxisAlignment? actionsAlignment,
VerticalDirection? actionsOverflowDirection,
double? actionsOverflowButtonSpacing,
EdgeInsetsGeometry? buttonPadding,
Color? backgroundColor,
double? elevation,
String? semanticLabel,
EdgeInsets insetPadding = kDialogDefaultInsetPadding,
Clip clipBehavior = Clip.none,
ShapeBorder? shape,
AlignmentGeometry? alignment,
bool scrollable = false,
// actions parameters
void Function(BuildContext)? yesOnPressed,
void Function(BuildContext)? yesOnLongPress,
void Function(BuildContext, bool)? yesOnHover,
void Function(BuildContext, bool)? yesOnFocusChange,
ButtonStyle? yesStyle,
FocusNode? yesFocusNode,
bool yesAutoFocus = false,
Clip yesClipBehavior = Clip.none,
void Function(BuildContext)? noOnPressed,
void Function(BuildContext)? noOnLongPress,
void Function(BuildContext, bool)? noOnHover,
void Function(BuildContext, bool)? noOnFocusChange,
ButtonStyle? noStyle,
FocusNode? noFocusNode,
bool noAutoFocus = false,
Clip noClipBehavior = Clip.none,
}) async {
return await showDialog<bool>(
context: context,
builder: (c) {
var actions = [
if (yesText != null)
TextButton(
child: yesText,
onPressed: yesOnPressed == null ? () => Navigator.of(c).pop(true) : () => yesOnPressed.call(c),
onLongPress: yesOnLongPress == null ? null : () => yesOnLongPress.call(c),
onHover: yesOnHover == null ? null : (v) => yesOnHover.call(c, v),
onFocusChange: yesOnFocusChange == null ? null : (v) => yesOnFocusChange.call(c, v),
style: yesStyle,
focusNode: yesFocusNode,
autofocus: yesAutoFocus,
clipBehavior: yesClipBehavior,
),
if (noText != null)
TextButton(
child: noText,
onPressed: noOnPressed == null ? () => Navigator.of(c).pop(false) : () => noOnPressed.call(c),
onLongPress: noOnLongPress == null ? null : () => noOnLongPress.call(c),
onHover: noOnHover == null ? null : (v) => noOnHover.call(c, v),
onFocusChange: noOnFocusChange == null ? null : (v) => noOnFocusChange.call(c, v),
style: noStyle,
focusNode: noFocusNode,
autofocus: noAutoFocus,
clipBehavior: noClipBehavior,
),
];
if (reverseYesNoOrder) {
actions = actions.reversed.toList();
}
return AlertDialog(
title: title,
content: content,
actions: actions,
// AlertDialog parameters
titlePadding: titlePadding,
titleTextStyle: titleTextStyle,
contentPadding: contentPadding,
contentTextStyle: contentTextStyle,
actionsPadding: actionsPadding,
actionsAlignment: actionsAlignment,
actionsOverflowDirection: actionsOverflowDirection,
actionsOverflowButtonSpacing: actionsOverflowButtonSpacing,
buttonPadding: buttonPadding,
backgroundColor: backgroundColor,
elevation: elevation,
semanticLabel: semanticLabel,
insetPadding: insetPadding,
clipBehavior: clipBehavior,
shape: shape,
alignment: alignment,
scrollable: scrollable,
);
},
// showDialog parameters
barrierDismissible: barrierDismissible,
barrierColor: barrierColor,
barrierLabel: barrierLabel,
useSafeArea: useSafeArea,
useRootNavigator: useRootNavigator,
routeSettings: routeSettings,
);
}