show static method

Future<void> show(
  1. BuildContext context, {
  2. Color? backgroundColor,
  3. Color? color,
  4. String? title,
  5. Widget? leading,
  6. required List<Widget> builder(
    1. ModalRef ref
    ),
  7. bool disableBackKey = false,
  8. bool popOnPress = true,
  9. bool willShowRepetition = false,
  10. EdgeInsetsGeometry contentPadding = const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
  11. AlignmentGeometry? alignment,
})

Give builder to display the modal.

The dialog can be closed by executing onClose passed to builder.

Pass the currently available BuildContext in context.

Describe the title of the message in title.

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 onClose interrupts the process with an Exception.

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

builderを与えてモーダルを表示します。

builderにはonCloseが渡されこれを実行することでダイアログを閉じることができます。

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

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

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

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

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

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

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

Implementation

static Future<void> show(
  BuildContext context, {
  Color? backgroundColor,
  Color? color,
  String? title,
  Widget? leading,
  required List<Widget> Function(ModalRef ref) builder,
  bool disableBackKey = false,
  bool popOnPress = true,
  bool willShowRepetition = false,
  EdgeInsetsGeometry contentPadding =
      const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
  AlignmentGeometry? alignment,
}) async {
  assert(
    (title == null && leading == null) ||
        (title != null && leading == null) ||
        (title != null && leading != null),
    "If [leading] is used, [title] is also required.",
  );
  bool clicked = false;
  ScaffoldMessenger.of(context);
  final overlay = Navigator.of(context).overlay;
  if (overlay == null) {
    return;
  }
  final ref = ModalRef._(() {
    if (popOnPress) {
      Navigator.of(context, rootNavigator: true).pop();
    }
    clicked = true;
  });

  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: SimpleDialog(
            contentPadding: contentPadding,
            alignment: alignment,
            title: title != null
                ? Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      if (leading != null) ...[
                        leading,
                        const SizedBox(width: 8),
                      ],
                      Expanded(
                        child: Text(
                          title,
                          style: TextStyle(
                            color: foregroundColor,
                          ),
                        ),
                      ),
                    ],
                  )
                : null,
            backgroundColor: backgroundColor,
            surfaceTintColor: backgroundColor,
            children: builder.call(ref),
          ),
        );
      },
    );
  } while (willShowRepetition && !clicked);
}