1
0
mirror of https://github.com/git-touch/git-touch synced 2025-02-02 17:07:06 +01:00

134 lines
3.8 KiB
Dart
Raw Normal View History

2019-02-20 16:31:22 +08:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2019-09-30 15:46:06 +08:00
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:git_touch/models/theme.dart';
2019-09-30 15:46:06 +08:00
import 'package:share/share.dart';
2019-02-20 16:31:22 +08:00
2019-09-30 15:46:06 +08:00
class ActionItem {
2021-05-16 15:16:35 +08:00
String? text;
String? url;
2020-02-01 18:30:32 +08:00
bool isDestructiveAction;
2021-05-16 15:16:35 +08:00
void Function(BuildContext context)? onTap;
IconData? iconData;
ActionItem({
2021-05-16 15:16:35 +08:00
required this.text,
2020-01-01 20:59:20 +08:00
this.onTap,
this.url,
this.iconData,
2020-02-01 18:30:32 +08:00
this.isDestructiveAction = false,
});
2019-02-20 16:31:22 +08:00
2021-05-16 15:16:35 +08:00
static List<ActionItem> getUrlActions(String? url) {
2020-01-27 13:41:17 +08:00
return [
ActionItem(
text: 'Share',
iconData: Octicons.rocket,
2020-01-27 13:41:17 +08:00
onTap: (_) {
2021-05-16 15:16:35 +08:00
Share.share(url!);
2020-01-27 13:41:17 +08:00
},
),
ActionItem(
text: 'Open in Browser',
iconData: Octicons.globe,
2020-01-27 13:41:17 +08:00
onTap: (_) {
2019-09-30 15:46:06 +08:00
launchUrl(url);
2020-01-27 13:41:17 +08:00
},
),
];
}
2019-02-20 16:31:22 +08:00
}
class ActionButton extends StatelessWidget {
final String title;
2019-09-30 15:46:06 +08:00
final List<ActionItem> items;
2019-02-20 16:31:22 +08:00
final IconData iconData;
2021-05-16 15:16:35 +08:00
final int? selected;
2019-02-20 16:31:22 +08:00
ActionButton({
2021-05-16 15:16:35 +08:00
required this.title,
required this.items,
2021-02-14 22:17:22 +08:00
this.iconData = Ionicons.ellipsis_horizontal,
2019-10-02 14:58:11 +08:00
this.selected,
2019-02-20 16:31:22 +08:00
});
@override
Widget build(BuildContext context) {
2020-01-01 20:59:20 +08:00
final theme = Provider.of<ThemeModel>(context);
switch (theme.theme) {
2019-09-19 21:10:50 +08:00
case AppThemeType.cupertino:
2020-01-04 22:47:48 +08:00
return CupertinoButton(
minSize: 0,
child: Icon(iconData, size: 22),
2020-01-04 22:47:48 +08:00
padding: EdgeInsets.zero,
onPressed: () async {
2019-09-30 15:46:06 +08:00
var value = await showCupertinoModalPopup<int>(
2019-02-20 16:31:22 +08:00
context: context,
builder: (BuildContext context) {
return CupertinoActionSheet(
title: Text(title),
2019-09-30 15:46:06 +08:00
actions: items.asMap().entries.map((entry) {
2019-02-20 16:31:22 +08:00
return CupertinoActionSheetAction(
child: Row(
children: [
Icon(entry.value.iconData),
SizedBox(width: 10),
Text(
2021-05-16 15:16:35 +08:00
entry.value.text!,
style: TextStyle(
fontWeight: selected == entry.key
? FontWeight.w500
: FontWeight.w400),
),
],
2019-10-02 14:58:11 +08:00
),
2019-02-20 16:31:22 +08:00
onPressed: () {
Navigator.pop(context, entry.key);
},
);
}).toList(),
cancelButton: CupertinoActionSheetAction(
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
);
2019-09-30 15:46:06 +08:00
if (value != null) {
2021-05-16 15:16:35 +08:00
if (items[value].onTap != null) items[value].onTap!(context);
2020-01-01 20:59:20 +08:00
if (items[value].url != null)
2021-05-16 15:16:35 +08:00
theme.push(context, items[value].url!);
2019-09-30 15:46:06 +08:00
}
2019-02-20 16:31:22 +08:00
},
);
default:
return PopupMenuButton(
icon: Icon(iconData),
2019-10-02 14:58:11 +08:00
initialValue: selected,
itemBuilder: (context) {
return items.asMap().entries.map((entry) {
return PopupMenuItem(
value: entry.key,
child: Row(
children: [
Icon(entry.value.iconData),
SizedBox(width: 10),
2021-05-16 15:16:35 +08:00
Text(entry.value.text!)
],
),
);
}).toList();
},
2021-05-16 15:16:35 +08:00
onSelected: (dynamic value) {
items[value].onTap!(context);
},
);
2019-02-20 16:31:22 +08:00
}
}
}