Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.0.2

- Switch to using `url_launcher_platform_interface`

# 0.0.1

- Initial open-source release.
56 changes: 25 additions & 31 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,40 @@ import 'dart:html' as html;
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:meta/meta.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

class UrlLauncherPlugin {
class UrlLauncherPlugin extends UrlLauncherPlatform {
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'plugins.flutter.io/url_launcher',
const StandardMethodCodec(),
registrar.messenger);
final UrlLauncherPlugin instance = UrlLauncherPlugin();
channel.setMethodCallHandler(instance.handleMethodCall);
UrlLauncherPlatform.instance = UrlLauncherPlugin();
}

Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'canLaunch':
final String url = call.arguments['url'];
return _canLaunch(url);
case 'launch':
final String url = call.arguments['url'];
return _launch(url);
default:
throw PlatformException(
code: 'Unimplemented',
details: "The url_launcher plugin for web doesn't implement "
"the method '${call.method}'");
}
@visibleForTesting
html.WindowBase openNewWindow(String url) {
return html.window.open(url, '');
}

bool _canLaunch(String url) {
@override
Future<bool> canLaunch(String url) {
final Uri parsedUrl = Uri.tryParse(url);
if (parsedUrl == null) return false;

return parsedUrl.isScheme('http') || parsedUrl.isScheme('https');
}
if (parsedUrl == null) return Future<bool>.value(false);

bool _launch(String url) {
return openNewWindow(url) != null;
return Future<bool>.value(
parsedUrl.isScheme('http') || parsedUrl.isScheme('https'));
}

@visibleForTesting
html.WindowBase openNewWindow(String url) {
return html.window.open(url, '');
/// Returns `true` if the given [url] was successfully launched.
///
/// For documentation on the other arguments, see the `launch` documentation
/// in `package:url_launcher/url_launcher.dart`.
Future<bool> launch(
String url, {
@required bool useSafariVC,
@required bool useWebView,
@required bool enableJavaScript,
@required bool enableDomStorage,
@required bool universalLinksOnly,
@required Map<String, String> headers,
}) {
return Future<bool>.value(openNewWindow(url) != null);
}
}
3 changes: 2 additions & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_web
description: Web platform implementation of url_launcher
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web
version: 0.0.1
version: 0.0.2

flutter:
plugin:
Expand All @@ -17,6 +17,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
meta: ^1.1.7
url_launcher_platform_interface: ^1.0.1

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_web/url_launcher_web.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
group('URL Launcher for Web', () {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
webPluginRegistry.registerMessageHandler();
final Registrar registrar =
webPluginRegistry.registrarFor(UrlLauncherPlugin);
UrlLauncherPlugin.registerWith(registrar);
UrlLauncherPlatform.instance = UrlLauncherPlugin();
});

test('$UrlLauncherPlugin is the live instance', () {
expect(UrlLauncherPlatform.instance, isA<UrlLauncherPlugin>());
});

test('can launch "http" URLs', () {
Expand All @@ -45,5 +46,9 @@ void main() {
expect(newWindow, isNot(equals(html.window)));
expect(newWindow.opener, equals(html.window));
});

test('does not implement closeWebView()', () {
expect(closeWebView(), throwsUnimplementedError);
});
});
}