alert static method

Future<void> alert(
  1. BuildContext context, {
  2. required String submitText,
  3. Color? backgroundColor,
  4. Color? color,
  5. required String title,
  6. required String text,
  7. Widget? leading,
  8. VoidCallback? onSubmit,
  9. bool disableBackKey = false,
  10. bool popOnPress = true,
  11. bool willShowRepetition = false,
  12. ButtonStyle? buttonStyle,
})

Displays a message modal with only one possible action.

context to pass the currently available BuildContext.

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

The text of the action button is described in submitText, and the processing when the action is executed is described in onSubmit.

Specify the background color with backgroundColor and the text color with color.

If disableBackKey is set to true, it is possible to disable the back button on Android devices.

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.

It is possible to wait with await until the modal closes.

アクションを1つだけ可能なメッセージモーダルを表示します。

contextで現在利用可能なBuildContextを渡します。

titleでメッセージのタイトルtextでメッセージの内容を記述します。

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

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

disableBackKeytrueにした場合、Android端末での戻るボタンを無効化することが可能です。

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

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

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

Implementation

static Future<void> alert(
  BuildContext context, {
  required String submitText,
  Color? backgroundColor,
  Color? color,
  required String title,
  required String text,
  Widget? leading,
  VoidCallback? onSubmit,
  bool disableBackKey = false,
  bool popOnPress = true,
  bool willShowRepetition = false,
  ButtonStyle? buttonStyle,
}) async {
  bool clicked = false;
  ScaffoldMessenger.of(context);
  final overlay = Navigator.of(context).overlay;
  if (overlay == null) {
    return;
  }
  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 PopScope(
          canPop: !disableBackKey,
          child: 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: buttonStyle,
                onPressed: () {
                  if (popOnPress) {
                    Navigator.of(context, rootNavigator: true).pop();
                  }
                  onSubmit?.call();
                  clicked = true;
                },
                child: Text(submitText),
              )
            ],
          ),
        );
      },
    );
  } while (willShowRepetition && !clicked);
}