我知道可以将WebView添加为整个页面,但找不到任何示例代码来实现。我猜想可以使用PageView作为基础,但不确定如何调用本地Android WebView并将其添加到PageView中。
有人能指点一下吗?
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,
),
);
Flutter没有内置支持嵌入式WebViews的功能。关注问题730以获取更新。
您仍然可以在应用程序中显示Web内容,但必须使用插件系统离开Flutter环境。
如果只想打开浏览器,可以使用url_launcher Flutter插件。
如果要执行更高级的操作(例如不想要可见的地址栏),可以实现自定义的UIViewController (iOS示例) 和 Activity (Android示例),并使用插件 API启动它们。
new MaterialApp(
routes: {
"/": (_) => new WebviewScaffold(
url: "https://www.google.com",
appBar: new AppBar(
title: new Text("Widget webview"),
),
),
},
);
webview_flutter
更好用。它不会出现无法显示键盘的问题,而且当点击下拉菜单等等时,我也没有遇到webview_flutter
造成的崩溃。 - AlvaroSantistebanWebview插件
功能良好,但是它会覆盖您应用的每一个部分,因此您需要添加额外的逻辑来显示和隐藏插件。您可以将其全屏显示或作为大小矩形显示。
在 pubspec.yml 文件中添加依赖项:
webview_flutter: ^0.1.1
对于 ioS 应用,将以下密钥粘贴到 ios 项目文件夹内的 .plist 文件中
<key>io.flutter.embedded_views_preview</key><string>yes</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
这是类代码:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'dart:async';
class WebViewClass extends StatefulWidget {
@override
_WebViewClassState createState() => _WebViewClassState();
}
class _WebViewClassState extends State<WebViewClass> {
Completer<WebViewController> _controller = Completer<WebViewController>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('WebView'),
),
body: WebView(
initialUrl: 'https://google.com',
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
),
);
}
}
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebView extends StatefulWidget {
@override
_WebViewState createState() => _WebViewState();
}
class _WebViewState extends State<WebView> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
num _stackToView = 1;
void _handleLoad(String value) {
setState(() {
_stackToView = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Builder(builder: (BuildContext context) {
return IconButton(
icon: Icon(Icons.volume_up, color: Colors.black,),
onPressed: () {
Navigator.pop(context);
},
);
}),
backgroundColor: Colors.white10,
elevation: 0,
),
body: IndexedStack(
index: _stackToView,
children: [
Column(
children: <Widget>[
Expanded(
child: WebView(
initialUrl: "https://www.google.co.in/",
javascriptMode: JavascriptMode.unrestricted,
onPageFinished: _handleLoad,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
)),
],
),
Container(
child: Center(child: CircularProgressIndicator(),)
),
],
));
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
WebViewController webViewController = WebViewController();
return Scaffold(
appBar: AppBar(),
body: WebViewWidget(
controller: webViewController
..loadRequest(Uri.parse('https://flutter.dev'))));
}
}
InAppWebView
类,您需要通过在应用程序的Info.plist
文件中添加一个布尔属性,并设置键io.flutter.embedded_views_preview
和值YES
来选择嵌入式视图预览。InAppWebView
附加到小部件树中。http://localhost:[port]/
上创建一个简单的服务器。默认的port
值为8080
。import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
InAppWebViewController _webViewController;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('InAppWebView Example'),
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child:InAppWebView(
initialUrl: "https://flutter.dev/",
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
_webViewController = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {
},
onLoadStop: (InAppWebViewController controller, String url) {
},
),
),
])),
),
);
}
}
库: https://pub.dev/packages/webview_flutter
我的实现方式如下:
import 'package:webview_flutter/webview_flutter.dart';
...
return Scaffold(
appBar: AppBar(toolbarHeight: 0),
body: Builder(builder: (BuildContext context) {
return WebView(
initialUrl: 'https://flutter.dev/',
javascriptMode: JavascriptMode.unrestricted,
gestureNavigationEnabled: true,
backgroundColor: const Color(0x00000000),
);
}),
);
因为在Android 9中禁止使用'http'
您可以进行以下设置:
在您的android>app>src>main文件夹中导航到AndroidManifest.xml
<application
android:name="io.flutter.app.FlutterApplication"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true" />
Flutter WebView插件在Android 9上出现错误