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

96 lines
2.7 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-09-27 15:39:36 +02:00
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:git_touch/models/theme.dart';
2019-02-20 09:31:22 +01:00
2019-06-20 16:59:13 +02:00
class MyAction {
2019-02-20 09:31:22 +01:00
String text;
Function onPress;
2019-06-20 16:59:13 +02:00
MyAction({@required this.text, @required this.onPress});
2019-02-20 09:31:22 +01:00
}
class ActionButton extends StatelessWidget {
final String title;
2019-06-20 16:59:13 +02:00
final List<MyAction> actions;
2019-02-20 09:31:22 +01:00
final IconData iconData;
ActionButton({
@required this.title,
@required this.actions,
this.iconData = Icons.more_vert,
});
void _onSelected(int value) {
if (value != null) {
actions[value].onPress();
}
}
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
2019-02-20 09:31:22 +01:00
return GestureDetector(
child: Icon(iconData, size: 24),
onTap: () async {
int value = await showCupertinoModalPopup<int>(
context: context,
builder: (BuildContext context) {
return CupertinoActionSheet(
title: Text(title),
actions: actions.asMap().entries.map((entry) {
return CupertinoActionSheetAction(
child: Text(entry.value.text),
onPressed: () {
Navigator.pop(context, entry.key);
},
);
}).toList(),
cancelButton: CupertinoActionSheetAction(
child: const Text('Cancel'),
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
),
);
},
);
_onSelected(value);
},
);
default:
2019-09-27 15:39:36 +02:00
return IconButton(
2019-02-20 09:31:22 +01:00
icon: Icon(iconData),
2019-09-27 15:39:36 +02:00
onPressed: () async {
await showModalBottomSheet(
context: context,
builder: (_) {
return Column(
children: join(
PopupMenuDivider(height: 1),
actions.asMap().entries.map((entry) {
return GestureDetector(
child: PopupMenuItem(
value: entry.key,
child: Text(entry.value.text),
),
onTap: () {
Navigator.of(context).pop();
_onSelected(entry.key);
},
);
}).toList(),
),
);
},
);
2019-02-20 09:31:22 +01:00
},
);
}
}
}