【Flutter】 APP 中打开浏览器指定网站

146 阅读1分钟

APP 点击按钮跳转到指定网站

使用插件url_launcher, 作为pubspec.yaml文件中的依赖项

使用方法

  1. 引入最新版本的插件url_launcherpubspec.yaml(当前最新版本就是 6.1.9),并进行 flutter pub get 命令进依赖的下载。
    url_launcher: ^6.1.9
  1. 便于在多处可以使用,在公共方法的类中进行定义并将目标网址作为参数:
    class CommonUtils {
        static launchURL() {
            launch('https://xxx.com/xxx/');
          }
    }
  1. 在需要的类里导入插件
    import '../../utils/common_utils.dart';
  1. 在页面所需处进行方法的调用
    CommonUtils.launchURL();

总结

  1. 引入依赖
  2. 进行方法的封装
  3. 导入类
  4. 调用并进行参数配置

参考文献: github.com/LampeMW/url…