Skip to content

Commit 89dd416

Browse files
committed
Refactor
1 parent f8f71e9 commit 89dd416

File tree

6 files changed

+181
-124
lines changed

6 files changed

+181
-124
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';
22
export 'src/alert_dialog.dart';
3-
export 'src/modal_action_sheet.dart';
3+
export 'src/modal_action_sheet/modal_action_sheet.dart';

lib/src/modal_action_sheet.dart

Lines changed: 0 additions & 123 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:adaptive_dialog/src/extensions/extensions.dart';
4+
import 'sheet_action.dart';
5+
6+
class CupertinoModalActionSheet<T> extends StatelessWidget {
7+
const CupertinoModalActionSheet({
8+
Key key,
9+
this.title,
10+
this.message,
11+
this.actions,
12+
this.cancelLabel,
13+
}) : super(key: key);
14+
15+
final String title;
16+
final String message;
17+
final List<SheetAction<T>> actions;
18+
final String cancelLabel;
19+
20+
@override
21+
Widget build(BuildContext context) {
22+
void pop(T key) => Navigator.of(context).pop(key);
23+
return CupertinoActionSheet(
24+
title: title == null ? null : Text(title),
25+
message: message == null ? null : Text(message),
26+
cancelButton: CupertinoActionSheetAction(
27+
child: Text(
28+
cancelLabel ??
29+
MaterialLocalizations.of(context)
30+
.cancelButtonLabel
31+
.capitalizedForce,
32+
),
33+
isDefaultAction: !actions.any((a) => a.isDefaultAction),
34+
onPressed: () => pop(null),
35+
),
36+
actions: actions
37+
.map((a) => CupertinoActionSheetAction(
38+
child: Text(a.label),
39+
isDestructiveAction: a.isDestructiveAction,
40+
isDefaultAction: a.isDefaultAction,
41+
onPressed: () => pop(a.key),
42+
))
43+
.toList(),
44+
);
45+
}
46+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:flutter/material.dart';
2+
import 'sheet_action.dart';
3+
4+
class MaterialModalActionSheet<T> extends StatelessWidget {
5+
const MaterialModalActionSheet({
6+
Key key,
7+
this.title,
8+
this.message,
9+
this.actions,
10+
this.cancelLabel,
11+
}) : super(key: key);
12+
13+
final String title;
14+
final String message;
15+
final List<SheetAction<T>> actions;
16+
final String cancelLabel;
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
final theme = Theme.of(context);
21+
final colorScheme = theme.colorScheme;
22+
return SafeArea(
23+
child: Column(
24+
mainAxisSize: MainAxisSize.min,
25+
children: [
26+
if (title != null && message == null)
27+
ListTile(
28+
title: Text(title),
29+
dense: true,
30+
),
31+
if (message != null) ...[
32+
ListTile(
33+
title: Text(title),
34+
subtitle: Text(message),
35+
),
36+
const Divider()
37+
],
38+
...actions.map((a) {
39+
final icon = a.icon;
40+
final color = a.isDestructiveAction ? colorScheme.error : null;
41+
return ListTile(
42+
leading: icon == null
43+
? null
44+
: Icon(
45+
icon,
46+
color: color,
47+
),
48+
title: Text(
49+
a.label,
50+
style: TextStyle(
51+
color: color,
52+
),
53+
),
54+
onTap: () => Navigator.of(context).pop(a.key),
55+
);
56+
}),
57+
],
58+
),
59+
);
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:adaptive_dialog/adaptive_dialog.dart';
2+
import 'package:adaptive_dialog/src/extensions/extensions.dart';
3+
import 'package:adaptive_dialog/src/modal_action_sheet/material_modal_action_sheet.dart';
4+
import 'package:flutter/cupertino.dart';
5+
import 'package:flutter/material.dart';
6+
7+
import 'cupertino_modal_action_sheet.dart';
8+
9+
export 'sheet_action.dart';
10+
11+
/// Show modal action sheet, whose appearance is adaptive according to platform
12+
13+
/// The [isDismissible] parameter only works for material style and it specifies
14+
/// whether the bottom sheet will be dismissed when user taps on the scrim.
15+
Future<T> showModalActionSheet<T>({
16+
@required BuildContext context,
17+
String title,
18+
String message,
19+
List<SheetAction<T>> actions = const [],
20+
String cancelLabel,
21+
AdaptiveStyle style = AdaptiveStyle.adaptive,
22+
bool isDismissible = true,
23+
}) {
24+
final theme = Theme.of(context);
25+
return style.isCupertinoStyle(theme)
26+
? showCupertinoModalPopup(
27+
context: context,
28+
builder: (context) => CupertinoModalActionSheet(
29+
title: title,
30+
message: message,
31+
actions: actions,
32+
cancelLabel: cancelLabel,
33+
),
34+
)
35+
: showModalBottomSheet(
36+
context: context,
37+
isDismissible: isDismissible,
38+
builder: (context) => MaterialModalActionSheet(
39+
title: title,
40+
message: message,
41+
actions: actions,
42+
cancelLabel: cancelLabel,
43+
),
44+
);
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter/widgets.dart';
2+
import 'package:meta/meta.dart';
3+
4+
/// Used for specifying showModalActionSheet's actions.
5+
@immutable
6+
class SheetAction<T> {
7+
const SheetAction({
8+
@required this.label,
9+
this.key,
10+
this.icon,
11+
this.isDefaultAction = false,
12+
this.isDestructiveAction = false,
13+
});
14+
15+
final String label;
16+
17+
/// Only works for Material Style
18+
final IconData icon;
19+
20+
/// Used for checking selection result
21+
final T key;
22+
23+
/// Make font weight to bold(Only works for CupertinoStyle).
24+
final bool isDefaultAction;
25+
26+
/// Make font color to destructive/error color(red).
27+
final bool isDestructiveAction;
28+
}

0 commit comments

Comments
 (0)