context_menu.dart 2.73 KB
Newer Older
1 2
import 'package:flutter/foundation.dart';

3
import 'webview.dart';
4 5 6 7 8 9 10 11 12
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.
13 14
  final void Function(InAppWebViewHitTestResult hitTestResult)
      onCreateContextMenu;
15 16 17 18 19 20 21

  ///Event fired when the context menu for this WebView is being hidden.
  final void Function() onHideContextMenu;

  ///Event fired when a context menu item has been clicked.
  ///
  ///[contextMenuItemClicked] represents the [ContextMenuItem] clicked.
22 23
  final void Function(ContextMenuItem contextMenuItemClicked)
      onContextMenuActionItemClicked;
24

25 26 27
  ///Context menu options.
  final ContextMenuOptions options;

28
  ///List of the custom [ContextMenuItem].
29
  final List<ContextMenuItem> menuItems;
30

31
  ContextMenu(
32
      {this.menuItems = const [],
33 34
      this.onCreateContextMenu,
      this.onHideContextMenu,
35 36 37
      this.options,
      this.onContextMenuActionItemClicked})
      : assert(menuItems != null);
38 39 40

  Map<String, dynamic> toMap() {
    return {
41 42
      "menuItems": menuItems.map((menuItem) => menuItem?.toMap()).toList(),
      "options": options?.toMap()
43 44 45 46 47 48
    };
  }

  Map<String, dynamic> toJson() {
    return this.toMap();
  }
49 50 51 52 53

  @override
  String toString() {
    return toMap().toString();
  }
54 55 56 57 58 59
}

///Class that represent an item of the [ContextMenu].
class ContextMenuItem {
  ///Android menu item ID.
  int androidId;
60

61 62
  ///iOS menu item ID.
  String iosId;
63

64 65
  ///Menu item title.
  String title;
66

67 68 69
  ///Menu item action that will be called when an user clicks on it.
  Function() action;

70 71 72 73 74
  ContextMenuItem(
      {@required this.androidId,
      @required this.iosId,
      @required this.title,
      this.action});
75 76

  Map<String, dynamic> toMap() {
77
    return {"androidId": androidId, "iosId": iosId, "title": title};
78 79 80 81 82
  }

  Map<String, dynamic> toJson() {
    return this.toMap();
  }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  @override
  String toString() {
    return toMap().toString();
  }
}

///Class that represents available options used by [ContextMenu].
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();
  }
111
}