🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 概述 需要设置安卓最低版本,android/app/build.gradle ``` android { defaultConfig { minSdkVersion 19 } } ``` > 不能直接获取 cookie, 可以用 js 交互获取 ## 示例 ``` import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; void main() { runApp( const MaterialApp( home: WebViewApp(), ), ); } class WebViewApp extends StatefulWidget { const WebViewApp({Key? key}) : super(key: key); @override State<WebViewApp> createState() => _WebViewAppState(); } class _WebViewAppState extends State<WebViewApp> { // late 表示 在使用时才加载 late final WebViewController controller=WebViewController(); late final WebViewCookieManager cookieManager = WebViewCookieManager(); @override void initState() { super.initState(); controller ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setBackgroundColor(const Color(0x00000000)) ..setNavigationDelegate( NavigationDelegate( onProgress: (int progress) { // Update loading bar. }, onPageStarted: (String url) { // 设置 cookie cookieManager.setCookie(const WebViewCookie( name: "a", value: 'b', domain: "/")); }, onPageFinished: (String url) async { // 获取 cookie controller.runJavaScriptReturningResult('document.cookie').then((res){ print("cookies"); print(res); }); }, onHttpError: (HttpResponseError error) {}, onWebResourceError: (WebResourceError error) {}, onNavigationRequest: (NavigationRequest request) { if (request.url.startsWith('https://www.youtube.com/')) { return NavigationDecision.prevent; } return NavigationDecision.navigate; }, ), ) ..loadRequest(Uri.parse('https://blog.csdn.net')); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Flutter WebView'), ), body: WebViewWidget( controller:controller, ), ); } } ```