alert static method
- 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,
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
でテキストカラーを指定します。
disableBackKey
をtrue
にした場合、Android端末での戻るボタンを無効化することが可能です。
popOnPress
がtrue
の場合は、アクションを実行した際、モーダルを自動で閉じます。
willShowRepetition
をtrue
にした場合、onSubmit
がExceptionで処理を中断した場合、自動でモーダルを再表示します。
モーダルが閉じるまで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);
}