Skip to content

Commit 3c558c7

Browse files
committed
Refactor
1 parent 89dd416 commit 3c558c7

File tree

4 files changed

+113
-74
lines changed

4 files changed

+113
-74
lines changed

lib/adaptive_dialog.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export 'src/adaptive_style.dart';
2-
export 'src/alert_dialog.dart';
2+
export 'src/alert_dialog/alert_dialog.dart';
33
export 'src/modal_action_sheet/modal_action_sheet.dart';

lib/src/alert_dialog.dart renamed to lib/src/alert_dialog/alert_dialog.dart

Lines changed: 13 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import 'package:flutter/cupertino.dart';
33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart';
55

6-
import 'extensions/extensions.dart';
6+
import '../extensions/extensions.dart';
7+
import 'alert_dialog_action.dart';
8+
9+
export 'alert_dialog_action.dart';
710

811
/// Show alert dialog, whose appearance is adaptive according to platform
912
///
@@ -28,21 +31,8 @@ Future<T> showAlertDialog<T>({
2831
context: context,
2932
title: title,
3033
message: message,
31-
cancelLabel: actions
32-
.firstWhere(
33-
(a) => a.key == OkCancelResult.cancel,
34-
orElse: () => null,
35-
)
36-
?.label,
37-
actions: actions
38-
.where((a) => a.key != OkCancelResult.cancel)
39-
.map((a) => SheetAction(
40-
key: a.key,
41-
label: a.label,
42-
isDefaultAction: a.isDefaultAction,
43-
isDestructiveAction: a.isDestructiveAction,
44-
))
45-
.toList(),
34+
cancelLabel: actions.findCancelLabel(),
35+
actions: actions.convertToSheetActions(),
4636
style: style,
4737
);
4838
}
@@ -54,17 +44,9 @@ Future<T> showAlertDialog<T>({
5444
builder: (context) => CupertinoAlertDialog(
5545
title: titleText,
5646
content: messageText,
57-
actions: actions
58-
.map(
59-
(a) => CupertinoDialogAction(
60-
child: Text(a.label),
61-
isDefaultAction: a.isDefaultAction,
62-
isDestructiveAction: a.isDestructiveAction,
63-
textStyle: a.textStyle,
64-
onPressed: () => pop(a.key),
65-
),
66-
)
67-
.toList(),
47+
actions: actions.convertToCupertinoDialogActions(
48+
onPressed: pop,
49+
),
6850
),
6951
)
7052
: showDialog(
@@ -73,19 +55,10 @@ Future<T> showAlertDialog<T>({
7355
builder: (context) => AlertDialog(
7456
title: titleText,
7557
content: messageText,
76-
actions: actions
77-
.map(
78-
(a) => FlatButton(
79-
child: Text(
80-
a.label,
81-
style: a.textStyle.copyWith(
82-
color: a.isDestructiveAction ? colorScheme.error : null,
83-
),
84-
),
85-
onPressed: () => pop(a.key),
86-
),
87-
)
88-
.toList(),
58+
actions: actions.convertToMaterialDialogActions(
59+
onPressed: pop,
60+
destructiveColor: colorScheme.error,
61+
),
8962
),
9063
);
9164
}
@@ -171,40 +144,8 @@ Future<OkCancelResult> showOkAlertDialog({
171144
return result ?? OkCancelResult.cancel;
172145
}
173146

174-
/// Used for specifying [showAlertDialog]'s actions.
175-
@immutable
176-
class AlertDialogAction<T> {
177-
const AlertDialogAction({
178-
this.key,
179-
@required this.label,
180-
this.isDefaultAction = false,
181-
this.isDestructiveAction = false,
182-
this.textStyle = const TextStyle(),
183-
});
184-
185-
final T key;
186-
final String label;
187-
188-
/// Make font weight to bold(Only works for CupertinoStyle).
189-
final bool isDefaultAction;
190-
191-
/// Make font color to destructive/error color(red).
192-
final bool isDestructiveAction;
193-
194-
/// Change textStyle to another from default.
195-
///
196-
/// Recommended to keep null.
197-
final TextStyle textStyle;
198-
}
199-
200147
// Used to specify [showOkCancelAlertDialog]'s [defaultType]
201148
enum OkCancelAlertDefaultType {
202149
ok,
203150
cancel,
204151
}
205-
206-
// Result type of [showOkAlertDialog] or [showOkCancelAlertDialog].
207-
enum OkCancelResult {
208-
ok,
209-
cancel,
210-
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import 'package:adaptive_dialog/src/modal_action_sheet/sheet_action.dart';
2+
import 'package:flutter/cupertino.dart';
3+
import 'package:flutter/material.dart';
4+
5+
typedef ActionCallback<T> = void Function(T key);
6+
7+
/// Used for specifying showAlertDialog's actions.
8+
@immutable
9+
class AlertDialogAction<T> {
10+
const AlertDialogAction({
11+
this.key,
12+
@required this.label,
13+
this.isDefaultAction = false,
14+
this.isDestructiveAction = false,
15+
this.textStyle = const TextStyle(),
16+
});
17+
18+
final T key;
19+
final String label;
20+
21+
/// Make font weight to bold(Only works for CupertinoStyle).
22+
final bool isDefaultAction;
23+
24+
/// Make font color to destructive/error color(red).
25+
final bool isDestructiveAction;
26+
27+
/// Change textStyle to another from default.
28+
///
29+
/// Recommended to keep null.
30+
final TextStyle textStyle;
31+
}
32+
33+
extension AlertDialogActionEx<T> on AlertDialogAction<T> {
34+
Widget convertToCupertinoDialogAction({
35+
@required ActionCallback<T> onPressed,
36+
}) {
37+
return CupertinoDialogAction(
38+
child: Text(label),
39+
isDefaultAction: isDefaultAction,
40+
isDestructiveAction: isDestructiveAction,
41+
textStyle: textStyle,
42+
onPressed: () => onPressed(key),
43+
);
44+
}
45+
46+
Widget convertToMaterialDialogAction({
47+
@required ActionCallback<T> onPressed,
48+
@required Color destructiveColor,
49+
}) {
50+
return FlatButton(
51+
child: Text(
52+
label,
53+
style: textStyle.copyWith(
54+
color: isDestructiveAction ? destructiveColor : null,
55+
),
56+
),
57+
onPressed: () => onPressed(key),
58+
);
59+
}
60+
}
61+
62+
extension AlertDialogActionListEx<T> on List<AlertDialogAction<T>> {
63+
List<Widget> convertToCupertinoDialogActions({
64+
@required ActionCallback<T> onPressed,
65+
}) =>
66+
map((a) => a.convertToCupertinoDialogAction(
67+
onPressed: onPressed,
68+
)).toList();
69+
70+
List<Widget> convertToMaterialDialogActions({
71+
@required ActionCallback<T> onPressed,
72+
@required Color destructiveColor,
73+
}) =>
74+
map((a) => a.convertToMaterialDialogAction(
75+
onPressed: onPressed,
76+
destructiveColor: destructiveColor,
77+
)).toList();
78+
79+
List<SheetAction<T>> convertToSheetActions() =>
80+
where((a) => a.key != OkCancelResult.cancel)
81+
.map((a) => SheetAction(
82+
key: a.key,
83+
label: a.label,
84+
isDefaultAction: a.isDefaultAction,
85+
isDestructiveAction: a.isDestructiveAction,
86+
))
87+
.toList();
88+
89+
String findCancelLabel() => firstWhere(
90+
(a) => a.key == OkCancelResult.cancel,
91+
orElse: () => null,
92+
)?.label;
93+
}
94+
95+
// Result type of [showOkAlertDialog] or [showOkCancelAlertDialog].
96+
enum OkCancelResult {
97+
ok,
98+
cancel,
99+
}

lib/src/modal_action_sheet/modal_action_sheet.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:adaptive_dialog/adaptive_dialog.dart';
2-
import 'package:adaptive_dialog/src/extensions/extensions.dart';
32
import 'package:adaptive_dialog/src/modal_action_sheet/material_modal_action_sheet.dart';
43
import 'package:flutter/cupertino.dart';
54
import 'package:flutter/material.dart';

0 commit comments

Comments
 (0)