Skip to content

Commit 964bc88

Browse files
committed
Add native dialog to toolkit
1 parent b6c2771 commit 964bc88

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

lib/ui/native_dialog.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:flutter/material.dart';
4+
5+
/// A native dialog which shows a [CupertinoAlertDialog] on iOS and a [AlertDialog] on Android.
6+
///
7+
/// Example:
8+
/// ```dart
9+
/// showNativeDialog(
10+
/// context,
11+
/// title: 'Title',
12+
/// content: 'Content',
13+
/// actions: [
14+
/// DialogAction(text: 'Action', onTap: () {}),
15+
/// ],
16+
/// );
17+
/// ```
18+
void showNativeDialog(
19+
BuildContext context, {
20+
required String title,
21+
required List<DialogAction> actions,
22+
required String content,
23+
}) {
24+
if (defaultTargetPlatform == TargetPlatform.iOS) {
25+
showCupertinoDialog<void>(
26+
context: context,
27+
builder: (context) => CupertinoAlertDialog(
28+
title: Text(title),
29+
content: Text(content),
30+
actions: actions
31+
.map(
32+
(action) => CupertinoDialogAction(
33+
onPressed: action.onTap,
34+
isDestructiveAction: action.isDestructiveAction,
35+
child: Text(action.text),
36+
),
37+
)
38+
.toList(),
39+
),
40+
);
41+
} else {
42+
showDialog<void>(
43+
context: context,
44+
builder: (context) => AlertDialog(
45+
title: Text(title),
46+
content: Text(content),
47+
actions: actions
48+
.map(
49+
(action) => TextButton(
50+
onPressed: action.onTap,
51+
child: Text(action.text),
52+
),
53+
)
54+
.toList(),
55+
),
56+
);
57+
}
58+
}
59+
60+
/// A dialog action which is used to show the actions of a native dialog.
61+
class DialogAction {
62+
/// Creates a [DialogAction].
63+
const DialogAction({required this.text, required this.onTap, this.isDestructiveAction = false});
64+
65+
/// The text of the action.
66+
final String text;
67+
68+
/// The callback that is called when the action is tapped.
69+
final VoidCallback onTap;
70+
71+
/// Whether the action is a destructive action. This is only used on iOS.
72+
final bool isDestructiveAction;
73+
}

0 commit comments

Comments
 (0)