2023-11-26 05:21:35 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:contextmenu/contextmenu.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-11-26 22:42:05 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-11-26 05:21:35 +01:00
|
|
|
|
2023-11-26 22:42:05 +01:00
|
|
|
import 'package:adguard_home_manager/widgets/custom_list_tile_dialog.dart';
|
2023-11-26 05:21:35 +01:00
|
|
|
import 'package:adguard_home_manager/widgets/custom_list_tile.dart';
|
|
|
|
|
|
|
|
import 'package:adguard_home_manager/models/menu_option.dart';
|
|
|
|
|
|
|
|
class OptionsMenu extends StatelessWidget {
|
|
|
|
final Widget child;
|
2023-12-17 22:09:13 +01:00
|
|
|
final List<MenuOption> Function(dynamic) options;
|
2023-11-26 05:21:35 +01:00
|
|
|
final dynamic value;
|
|
|
|
final BorderRadius? borderRadius;
|
|
|
|
final void Function(dynamic)? onTap;
|
|
|
|
|
|
|
|
const OptionsMenu({
|
|
|
|
super.key,
|
|
|
|
required this.child,
|
|
|
|
required this.options,
|
|
|
|
this.value,
|
|
|
|
this.borderRadius,
|
|
|
|
this.onTap,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
void openOptionsModal() {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
2023-11-26 22:42:05 +01:00
|
|
|
builder: (context) => _OptionsModal(
|
2023-11-26 05:21:35 +01:00
|
|
|
options: options,
|
|
|
|
value: value
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
child: ContextMenuArea(
|
2023-12-17 22:09:13 +01:00
|
|
|
builder: (context) => options(value).map((opt) => CustomListTile(
|
2023-11-26 05:21:35 +01:00
|
|
|
title: opt.title,
|
|
|
|
icon: opt.icon,
|
|
|
|
onTap: () {
|
2023-12-17 22:09:13 +01:00
|
|
|
opt.action();
|
2023-11-26 05:21:35 +01:00
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
)).toList(),
|
|
|
|
child: InkWell(
|
|
|
|
onTap: onTap != null
|
|
|
|
? () => onTap!(value)
|
|
|
|
: null,
|
|
|
|
onLongPress: (Platform.isAndroid || Platform.isIOS)
|
|
|
|
? () => openOptionsModal()
|
|
|
|
: null,
|
|
|
|
borderRadius: borderRadius,
|
|
|
|
child: child,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-11-26 22:42:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class _OptionsModal extends StatelessWidget {
|
2023-12-17 22:09:13 +01:00
|
|
|
final List<MenuOption> Function(dynamic) options;
|
2023-11-26 22:42:05 +01:00
|
|
|
final dynamic value;
|
|
|
|
|
|
|
|
const _OptionsModal({
|
|
|
|
required this.options,
|
|
|
|
this.value,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(vertical: 16),
|
2024-03-09 20:41:49 +01:00
|
|
|
scrollable: true,
|
2023-11-26 22:42:05 +01:00
|
|
|
title: Column(
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
Icons.more_horiz,
|
|
|
|
size: 24,
|
|
|
|
color: Theme.of(context).listTileTheme.iconColor
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context)!.options,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
content: ConstrainedBox(
|
|
|
|
constraints: const BoxConstraints(
|
2024-03-09 20:41:49 +01:00
|
|
|
maxWidth: 500
|
2023-11-26 22:42:05 +01:00
|
|
|
),
|
2024-03-09 20:41:49 +01:00
|
|
|
child: Column(
|
|
|
|
children: options(value).map((opt) => CustomListTileDialog(
|
|
|
|
title: opt.title,
|
|
|
|
icon: opt.icon,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
opt.action();
|
|
|
|
},
|
|
|
|
)).toList()
|
2023-11-26 22:42:05 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
child: Text(AppLocalizations.of(context)!.cancel)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
2023-11-26 05:21:35 +01:00
|
|
|
}
|