flutter_webview_plugin.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. class FlutterWebviewPlugin {
  5. static bool _init = false;
  6. static StreamController<Null> _onDestroy = new StreamController.broadcast();
  7. static Stream<Null> get onDestroy => _onDestroy.stream;
  8. static StreamController<Null> _onBackPressed =
  9. new StreamController.broadcast();
  10. static Stream<Null> get onBackPressed => _onDestroy.stream;
  11. static const MethodChannel _channel =
  12. const MethodChannel('flutter_webview_plugin');
  13. static Future<Null> launch(String url,
  14. {bool withJavascript: true,
  15. bool clearCache: false,
  16. bool clearCookies: false}) =>
  17. _channel.invokeMethod('launch', {
  18. "url": url,
  19. "withJavascript": withJavascript,
  20. "clearCache": clearCache,
  21. "clearCookies": clearCookies
  22. });
  23. static Future<Null> close() => _channel.invokeMethod("close");
  24. static init() {
  25. if (!_init) {
  26. _init = true;
  27. _channel.setMethodCallHandler(_handleMessages);
  28. }
  29. }
  30. static Future<Null> _handleMessages(MethodCall call) async {
  31. switch (call.method) {
  32. case "onDestroy":
  33. _onDestroy.add(null);
  34. break;
  35. case "onBackPressed":
  36. _onBackPressed.add(null);
  37. break;
  38. }
  39. }
  40. }