Skip to content

Commit cedf2af

Browse files
committed
Refactor
1 parent 8f99e98 commit cedf2af

File tree

4 files changed

+153
-150
lines changed

4 files changed

+153
-150
lines changed
Lines changed: 3 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,4 @@
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-
91
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';
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'package:adaptive_dialog/adaptive_dialog.dart';
2+
import 'package:flutter/cupertino.dart';
3+
import 'package:flutter/material.dart';
4+
5+
/// Show alert dialog, whose appearance is adaptive according to platform
6+
///
7+
/// [useActionSheetForCupertino] (default: false) only works for
8+
/// cupertino style. If it is set to true, [showModalActionSheet] is called
9+
/// instead.
10+
Future<T> showAlertDialog<T>({
11+
@required BuildContext context,
12+
String title,
13+
String message,
14+
List<AlertDialogAction<T>> actions = const [],
15+
bool barrierDismissible = true,
16+
AdaptiveStyle style = AdaptiveStyle.adaptive,
17+
bool useActionSheetForCupertino = false,
18+
}) {
19+
void pop(T key) => Navigator.of(context).pop(key);
20+
final theme = Theme.of(context);
21+
final colorScheme = theme.colorScheme;
22+
final isCupertinoStyle = style.isCupertinoStyle(theme);
23+
if (isCupertinoStyle && useActionSheetForCupertino) {
24+
return showModalActionSheet(
25+
context: context,
26+
title: title,
27+
message: message,
28+
cancelLabel: actions.findCancelLabel(),
29+
actions: actions.convertToSheetActions(),
30+
style: style,
31+
);
32+
}
33+
final titleText = title == null ? null : Text(title);
34+
final messageText = message == null ? null : Text(message);
35+
return style.isCupertinoStyle(theme)
36+
? showCupertinoDialog(
37+
context: context,
38+
builder: (context) => CupertinoAlertDialog(
39+
title: titleText,
40+
content: messageText,
41+
actions: actions.convertToCupertinoDialogActions(
42+
onPressed: pop,
43+
),
44+
),
45+
)
46+
: showDialog(
47+
context: context,
48+
barrierDismissible: barrierDismissible,
49+
builder: (context) => AlertDialog(
50+
title: titleText,
51+
content: messageText,
52+
actions: actions.convertToMaterialDialogActions(
53+
onPressed: pop,
54+
destructiveColor: colorScheme.error,
55+
),
56+
),
57+
);
58+
}
59+
60+
// Used to specify [showOkCancelAlertDialog]'s [defaultType]
61+
enum OkCancelAlertDefaultType {
62+
ok,
63+
cancel,
64+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:adaptive_dialog/adaptive_dialog.dart';
2+
import 'package:flutter/material.dart';
3+
4+
/// Show OK alert dialog, whose appearance is adaptive according to platform
5+
///
6+
/// This is convenient wrapper of [showAlertDialog].
7+
/// [barrierDismissible] (default: true) only works for material style,
8+
/// and if it is set to false, pressing OK button is only way to close alert.
9+
Future<OkCancelResult> showOkAlertDialog({
10+
@required BuildContext context,
11+
String title,
12+
String message,
13+
String okLabel,
14+
bool barrierDismissible = true,
15+
AdaptiveStyle alertStyle = AdaptiveStyle.adaptive,
16+
bool useActionSheetForCupertino = false,
17+
}) async {
18+
final result = await showAlertDialog<OkCancelResult>(
19+
context: context,
20+
title: title,
21+
message: message,
22+
barrierDismissible: barrierDismissible,
23+
style: alertStyle,
24+
useActionSheetForCupertino: useActionSheetForCupertino,
25+
actions: [
26+
AlertDialogAction(
27+
label: okLabel ?? MaterialLocalizations.of(context).okButtonLabel,
28+
key: OkCancelResult.ok,
29+
)
30+
],
31+
);
32+
return result ?? OkCancelResult.cancel;
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:adaptive_dialog/adaptive_dialog.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:adaptive_dialog/src/extensions/extensions.dart';
4+
5+
/// Show OK/Cancel alert dialog, whose appearance is adaptive according to platform
6+
///
7+
/// This is convenient wrapper of [showAlertDialog].
8+
/// [barrierDismissible] (default: true) only works for material style,
9+
/// and if it is set to false, pressing OK or Cancel buttons is only way to
10+
/// close alert.
11+
/// [defaultType] only works for cupertino style and if it is specified
12+
/// OK or Cancel button label will be changed to bold.
13+
Future<OkCancelResult> showOkCancelAlertDialog({
14+
@required BuildContext context,
15+
String title,
16+
String message,
17+
String okLabel,
18+
String cancelLabel,
19+
OkCancelAlertDefaultType defaultType,
20+
bool isDestructiveAction = false,
21+
bool barrierDismissible = true,
22+
AdaptiveStyle alertStyle = AdaptiveStyle.adaptive,
23+
bool useActionSheetForCupertino = false,
24+
}) async {
25+
final isCupertinoStyle = Theme.of(context).isCupertinoStyle;
26+
String defaultCancelLabel() {
27+
final label = MaterialLocalizations.of(context).cancelButtonLabel;
28+
return isCupertinoStyle ? label.capitalizedForce : label;
29+
}
30+
31+
final result = await showAlertDialog<OkCancelResult>(
32+
context: context,
33+
title: title,
34+
message: message,
35+
barrierDismissible: barrierDismissible,
36+
style: alertStyle,
37+
useActionSheetForCupertino: useActionSheetForCupertino,
38+
actions: [
39+
AlertDialogAction(
40+
label: cancelLabel ?? defaultCancelLabel(),
41+
key: OkCancelResult.cancel,
42+
isDefaultAction: defaultType == OkCancelAlertDefaultType.cancel,
43+
),
44+
AlertDialogAction(
45+
label: okLabel ?? MaterialLocalizations.of(context).okButtonLabel,
46+
key: OkCancelResult.ok,
47+
isDefaultAction: defaultType == OkCancelAlertDefaultType.ok,
48+
isDestructiveAction: isDestructiveAction,
49+
),
50+
],
51+
);
52+
return result ?? OkCancelResult.cancel;
53+
}

0 commit comments

Comments
 (0)