2022-04-23 14:00:47 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
2021-02-22 12:16:23 +01:00
|
|
|
import 'in_app_webview/webview.dart';
|
2020-05-21 03:34:39 +02:00
|
|
|
import 'types.dart';
|
|
|
|
|
|
|
|
///Class that represents the WebView context menu. It used by [WebView.contextMenu].
|
|
|
|
///
|
|
|
|
///**NOTE**: To make it work properly on Android, JavaScript should be enabled!
|
|
|
|
class ContextMenu {
|
|
|
|
///Event fired when the context menu for this WebView is being built.
|
|
|
|
///
|
|
|
|
///[hitTestResult] represents the hit result for hitting an HTML elements.
|
2021-01-28 17:10:15 +01:00
|
|
|
final void Function(InAppWebViewHitTestResult hitTestResult)?
|
2020-05-29 19:56:03 +02:00
|
|
|
onCreateContextMenu;
|
2020-05-21 03:34:39 +02:00
|
|
|
|
|
|
|
///Event fired when the context menu for this WebView is being hidden.
|
2021-01-28 17:10:15 +01:00
|
|
|
final void Function()? onHideContextMenu;
|
2020-05-21 03:34:39 +02:00
|
|
|
|
|
|
|
///Event fired when a context menu item has been clicked.
|
|
|
|
///
|
|
|
|
///[contextMenuItemClicked] represents the [ContextMenuItem] clicked.
|
2021-01-28 17:10:15 +01:00
|
|
|
final void Function(ContextMenuItem contextMenuItemClicked)?
|
2020-05-29 19:56:03 +02:00
|
|
|
onContextMenuActionItemClicked;
|
2020-05-21 03:34:39 +02:00
|
|
|
|
2022-04-23 14:00:47 +02:00
|
|
|
///Use [settings] instead
|
|
|
|
@Deprecated("Use settings instead")
|
2021-01-28 17:10:15 +01:00
|
|
|
final ContextMenuOptions? options;
|
2020-05-30 20:23:33 +02:00
|
|
|
|
2022-04-23 14:00:47 +02:00
|
|
|
///Context menu settings.
|
|
|
|
final ContextMenuSettings? settings;
|
|
|
|
|
2020-05-21 03:34:39 +02:00
|
|
|
///List of the custom [ContextMenuItem].
|
2020-05-30 20:23:33 +02:00
|
|
|
final List<ContextMenuItem> menuItems;
|
2020-05-21 03:34:39 +02:00
|
|
|
|
2020-05-29 19:56:03 +02:00
|
|
|
ContextMenu(
|
2020-05-30 20:23:33 +02:00
|
|
|
{this.menuItems = const [],
|
2020-05-29 19:56:03 +02:00
|
|
|
this.onCreateContextMenu,
|
|
|
|
this.onHideContextMenu,
|
2020-05-30 20:23:33 +02:00
|
|
|
this.options,
|
2022-04-23 14:00:47 +02:00
|
|
|
this.settings,
|
2021-01-28 17:10:15 +01:00
|
|
|
this.onContextMenuActionItemClicked});
|
2020-05-21 03:34:39 +02:00
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
2021-01-28 17:10:15 +01:00
|
|
|
"menuItems": menuItems.map((menuItem) => menuItem.toMap()).toList(),
|
2022-04-23 14:00:47 +02:00
|
|
|
// ignore: deprecated_member_use_from_same_package
|
|
|
|
"settings": settings?.toMap() ?? options?.toMap()
|
2020-05-21 03:34:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return this.toMap();
|
|
|
|
}
|
2020-05-30 20:23:33 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toMap().toString();
|
|
|
|
}
|
2020-05-21 03:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///Class that represent an item of the [ContextMenu].
|
|
|
|
class ContextMenuItem {
|
2022-04-23 14:00:47 +02:00
|
|
|
///Use [id] instead.
|
|
|
|
@Deprecated("Use id instead")
|
2021-01-28 17:10:15 +01:00
|
|
|
int? androidId;
|
2020-05-29 19:56:03 +02:00
|
|
|
|
2022-04-23 14:00:47 +02:00
|
|
|
///Use [id] instead.
|
|
|
|
@Deprecated("Use id instead")
|
2021-01-28 17:10:15 +01:00
|
|
|
String? iosId;
|
2020-05-29 19:56:03 +02:00
|
|
|
|
2022-04-23 14:00:47 +02:00
|
|
|
///Menu item ID. It cannot be `null` and it can be a [String] or an [int].
|
|
|
|
///
|
|
|
|
///**NOTE for Android**: it must be an [int] value.
|
|
|
|
dynamic id;
|
|
|
|
|
2020-05-21 03:34:39 +02:00
|
|
|
///Menu item title.
|
|
|
|
String title;
|
2020-05-29 19:56:03 +02:00
|
|
|
|
2020-05-21 03:34:39 +02:00
|
|
|
///Menu item action that will be called when an user clicks on it.
|
2021-01-28 17:10:15 +01:00
|
|
|
Function()? action;
|
2020-05-21 03:34:39 +02:00
|
|
|
|
2020-05-29 19:56:03 +02:00
|
|
|
ContextMenuItem(
|
2022-04-23 14:00:47 +02:00
|
|
|
{this.id,
|
2022-04-23 17:51:56 +02:00
|
|
|
@Deprecated("Use id instead") this.androidId,
|
|
|
|
@Deprecated("Use id instead") this.iosId,
|
2022-04-23 14:00:47 +02:00
|
|
|
required this.title,
|
|
|
|
this.action}) {
|
|
|
|
if (defaultTargetPlatform == TargetPlatform.android) {
|
|
|
|
// ignore: deprecated_member_use_from_same_package
|
|
|
|
this.id = this.id ?? this.androidId;
|
|
|
|
assert(this.id is int);
|
|
|
|
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
|
|
|
|
// ignore: deprecated_member_use_from_same_package
|
|
|
|
this.id = this.id ?? this.iosId;
|
|
|
|
}
|
|
|
|
assert(this.id != null && (this.id is int || this.id is String));
|
|
|
|
}
|
2020-05-21 03:34:39 +02:00
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
2022-04-23 14:00:47 +02:00
|
|
|
return {
|
|
|
|
"id": id,
|
|
|
|
// ignore: deprecated_member_use_from_same_package
|
|
|
|
"androidId": androidId,
|
|
|
|
// ignore: deprecated_member_use_from_same_package
|
|
|
|
"iosId": iosId,
|
|
|
|
"title": title
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return this.toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toMap().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///Class that represents available settings used by [ContextMenu].
|
|
|
|
class ContextMenuSettings {
|
|
|
|
///Whether all the default system context menu items should be hidden or not. The default value is `false`.
|
|
|
|
bool hideDefaultSystemContextMenuItems;
|
|
|
|
|
|
|
|
ContextMenuSettings({this.hideDefaultSystemContextMenuItems = false});
|
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
|
|
|
"hideDefaultSystemContextMenuItems": hideDefaultSystemContextMenuItems
|
|
|
|
};
|
2020-05-21 03:34:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return this.toMap();
|
|
|
|
}
|
2020-05-30 20:23:33 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toMap().toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 14:00:47 +02:00
|
|
|
///Use [ContextMenuSettings] instead.
|
|
|
|
@Deprecated("Use ContextMenuSettings instead")
|
2020-05-30 20:23:33 +02:00
|
|
|
class ContextMenuOptions {
|
|
|
|
///Whether all the default system context menu items should be hidden or not. The default value is `false`.
|
|
|
|
bool hideDefaultSystemContextMenuItems;
|
|
|
|
|
|
|
|
ContextMenuOptions({this.hideDefaultSystemContextMenuItems = false});
|
|
|
|
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
|
|
|
"hideDefaultSystemContextMenuItems": hideDefaultSystemContextMenuItems
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return this.toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return toMap().toString();
|
|
|
|
}
|
2020-05-21 03:34:39 +02:00
|
|
|
}
|