show static method
- BuildContext context, {
- Color? backgroundColor,
- Color? color,
- String? title,
- Widget? leading,
- required List<
Widget> builder(- ModalRef ref
- bool disableBackKey = false,
- bool popOnPress = true,
- bool willShowRepetition = false,
- EdgeInsetsGeometry contentPadding = const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
- 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
でテキストカラーを指定します。
disableBackKey
をtrue
にした場合、Android端末での戻るボタンを無効化することが可能です。
popOnPress
がtrue
の場合は、アクションを実行した際、モーダルを自動で閉じます。
willShowRepetition
をtrue
にした場合、onClose
がExceptionで処理を中断した場合、自動でモーダルを再表示します。
モーダルが閉じるまで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);
}