From 964bc880d164e59d57668624b921d442f8e05b7d Mon Sep 17 00:00:00 2001 From: Niels Rijnberg Date: Tue, 25 Feb 2025 09:27:33 +0100 Subject: [PATCH 1/2] Add native dialog to toolkit --- lib/ui/native_dialog.dart | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/ui/native_dialog.dart diff --git a/lib/ui/native_dialog.dart b/lib/ui/native_dialog.dart new file mode 100644 index 0000000..5c9f34a --- /dev/null +++ b/lib/ui/native_dialog.dart @@ -0,0 +1,73 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +/// A native dialog which shows a [CupertinoAlertDialog] on iOS and a [AlertDialog] on Android. +/// +/// Example: +/// ```dart +/// showNativeDialog( +/// context, +/// title: 'Title', +/// content: 'Content', +/// actions: [ +/// DialogAction(text: 'Action', onTap: () {}), +/// ], +/// ); +/// ``` +void showNativeDialog( + BuildContext context, { + required String title, + required List actions, + required String content, +}) { + if (defaultTargetPlatform == TargetPlatform.iOS) { + showCupertinoDialog( + context: context, + builder: (context) => CupertinoAlertDialog( + title: Text(title), + content: Text(content), + actions: actions + .map( + (action) => CupertinoDialogAction( + onPressed: action.onTap, + isDestructiveAction: action.isDestructiveAction, + child: Text(action.text), + ), + ) + .toList(), + ), + ); + } else { + showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text(title), + content: Text(content), + actions: actions + .map( + (action) => TextButton( + onPressed: action.onTap, + child: Text(action.text), + ), + ) + .toList(), + ), + ); + } +} + +/// A dialog action which is used to show the actions of a native dialog. +class DialogAction { + /// Creates a [DialogAction]. + const DialogAction({required this.text, required this.onTap, this.isDestructiveAction = false}); + + /// The text of the action. + final String text; + + /// The callback that is called when the action is tapped. + final VoidCallback onTap; + + /// Whether the action is a destructive action. This is only used on iOS. + final bool isDestructiveAction; +} From 25099acce752e7a37933dc183d57888b1e718fe5 Mon Sep 17 00:00:00 2001 From: Niels Rijnberg Date: Tue, 25 Feb 2025 10:26:07 +0100 Subject: [PATCH 2/2] Format --- lib/ui/native_dialog.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ui/native_dialog.dart b/lib/ui/native_dialog.dart index 5c9f34a..dd130a5 100644 --- a/lib/ui/native_dialog.dart +++ b/lib/ui/native_dialog.dart @@ -60,7 +60,11 @@ void showNativeDialog( /// A dialog action which is used to show the actions of a native dialog. class DialogAction { /// Creates a [DialogAction]. - const DialogAction({required this.text, required this.onTap, this.isDestructiveAction = false}); + const DialogAction({ + required this.text, + required this.onTap, + this.isDestructiveAction = false, + }); /// The text of the action. final String text;