1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-22 13:25:13 +01:00
git-touch-android-ios-app/lib/widgets/action_button.dart

129 lines
3.9 KiB
Dart
Raw Normal View History

2019-02-20 09:31:22 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2019-11-02 16:36:49 +01:00
import 'package:git_touch/screens/issue.dart';
import 'package:git_touch/screens/repository.dart';
import 'package:git_touch/screens/user.dart';
2019-09-30 09:46:06 +02:00
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:git_touch/models/theme.dart';
2019-09-30 09:46:06 +02:00
import 'package:share/share.dart';
2019-02-20 09:31:22 +01:00
2019-09-30 09:46:06 +02:00
class ActionItem {
2019-02-20 09:31:22 +01:00
String text;
2019-11-02 16:36:49 +01:00
void Function(BuildContext context) onPress;
IconData iconData;
ActionItem({
@required this.text,
@required this.onPress,
this.iconData,
});
2019-02-20 09:31:22 +01:00
2019-09-30 09:46:06 +02:00
ActionItem.share(String url)
: text = 'Share',
2019-11-02 16:36:49 +01:00
onPress = ((_) {
2019-09-30 09:46:06 +02:00
Share.share(url);
});
ActionItem.launch(String url)
: text = 'Open in Browser',
2019-11-02 16:36:49 +01:00
onPress = ((_) {
2019-09-30 09:46:06 +02:00
launchUrl(url);
});
2019-11-02 16:36:49 +01:00
ActionItem.user(String login)
: text = '@$login',
onPress = ((context) {
Provider.of<ThemeModel>(context)
.pushRoute(context, (_) => UserScreen(login));
});
ActionItem.repository(String owner, String name)
: text = '$owner/$name',
onPress = ((context) {
Provider.of<ThemeModel>(context)
.pushRoute(context, (_) => RepositoryScreen(owner, name));
});
2019-11-03 16:13:37 +01:00
ActionItem.issue(String owner, String name, int number,
{isPullRequest = false})
: text = (isPullRequest ? 'Pull Request' : 'Issue') + ' #$number',
2019-11-02 16:36:49 +01:00
onPress = ((context) {
2019-11-03 16:13:37 +01:00
Provider.of<ThemeModel>(context).pushRoute(
context,
(_) => IssueScreen(owner, name, number,
isPullRequest: isPullRequest));
2019-11-02 16:36:49 +01:00
});
2019-02-20 09:31:22 +01:00
}
class ActionButton extends StatelessWidget {
final String title;
2019-09-30 09:46:06 +02:00
final List<ActionItem> items;
2019-02-20 09:31:22 +01:00
final IconData iconData;
2019-10-02 08:58:11 +02:00
final int selected;
2019-02-20 09:31:22 +01:00
ActionButton({
@required this.title,
2019-09-30 09:46:06 +02:00
@required this.items,
2019-02-20 09:31:22 +01:00
this.iconData = Icons.more_vert,
2019-10-02 08:58:11 +02:00
this.selected,
2019-02-20 09:31:22 +01:00
});
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
return GestureDetector(
child: Icon(iconData, size: 22),
2019-02-20 09:31:22 +01:00
onTap: () async {
2019-09-30 09:46:06 +02:00
var value = await showCupertinoModalPopup<int>(
2019-02-20 09:31:22 +01:00
context: context,
builder: (BuildContext context) {
return CupertinoActionSheet(
title: Text(title),
2019-09-30 09:46:06 +02:00
actions: items.asMap().entries.map((entry) {
2019-02-20 09:31:22 +01:00
return CupertinoActionSheetAction(
2019-10-02 08:58:11 +02:00
child: Text(
entry.value.text,
style: TextStyle(
fontWeight: selected == entry.key
? FontWeight.w500
: FontWeight.w400),
),
2019-02-20 09:31:22 +01:00
onPressed: () {
Navigator.pop(context, entry.key);
},
);
}).toList(),
cancelButton: CupertinoActionSheetAction(
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
);
2019-09-30 09:46:06 +02:00
if (value != null) {
2019-11-02 16:36:49 +01:00
items[value].onPress(context);
2019-09-30 09:46:06 +02:00
}
2019-02-20 09:31:22 +01:00
},
);
default:
return PopupMenuButton(
icon: Icon(iconData),
2019-10-02 08:58:11 +02:00
initialValue: selected,
itemBuilder: (context) {
return items.asMap().entries.map((entry) {
return PopupMenuItem(
value: entry.key,
child: Text(entry.value.text),
);
}).toList();
},
2019-10-02 08:58:11 +02:00
onSelected: (value) {
2019-11-02 16:36:49 +01:00
items[value].onPress(context);
},
);
2019-02-20 09:31:22 +01:00
}
}
}