Skip to content

Commit

Permalink
fix: Added Modal.show.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Mar 14, 2023
1 parent 8a29a68 commit 4db6817
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/katana_ui/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.2"
version: "1.2.3"
lints:
dependency: transitive
description:
Expand Down
82 changes: 82 additions & 0 deletions packages/katana_ui/lib/modal/modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,88 @@ part of katana_ui;
class Modal {
const Modal._();

/// 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`で待つことが可能です。
static Future<void> show(
BuildContext context, {
Color? backgroundColor,
Color? color,
required String title,
required List<Widget> Function(VoidCallback onClose) builder,
bool disableBackKey = false,
bool popOnPress = true,
bool willShowRepetition = false,
}) async {
bool clicked = false;
ScaffoldMessenger.of(context);
final overlay = Navigator.of(context).overlay;
if (overlay == null) {
return;
}
onClose() {
if (popOnPress) {
Navigator.of(context, rootNavigator: true).pop();
}
clicked = true;
}

do {
await showDialog(
context: overlay.context,
barrierDismissible: false,
builder: (context) {
return WillPopScope(
onWillPop: disableBackKey ? () async => true : null,
child: SimpleDialog(
title: Text(
title,
style: TextStyle(
color: color ?? Theme.of(context).colorScheme.onSurface,
),
),
backgroundColor:
backgroundColor ?? Theme.of(context).colorScheme.surface,
children: builder.call(onClose),
),
);
},
);
} while (willShowRepetition && !clicked);
}

/// Displays a message modal with only one possible action.
///
/// [context] to pass the currently available [BuildContext].
Expand Down

0 comments on commit 4db6817

Please sign in to comment.