confirm static method
- BuildContext context, {
- Color? backgroundColor,
- Color? color,
- required String submitText,
- required String cancelText,
- required String title,
- String? text,
- Widget? child,
- Widget? leading,
- VoidCallback? onSubmit,
- VoidCallback? onCancel,
- bool popOnPress = true,
- bool willShowRepetition = false,
- ButtonStyle? submitButtonStyle,
- ButtonStyle? cancelButtonStyle,
Displays a message modal that can perform two actions.
context
to pass the currently available BuildContext.
Use title
to describe the message title text
to describe the message content. By using child
, the text can be substituted by a widget.
In the submitText
field, enter the text of the confirmed action button, and in the onSubmit
field, enter the processing to be performed when the confirmed action is executed.
The text of the cancel action button is described in cancelText
, and the processing when the cancel action is executed is described in onCancel
.
Specify the background color with backgroundColor
and the text color with color
.
If popOnPress
is true
, the modal is automatically closed when the action is executed.
If willShowRepetition
is set to true
, the modal is automatically redisplayed if onSubmit
aborts the process with an Exception.
Returns true
if the modal's confirm button is pressed, or false
if it is canceled.
アクションを2つ実行可能なメッセージモーダルを表示します。
context
で現在利用可能なBuildContextを渡します。
title
でメッセージのタイトルtext
でメッセージの内容を記述します。child
を使用することで、テキストをウィジェットで代用することが可能です。
submitText
で確定アクションボタンのテキスト、onSubmit
に確定アクションを実行する際の処理を記述します。
cancelText
でキャンセルアクションボタンのテキスト、onCancel
にキャンセルアクションを実行する際の処理を記述します。
backgroundColor
で背景色、color
でテキストカラーを指定します。
popOnPress
がtrue
の場合は、アクションを実行した際、モーダルを自動で閉じます。
willShowRepetition
をtrue
にした場合、onSubmit
がExceptionで処理を中断した場合、自動でモーダルを再表示します。
モーダルが閉じるまでawait
で待つことが可能です。
モーダルの確定ボタンが押された場合true
、キャンセルされた場合はfalse
が返されます。
Implementation
static Future<bool> confirm(
BuildContext context, {
Color? backgroundColor,
Color? color,
required String submitText,
required String cancelText,
required String title,
String? text,
Widget? child,
Widget? leading,
VoidCallback? onSubmit,
VoidCallback? onCancel,
bool popOnPress = true,
bool willShowRepetition = false,
ButtonStyle? submitButtonStyle,
ButtonStyle? cancelButtonStyle,
}) async {
bool state = false;
bool clicked = false;
final overlay = Navigator.of(context).overlay;
if (overlay == null) {
return state;
}
final foregroundColor = color ??
Theme.of(context).dialogTheme.iconColor ??
Theme.of(context).colorScheme.onSurface;
backgroundColor ??= Theme.of(context).dialogTheme.backgroundColor ??
Theme.of(context).colorScheme.surface;
do {
await showDialog(
context: overlay.context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (leading != null) ...[
leading,
const SizedBox(width: 8),
],
Expanded(
child: Text(
title,
style: TextStyle(
color: foregroundColor,
),
),
),
],
),
content: SingleChildScrollView(
child: DefaultTextStyle(
style: TextStyle(
color: foregroundColor,
),
child: child ?? Text(text ?? ""),
),
),
backgroundColor: backgroundColor,
surfaceTintColor: backgroundColor,
actions: <Widget>[
TextButton(
style: cancelButtonStyle,
onPressed: () {
if (popOnPress) {
Navigator.of(context, rootNavigator: true).pop();
}
onCancel?.call();
state = false;
clicked = true;
},
child: Text(cancelText),
),
TextButton(
style: submitButtonStyle,
onPressed: () {
if (popOnPress) {
Navigator.of(context, rootNavigator: true).pop();
}
onSubmit?.call();
state = true;
clicked = true;
},
child: Text(submitText),
)
],
);
},
);
} while (willShowRepetition && !clicked);
return state;
}