confirm static method

Future<bool> confirm(
  1. BuildContext context, {
  2. Color? backgroundColor,
  3. Color? color,
  4. required String submitText,
  5. required String cancelText,
  6. required String title,
  7. required String text,
  8. Widget? leading,
  9. VoidCallback? onSubmit,
  10. VoidCallback? onCancel,
  11. bool popOnPress = true,
  12. bool willShowRepetition = false,
  13. ButtonStyle? submitButtonStyle,
  14. ButtonStyle? cancelButtonStyle,
})

Displays a message modal that can perform two actions.

context to pass the currently available BuildContext.

Describe the message title in title and the message content in text.

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でメッセージの内容を記述します。

submitTextで確定アクションボタンのテキスト、onSubmitに確定アクションを実行する際の処理を記述します。

cancelTextでキャンセルアクションボタンのテキスト、onCancelにキャンセルアクションを実行する際の処理を記述します。

backgroundColorで背景色、colorでテキストカラーを指定します。

popOnPresstrueの場合は、アクションを実行した際、モーダルを自動で閉じます。

willShowRepetitiontrueにした場合、onSubmitExceptionで処理を中断した場合、自動でモーダルを再表示します。

モーダルが閉じるまでawaitで待つことが可能です。

モーダルの確定ボタンが押された場合true、キャンセルされた場合はfalseが返されます。

Implementation

static Future<bool> confirm(
  BuildContext context, {
  Color? backgroundColor,
  Color? color,
  required String submitText,
  required String cancelText,
  required String title,
  required String text,
  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: Text(
              text,
              style: TextStyle(
                color: foregroundColor,
              ),
            ),
          ),
          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;
}