1
+ import 'dart:async' ;
2
+
1
3
import 'package:flutter/cupertino.dart' ;
2
4
import 'package:flutter/foundation.dart' ;
3
5
import 'package:flutter/material.dart' ;
@@ -15,14 +17,14 @@ import 'package:flutter/material.dart';
15
17
/// ],
16
18
/// );
17
19
/// ```
18
- void showNativeDialog (
20
+ Future < T ?> showNativeDialog < T > (
19
21
BuildContext context, {
20
22
required String title,
21
- required List <DialogAction > actions,
23
+ required List <DialogAction < T > > actions,
22
24
required String content,
23
25
}) {
24
26
if (defaultTargetPlatform == TargetPlatform .iOS) {
25
- showCupertinoDialog <void >(
27
+ return showCupertinoDialog <T >(
26
28
context: context,
27
29
builder:
28
30
(dialogContext) => CupertinoAlertDialog (
@@ -32,9 +34,9 @@ void showNativeDialog(
32
34
actions
33
35
.map (
34
36
(action) => CupertinoDialogAction (
35
- onPressed: () {
36
- action.onTap ();
37
- Navigator .of (dialogContext).pop ();
37
+ onPressed: () async {
38
+ final result = await action.onTap ();
39
+ if (dialogContext.mounted) Navigator .of (dialogContext).pop (result );
38
40
},
39
41
isDestructiveAction: action.isDestructiveAction,
40
42
child: Text (action.text),
@@ -44,7 +46,7 @@ void showNativeDialog(
44
46
),
45
47
);
46
48
} else {
47
- showDialog <void >(
49
+ return showDialog <T ? >(
48
50
context: context,
49
51
builder:
50
52
(dialogContext) => AlertDialog (
@@ -54,9 +56,9 @@ void showNativeDialog(
54
56
actions
55
57
.map (
56
58
(action) => TextButton (
57
- onPressed: () {
58
- action.onTap ();
59
- Navigator .of (dialogContext).pop ();
59
+ onPressed: () async {
60
+ final result = await action.onTap ();
61
+ if (dialogContext.mounted) Navigator .of (dialogContext).pop (result );
60
62
},
61
63
child: Text (action.text),
62
64
),
@@ -68,15 +70,15 @@ void showNativeDialog(
68
70
}
69
71
70
72
/// A dialog action which is used to show the actions of a native dialog. Tapping a action will also close the dialog.
71
- class DialogAction {
73
+ class DialogAction < T > {
72
74
/// Creates a [DialogAction] .
73
75
const DialogAction ({required this .text, required this .onTap, this .isDestructiveAction = false });
74
76
75
77
/// The text of the action.
76
78
final String text;
77
79
78
80
/// The callback that is called when the action is tapped.
79
- final VoidCallback onTap;
81
+ final FutureOr < T ?> Function () onTap;
80
82
81
83
/// Whether the action is a destructive action. This is only used on iOS.
82
84
final bool isDestructiveAction;
0 commit comments