Flutter:如何使用WebView插件

news/2024/7/5 22:32:56

Displaying webpages inside of your Flutter applications is easy with the use of the WebView plugin. In our example application, we’ll look at how to create a custom Widget that can be used throughout our application to launch a WebView from anywhere.

显示网页的颤振应用中很容易与使用的WebView插件。 在示例应用程序中,我们将研究如何创建可在整个应用程序中使用的自定义Widget ,以便从任何地方启动WebView

创建一个新的Flutter项目 (Creating a new Flutter Project)

As always, we’ll start off by setting up a new project and adding the plugin:

与往常一样,我们将从建立一个新项目并添加插件开始:

# New Flutter project
$ flutter create my_webview_project

# Open this up inside of VS Code
$ cd my_webview_project && code .

添加WebView插件 (Adding the WebView plugin)

Next up, we’ll need to add the webview_flutter plugin within our pubspec.yaml:

接下来,我们需要添加webview_flutter我们中的插件pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  webview_flutter: ^0.3.14+1

We’ll then need to add the appropriate values to our Info.plist, opting into the embedded views preview:

然后,我们需要将适当的值添加到Info.plist ,以选择嵌入的视图预览:

<key>io.flutter.embedded_views_preview</key>
<true/>

That’s all the required plugin preparation, we can now open our application up in the iOS or Android simulator.

这就是所有必要的插件准备工作,我们现在可以在iOS或Android模拟器中打开应用程序。

脚手架我们的项目 (Scaffolding our Project)

We can now update main.dart to contain our HomePage widget that we’ll create in a second:

现在,我们可以更新main.dart包含我们的HomePage小工具,我们将在第二个创建:

import 'package:flutter/material.dart';
import 'package:my_webview_project/home_page.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter WebView',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: HomePage());
  }
}

The HomePage widget will simply consist of a FlatButton with an onPressed event:

HomePage部件将仅包括一个的FlatButtononPressed事件:

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home Page"),
      ),
      body: Center(
        child: FlatButton(
          child: Text("Open Webpage"),
          onPressed: () {},
        ),
      ),
    );
  }
}

使用WebView插件 (Using the WebView Plugin)

Let’s create a StatelessWidget named MyWebView which we can use to navigate a user to this page whenever we want to display WebView data:

让我们创建一个StatelessWidget名为MyWebView我们可以用它来用户导航到该页面时,我们要显示WebView数据:

import 'dart:async';
import 'package:flutter/material.dart';

import 'package:webview_flutter/webview_flutter.dart';

class MyWebView extends StatelessWidget {
  final String title;
  final String selectedUrl;

  final Completer<WebViewController> _controller =
      Completer<WebViewController>();

  MyWebView({
    @required this.title,
    @required this.selectedUrl,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: WebView(
          initialUrl: selectedUrl,
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (WebViewController webViewController) {
            _controller.complete(webViewController);
          },
        ));
  }
}

If we wanted to navigate the user to https://alligator.io, we could therefore use Navigator.push with the selectedUrl equal to https://alligator.io. Let’s update our FlatButton within the HomePage:

如果我们想将用户导航到https://alligator.io ,则可以使用Navigator.push ,并且selectedUrl等于https://alligator.io 。 让我们在HomePage更新FlatButton

child: FlatButton(
  child: Text("Open Webpage"),
  onPressed: () {
    Navigator.of(context).push(MaterialPageRoute(
        builder: (BuildContext context) => MyWebView(
              title: "Alligator.io",
              selectedUrl: "https://alligator.io",
            )));
  },
),

And there we have it! Here’s our WebView showing the Alligator homepage:

我们终于得到它了! 这是我们的WebView显示了鳄鱼主页:

翻译自: https://www.digitalocean.com/community/tutorials/flutter-webview


http://www.niftyadmin.cn/n/3649741.html

相关文章

[J2ME]手机流媒体之实作[附源码][与RTSP/MMS协议无关]

郑昀ultrapower产品名称产品版本Keyword: JavaME Streaming Audio MMAPIStreamingDemo1.0.13[J2ME]现行环境下手机实现网络媒体的流畅实时播放之简单演示(StreamingDemo)说明我的资源&#xff1a;http://www.cnblogs.com/Files/zhengyun_ustc/StreamingDemo-src-1.0.13.rar 这个…

[J2ME]增强型二级菜单(增加了三级菜单以及悬浮菜单)

[J2ME]增强型二级菜单演示(AdvdMenuBar)说明我的资源&#xff1a;http://www.cnblogs.com/Files/zhengyun_ustc/AdvdMenuBar-src-1.0.26.rar 这个1.0.26版本的j2me软件&#xff0c;能够在WTK2.3beta的模拟器上运行。本源代码改自SaltedFish的增强版本的AdvdMenuBar&#xff0c;…

[J2ME QA]Target port denied to untrusted applications问题回应

[When]开发J2ME程序的时候&#xff0c;用户可能在运行Socket程序连接远端Socket服务器时出现不受信任的错误&#xff0c;如下面的代码&#xff1a; SocketConnection sc (SocketConnection)Connector.open("socket://localhost:8080");报错为&#xff1a;Uncaught e…

flutter 主题_在Flutter中使用主题

flutter 主题One great aspects of Flutter is its use of UI packages like the Material and Cupertino design systems. They work to shortcut all of the minute and repetitive design choices like the appBar height or the shadows on buttons. But obviously always …

[J2ME QA]untrusted domain is not configured问题回应

[When]第一次使用Netbean 4.0/5.0开发程序的时候&#xff0c;用户可能在运行程序时&#xff0c;遇到下面这种报错&#xff0c;导致模拟器一闪而过&#xff1a;正在通过存储根 DefaultColorPhone 来运行 域名不正确&#xff0c;请切换到 untrustedjava.lang.SecurityException: …

[j2me]二级菜单演示 开源声明

郑昀ultrapower产品名称产品版本Keyword: JavaME 二级菜单MenuBarlet1.0.20[J2ME]二级菜单演示(MenuBarlet)开源说明我的资源&#xff1a;http://www.cnblogs.com/Files/zhengyun_ustc/menubar-1.0.20-src.rar这个1.0.20版本的j2me软件&#xff0c;能够在Nokia S60系列、索爱K7…

react ui 组件_如何使用React和Storybook构建UI组件

react ui 组件介绍 (Introduction) In the previous article, I gave an Introduction to Storybook and how it can be used to organize and build JavaScript components. Storybook is a UI library that can be used to document components. In this article, I’ll be e…

了解JavaScript中的变量范围

The scope is the execution context of a variable or function. It defines what data it has access to. This concept may seem pretty straightforward, but has some important subtleties. 作用域是变量或函数的执行上下文。 它定义了它有权访问的数据。 这个概念可能看…