问题描述
我正在尝试在 Flutter 中使用 Web 视图。我关注了这篇描述我们如何使用 Flutter embed webview 创建简单应用程序的中等文章。
https://medium.com/@ekosuprastyo15/webview-in-flutter-example-a11a24eb617f
我能够成功创建应用程序,但现在该应用程序正在通过单击按钮打开,如上述文章所示。
我想要的 - 现在我想创建一个应用程序,在加载时在 webview 中加载一个 URL(即用户不必点击任何按钮或链接来打开该 URL)。
到目前为止我们尝试了什么?
我们已经尝试过 Flutter url_launcher 插件和 flutter_webview_plugin 插件。
解决方法
添加这个库 Flutter WebView
import 'package:webview_flutter/webview_flutter.dart';
return Scaffold(
appBar: AppBar(
title: const Text('Flutter WebView example'),),body: const WebView(
initialUrl: 'https://flutter.io',javascriptMode: JavascriptMode.unrestricted,);
,
如果您想直接打开您想要的网站而不按任何按钮,您应该提供初始网址。但你应该从其他页面 nagivate.push 到这里。就是这样;
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewExample extends StatefulWidget {
@override
WebViewExampleState createState() => WebViewExampleState();
}
class WebViewExampleState extends State<WebViewExample> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("webview"),body: WebView(
initialUrl: "https://www.google.com/",);
}
}