Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added getCurrentUrl function & url in onClose Callback #24

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
22 changes: 11 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_macos_webview/flutter_macos_webview.dart';

Expand All @@ -7,26 +9,24 @@ class App extends StatelessWidget {
Future<void> _onOpenPressed(PresentationStyle presentationStyle) async {
final webview = FlutterMacOSWebView(
onOpen: () => print('Opened'),
onClose: () => print('Closed'),
onClose: (url) => print('Closed $url'),
onSetUrl: (url) => print('Set url $url'),
onTapSpecial: (url) => print("Special $url"),
onPageStarted: (url) => print('Page started: $url'),
onPageFinished: (url) => print('Page finished: $url'),
onWebResourceError: (err) {
print(
'Error: ${err.errorCode}, ${err.errorType}, ${err.domain}, ${err.description}',
);
},
onWebResourceError: (err) => print(
'Error: ${err.errorCode}, ${err.errorType}, ${err.domain}, ${err.description}',
),
);

await webview.open(
url: 'https://google.com',
presentationStyle: presentationStyle,
size: Size(400.0, 400.0),
size: Size(800, 600),
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
showSpecialButton: true,
specialButtonText: "Link is broken",
);

// await Future.delayed(Duration(seconds: 5));
// await webview.close();
}

@override
Expand Down
4 changes: 2 additions & 2 deletions example/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
flutter_macos_webview: a17978522d9014b5ebc0279d76a332b9531c4a95
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
FlutterMacOS: ae6af50a8ea7d6103d888583d46bd8328a7e9811

PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c

COCOAPODS: 1.10.0
COCOAPODS: 1.11.3
49 changes: 21 additions & 28 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,28 +21,21 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
cupertino_icons:
dependency: "direct main"
description:
Expand All @@ -56,7 +49,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -80,21 +73,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.2"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -106,7 +106,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -127,35 +127,28 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.12"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.2"
sdks:
dart: ">=2.13.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
flutter: ">=1.20.0"
26 changes: 24 additions & 2 deletions lib/flutter_macos_webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class FlutterMacOSWebView {
FlutterMacOSWebView({
this.onOpen,
this.onClose,
this.onSetUrl,
this.onTapSpecial,
this.onPageStarted,
this.onPageFinished,
this.onWebResourceError,
Expand All @@ -39,7 +41,9 @@ class FlutterMacOSWebView {
final MethodChannel _channel;

final void Function()? onOpen;
final void Function()? onClose;
final void Function(String? url)? onClose;
final void Function(String? url)? onSetUrl;
final void Function(String? url)? onTapSpecial;
final void Function(String? url)? onPageStarted;
final void Function(String? url)? onPageFinished;
final void Function(WebResourceError error)? onWebResourceError;
Expand All @@ -63,6 +67,8 @@ class FlutterMacOSWebView {
/// [modalTitle] - title for window when using `modal` presentation style
///
/// [sheetCloseButtonTitle] - title for close button when using `sheet` presentation style
///
/// [specialButtonText] - title for special button when using `sheet` presentation style
Future<void> open({
required String url,
bool javascriptEnabled = true,
Expand All @@ -72,6 +78,8 @@ class FlutterMacOSWebView {
String? userAgent,
String modalTitle = '',
String sheetCloseButtonTitle = 'Close',
String specialButtonText = 'Special',
bool showSpecialButton = false,
}) async {
assert(url.trim().isNotEmpty);

Expand All @@ -85,6 +93,9 @@ class FlutterMacOSWebView {
'userAgent': userAgent,
'modalTitle': modalTitle,
'sheetCloseButtonTitle': sheetCloseButtonTitle,
'showSetUrlButton': onSetUrl != null,
'specialButtonText': specialButtonText,
'showSpecialButton': showSpecialButton,
// 'customOrigin': origin != null,
// 'x': origin?.dx,
// 'y': origin?.dy,
Expand All @@ -96,20 +107,31 @@ class FlutterMacOSWebView {
await _channel.invokeMethod('close');
}

/// Gets current URL of WebView
Future<String?> getCurrentUrl() async {
return await _channel.invokeMethod('getCurrentUrl');
}

Future<void> _onMethodCall(MethodCall call) async {
switch (call.method) {
case 'onOpen':
onOpen?.call();
return;
case 'onClose':
onClose?.call();
onClose?.call(call.arguments['url']);
return;
case 'onSetUrl':
onSetUrl?.call(call.arguments['url']);
return;
case 'onPageStarted':
onPageStarted?.call(call.arguments['url']);
return;
case 'onPageFinished':
onPageFinished?.call(call.arguments['url']);
return;
case 'onTapSpecial':
onTapSpecial?.call(call.arguments['url']);
return;
case 'onWebResourceError':
onWebResourceError?.call(
WebResourceError(
Expand Down
Loading