Flutter插件開(kāi)發(fā)流程3(Use a Platform Interface)

1.分析上一節(jié)中Web插件的不足

  • methodchannel是通過(guò)字節(jié)數(shù)組傳遞的在web中是比必要的,因?yàn)閣eb最后都是js代碼

For one, there is unnecessary overhead of sending plugin method calls over a MethodChannel. On the web, your entire app is compiled into one JavaScript bundle, so the plugin code is needlessly serializing the method call into a byte array, which is then instantly deserialized by the web plugin.

  • methodchannel 使用字符串和插件匹配遵堵,在其他平臺(tái)箱玷,這個(gè)web插件是不必要的,上一講的寫(xiě)法不利于屏蔽web插件代碼

Another disadvantage of using a MethodChannel is that it makes it difficult for the compiler to remove (by tree-shaking) unused plugin code. The web plugin calls the appropriate method based on the name of the method call passed by the MethodChannel, so the compiler has to assume that all of the methods in the plugin are live, and none of them can be tree-shaken out

2.interface模塊引入陌宿,依賴(lài)關(guān)系如下

image.png

3.關(guān)鍵代碼分析

image.png
平臺(tái)接口的launch和closeWebView對(duì)應(yīng)原來(lái)methodChannel方法
import 'dart:async';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'method_channel_url_launcher.dart';

abstract class UrlLauncherPlatform extends PlatformInterface {
  /// Constructs a UrlLauncherPlatform.
  UrlLauncherPlatform() : super(token: _token);

  static final Object _token = Object();

  static UrlLauncherPlatform _instance = MethodChannelUrlLauncher();

  /// The default instance of [UrlLauncherPlatform] to use.
  ///
  /// Defaults to [MethodChannelUrlLauncher].
  static UrlLauncherPlatform get instance => _instance;
themselves.
  // TODO(amirh): Extract common platform interface logic.
  // https://github.com/flutter/flutter/issues/43368
  static set instance(UrlLauncherPlatform instance) {
    PlatformInterface.verifyToken(instance, _token);
    _instance = instance;
  }

  /// The delegate used by the Link widget to build itself.
  LinkDelegate? get linkDelegate;

  /// Returns `true` if this platform is able to launch [url].
  Future<bool> canLaunch(String url) {
    throw UnimplementedError('canLaunch() has not been implemented.');
  }

  /// 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,
    String? webOnlyWindowName,
  }) {
    throw UnimplementedError('launch() has not been implemented.');
  }

  /// Closes the WebView, if one was opened earlier by [launch].
  Future<void> closeWebView() {
    throw UnimplementedError('closeWebView() has not been implemented.');
  }
}

url_lancher_web給出實(shí)現(xiàn)
注意registerWith不再是創(chuàng)建個(gè)methodChannel接收調(diào)用锡足,而是更加平臺(tái)接口的實(shí)現(xiàn)為當(dāng)前web插件
UrlLauncherPlatform.instance = UrlLauncherPlugin();

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:html' as html;
import 'src/shims/dart_ui.dart' as ui;

import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:meta/meta.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

import 'src/link.dart';
import 'src/third_party/platform_detect/browser.dart';

const _safariTargetTopSchemes = {
  'mailto',
  'tel',
  'sms',
};
String? _getUrlScheme(String url) => Uri.tryParse(url)?.scheme;

bool _isSafariTargetTopScheme(String url) =>
    _safariTargetTopSchemes.contains(_getUrlScheme(url));

/// The web implementation of [UrlLauncherPlatform].
///
/// This class implements the `package:url_launcher` functionality for the web.
class UrlLauncherPlugin extends UrlLauncherPlatform {
  html.Window _window;
  bool _isSafari = false;

  // The set of schemes that can be handled by the plugin
  static final _supportedSchemes = {
    'http',
    'https',
  }.union(_safariTargetTopSchemes);

  /// A constructor that allows tests to override the window object used by the plugin.
  UrlLauncherPlugin({@visibleForTesting html.Window? debugWindow})
      : _window = debugWindow ?? html.window {
    _isSafari = navigatorIsSafari(_window.navigator);
  }

  /// Registers this class as the default instance of [UrlLauncherPlatform].
  static void registerWith(Registrar registrar) {
    UrlLauncherPlatform.instance = UrlLauncherPlugin();
    ui.platformViewRegistry.registerViewFactory(linkViewType, linkViewFactory);
  }

  @override
  LinkDelegate get linkDelegate {
    return (LinkInfo linkInfo) => WebLinkDelegate(linkInfo);
  }

  /// Opens the given [url] in the specified [webOnlyWindowName].
  ///
  /// Returns the newly created window.
  @visibleForTesting
  html.WindowBase openNewWindow(String url, {String? webOnlyWindowName}) {
    // We need to open mailto, tel and sms urls on the _top window context on safari browsers.
    // See https://github.com/flutter/flutter/issues/51461 for reference.
    final target = webOnlyWindowName ??
        ((_isSafari && _isSafariTargetTopScheme(url)) ? '_top' : '');
    return _window.open('https://baidu.com', target);
  }

  @override
  Future<bool> canLaunch(String url) {
    return Future<bool>.value(_supportedSchemes.contains(_getUrlScheme(url)));
  }

  @override
  Future<bool> launch(
    String url, {
    bool useSafariVC = false,
    bool useWebView = false,
    bool enableJavaScript = false,
    bool enableDomStorage = false,
    bool universalLinksOnly = false,
    Map<String, String> headers = const <String, String>{},
    String? webOnlyWindowName,
  }) {
    return Future<bool>.value(
        openNewWindow('https://baidu.com', webOnlyWindowName: webOnlyWindowName) != null);
  }
}

url_lancher插件接口修改

以前是主插件那里,發(fā)送個(gè)methodchannel調(diào)用壳坪,現(xiàn)在則是改成平臺(tái)接口調(diào)用舶得,而平臺(tái)接口的實(shí)現(xiàn)可以是通過(guò)methodchannel方式,而對(duì)應(yīng)web插件的實(shí)現(xiàn)爽蝴,則是直接調(diào)用了沐批,不再是methodchannel
插件url_launcher.dart

Future<bool> canLaunch(String urlString) async {
  return await UrlLauncherPlatform.instance.canLaunch(urlString);
}

版本依賴(lài)和發(fā)布

image.png

目前認(rèn)為纫骑,先把被依賴(lài)的發(fā)布上去,和maven庫(kù)發(fā)布流程類(lèi)似吧九孩,具體沒(méi)有實(shí)際操作過(guò)先馆!

參考地址:
How To Write a Flutter Web Plugin: Part 2

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市躺彬,隨后出現(xiàn)的幾起案子煤墙,更是在濱河造成了極大的恐慌,老刑警劉巖宪拥,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件仿野,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡她君,警方通過(guò)查閱死者的電腦和手機(jī)脚作,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)缔刹,“玉大人球涛,你說(shuō)我怎么就攤上這事〗奥荩” “怎么了宾符?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵酿秸,是天一觀的道長(zhǎng)灭翔。 經(jīng)常有香客問(wèn)我,道長(zhǎng)辣苏,這世上最難降的妖魔是什么肝箱? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮稀蟋,結(jié)果婚禮上煌张,老公的妹妹穿的比我還像新娘。我一直安慰自己退客,他們只是感情好骏融,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著萌狂,像睡著了一般档玻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上茫藏,一...
    開(kāi)封第一講書(shū)人閱讀 49,144評(píng)論 1 285
  • 那天误趴,我揣著相機(jī)與錄音,去河邊找鬼务傲。 笑死凉当,一個(gè)胖子當(dāng)著我的面吹牛枣申,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播看杭,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼忠藤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了泊窘?” 一聲冷哼從身側(cè)響起熄驼,我...
    開(kāi)封第一講書(shū)人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎烘豹,沒(méi)想到半個(gè)月后瓜贾,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡携悯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年祭芦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片憔鬼。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡龟劲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出轴或,到底是詐尸還是另有隱情昌跌,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布照雁,位于F島的核電站蚕愤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏饺蚊。R本人自食惡果不足惜萍诱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望污呼。 院中可真熱鬧裕坊,春花似錦、人聲如沸燕酷。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)苗缩。三九已至饵蒂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挤渐,已是汗流浹背苹享。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人得问。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓囤攀,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親宫纬。 傳聞我的和親對(duì)象是個(gè)殘疾皇子焚挠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容