|
1 |
| -import 'package:adaptive_dialog/adaptive_dialog.dart'; |
2 |
| -import 'package:flutter/cupertino.dart'; |
3 |
| -import 'package:flutter/foundation.dart'; |
4 |
| -import 'package:flutter/material.dart'; |
5 |
| - |
6 |
| -import '../extensions/extensions.dart'; |
7 |
| -import 'alert_dialog_action.dart'; |
8 |
| - |
9 | 1 | export 'alert_dialog_action.dart';
|
10 |
| - |
11 |
| -/// Show alert dialog, whose appearance is adaptive according to platform |
12 |
| -/// |
13 |
| -/// [useActionSheetForCupertino] (default: false) only works for |
14 |
| -/// cupertino style. If it is set to true, [showModalActionSheet] is called |
15 |
| -/// instead. |
16 |
| -Future<T> showAlertDialog<T>({ |
17 |
| - @required BuildContext context, |
18 |
| - String title, |
19 |
| - String message, |
20 |
| - List<AlertDialogAction<T>> actions = const [], |
21 |
| - bool barrierDismissible = true, |
22 |
| - AdaptiveStyle style = AdaptiveStyle.adaptive, |
23 |
| - bool useActionSheetForCupertino = false, |
24 |
| -}) { |
25 |
| - void pop(T key) => Navigator.of(context).pop(key); |
26 |
| - final theme = Theme.of(context); |
27 |
| - final colorScheme = theme.colorScheme; |
28 |
| - final isCupertinoStyle = style.isCupertinoStyle(theme); |
29 |
| - if (isCupertinoStyle && useActionSheetForCupertino) { |
30 |
| - return showModalActionSheet( |
31 |
| - context: context, |
32 |
| - title: title, |
33 |
| - message: message, |
34 |
| - cancelLabel: actions.findCancelLabel(), |
35 |
| - actions: actions.convertToSheetActions(), |
36 |
| - style: style, |
37 |
| - ); |
38 |
| - } |
39 |
| - final titleText = title == null ? null : Text(title); |
40 |
| - final messageText = message == null ? null : Text(message); |
41 |
| - return style.isCupertinoStyle(theme) |
42 |
| - ? showCupertinoDialog( |
43 |
| - context: context, |
44 |
| - builder: (context) => CupertinoAlertDialog( |
45 |
| - title: titleText, |
46 |
| - content: messageText, |
47 |
| - actions: actions.convertToCupertinoDialogActions( |
48 |
| - onPressed: pop, |
49 |
| - ), |
50 |
| - ), |
51 |
| - ) |
52 |
| - : showDialog( |
53 |
| - context: context, |
54 |
| - barrierDismissible: barrierDismissible, |
55 |
| - builder: (context) => AlertDialog( |
56 |
| - title: titleText, |
57 |
| - content: messageText, |
58 |
| - actions: actions.convertToMaterialDialogActions( |
59 |
| - onPressed: pop, |
60 |
| - destructiveColor: colorScheme.error, |
61 |
| - ), |
62 |
| - ), |
63 |
| - ); |
64 |
| -} |
65 |
| - |
66 |
| -/// Show OK/Cancel alert dialog, whose appearance is adaptive according to platform |
67 |
| -/// |
68 |
| -/// This is convenient wrapper of [showAlertDialog]. |
69 |
| -/// [barrierDismissible] (default: true) only works for material style, |
70 |
| -/// and if it is set to false, pressing OK or Cancel buttons is only way to |
71 |
| -/// close alert. |
72 |
| -/// [defaultType] only works for cupertino style and if it is specified |
73 |
| -/// OK or Cancel button label will be changed to bold. |
74 |
| -Future<OkCancelResult> showOkCancelAlertDialog({ |
75 |
| - @required BuildContext context, |
76 |
| - String title, |
77 |
| - String message, |
78 |
| - String okLabel, |
79 |
| - String cancelLabel, |
80 |
| - OkCancelAlertDefaultType defaultType, |
81 |
| - bool isDestructiveAction = false, |
82 |
| - bool barrierDismissible = true, |
83 |
| - AdaptiveStyle alertStyle = AdaptiveStyle.adaptive, |
84 |
| - bool useActionSheetForCupertino = false, |
85 |
| -}) async { |
86 |
| - final isCupertinoStyle = Theme.of(context).isCupertinoStyle; |
87 |
| - String defaultCancelLabel() { |
88 |
| - final label = MaterialLocalizations.of(context).cancelButtonLabel; |
89 |
| - return isCupertinoStyle ? label.capitalizedForce : label; |
90 |
| - } |
91 |
| - |
92 |
| - final result = await showAlertDialog<OkCancelResult>( |
93 |
| - context: context, |
94 |
| - title: title, |
95 |
| - message: message, |
96 |
| - barrierDismissible: barrierDismissible, |
97 |
| - style: alertStyle, |
98 |
| - useActionSheetForCupertino: useActionSheetForCupertino, |
99 |
| - actions: [ |
100 |
| - AlertDialogAction( |
101 |
| - label: cancelLabel ?? defaultCancelLabel(), |
102 |
| - key: OkCancelResult.cancel, |
103 |
| - isDefaultAction: defaultType == OkCancelAlertDefaultType.cancel, |
104 |
| - ), |
105 |
| - AlertDialogAction( |
106 |
| - label: okLabel ?? MaterialLocalizations.of(context).okButtonLabel, |
107 |
| - key: OkCancelResult.ok, |
108 |
| - isDefaultAction: defaultType == OkCancelAlertDefaultType.ok, |
109 |
| - isDestructiveAction: isDestructiveAction, |
110 |
| - ), |
111 |
| - ], |
112 |
| - ); |
113 |
| - return result ?? OkCancelResult.cancel; |
114 |
| -} |
115 |
| - |
116 |
| -/// Show OK alert dialog, whose appearance is adaptive according to platform |
117 |
| -/// |
118 |
| -/// This is convenient wrapper of [showAlertDialog]. |
119 |
| -/// [barrierDismissible] (default: true) only works for material style, |
120 |
| -/// and if it is set to false, pressing OK button is only way to close alert. |
121 |
| -Future<OkCancelResult> showOkAlertDialog({ |
122 |
| - @required BuildContext context, |
123 |
| - String title, |
124 |
| - String message, |
125 |
| - String okLabel, |
126 |
| - bool barrierDismissible = true, |
127 |
| - AdaptiveStyle alertStyle = AdaptiveStyle.adaptive, |
128 |
| - bool useActionSheetForCupertino = false, |
129 |
| -}) async { |
130 |
| - final result = await showAlertDialog<OkCancelResult>( |
131 |
| - context: context, |
132 |
| - title: title, |
133 |
| - message: message, |
134 |
| - barrierDismissible: barrierDismissible, |
135 |
| - style: alertStyle, |
136 |
| - useActionSheetForCupertino: useActionSheetForCupertino, |
137 |
| - actions: [ |
138 |
| - AlertDialogAction( |
139 |
| - label: okLabel ?? MaterialLocalizations.of(context).okButtonLabel, |
140 |
| - key: OkCancelResult.ok, |
141 |
| - ) |
142 |
| - ], |
143 |
| - ); |
144 |
| - return result ?? OkCancelResult.cancel; |
145 |
| -} |
146 |
| - |
147 |
| -// Used to specify [showOkCancelAlertDialog]'s [defaultType] |
148 |
| -enum OkCancelAlertDefaultType { |
149 |
| - ok, |
150 |
| - cancel, |
151 |
| -} |
| 2 | +export 'show_alert_dialog.dart'; |
| 3 | +export 'show_ok_alert_dialog.dart'; |
| 4 | +export 'show_ok_cancel_alert_dialog.dart'; |
0 commit comments