Commit 09fb1af9a2109a852b6f2248ae94ecd06f8a92ca
1 parent
8865a7c0
feat:flutter_inappwebview升级
Showing
4 changed files
with
72 additions
and
70 deletions
lib/app/splash_page.dart
| ... | ... | @@ -84,6 +84,8 @@ class _TransitionViewState extends State<TransitionView> { | 
| 84 | 84 | child: WebviewDialog( | 
| 85 | 85 | title: "服务条款及隐私政策", | 
| 86 | 86 | webUrl: AppConsts.userPrivacyPolicyUrl, | 
| 87 | + leftButtonText: '同意并继续', | |
| 88 | + rightButtonText: '不同意,退出应用', | |
| 87 | 89 | leftTap: () { | 
| 88 | 90 | AppConfigHelper.saveAgreementAccepted(true); | 
| 89 | 91 | _initData(); | ... | ... | 
lib/common/pages/wow_web_page.dart
| ... | ... | @@ -13,7 +13,7 @@ class WowWebViewPage extends StatefulWidget { | 
| 13 | 13 | WowWebViewPage({super.key, required this.urlStr, required this.webViewTitle}); | 
| 14 | 14 | |
| 15 | 15 | final String urlStr; | 
| 16 | - String webViewTitle; | |
| 16 | + String? webViewTitle; | |
| 17 | 17 | |
| 18 | 18 | @override | 
| 19 | 19 | State<StatefulWidget> createState() { | 
| ... | ... | @@ -36,7 +36,7 @@ class _WowWebViewPageState extends State<WowWebViewPage> { | 
| 36 | 36 | double _progress = 0; | 
| 37 | 37 | bool isCanGoBack = false; | 
| 38 | 38 | bool isCanForward = false; | 
| 39 | - late final String defaultWebViewTitle = widget.webViewTitle; | |
| 39 | + late final String defaultWebViewTitle = widget.webViewTitle ?? ''; | |
| 40 | 40 | final String TAG = "WowWebViewPage"; | 
| 41 | 41 | |
| 42 | 42 | // InAppWebViewSettings webViewSettings = InAppWebViewSettings( | 
| ... | ... | @@ -47,18 +47,15 @@ class _WowWebViewPageState extends State<WowWebViewPage> { | 
| 47 | 47 | // useHybridComposition: true, | 
| 48 | 48 | // allowsInlineMediaPlayback: true, | 
| 49 | 49 | // ); | 
| 50 | - InAppWebViewGroupOptions webViewSettings = InAppWebViewGroupOptions( | |
| 51 | - crossPlatform: InAppWebViewOptions( | |
| 52 | - useShouldOverrideUrlLoading: true, // 是否需要跳转 | |
| 53 | - mediaPlaybackRequiresUserGesture: false, // 设置为true,防止H5的音频自动播放 | |
| 54 | - transparentBackground: true, | |
| 55 | - ), | |
| 56 | - android: AndroidInAppWebViewOptions( | |
| 57 | - useHybridComposition: true, | |
| 58 | - mixedContentMode: AndroidMixedContentMode.MIXED_CONTENT_ALWAYS_ALLOW), | |
| 59 | - ios: IOSInAppWebViewOptions( | |
| 60 | - allowsInlineMediaPlayback: true, | |
| 61 | - ), | |
| 50 | + InAppWebViewSettings webViewSettings = InAppWebViewSettings( | |
| 51 | + useShouldOverrideUrlLoading: true, | |
| 52 | + // 是否需要跳转 | |
| 53 | + mediaPlaybackRequiresUserGesture: false, | |
| 54 | + // 设置为true,防止H5的音频自动播放 | |
| 55 | + transparentBackground: true, | |
| 56 | + useHybridComposition: true, | |
| 57 | + mixedContentMode: MixedContentMode.MIXED_CONTENT_ALWAYS_ALLOW, | |
| 58 | + allowsInlineMediaPlayback: true, | |
| 62 | 59 | ); | 
| 63 | 60 | |
| 64 | 61 | Future<String?> getUrl() { | 
| ... | ... | @@ -73,7 +70,7 @@ class _WowWebViewPageState extends State<WowWebViewPage> { | 
| 73 | 70 | return Future.sync(() => null); | 
| 74 | 71 | } | 
| 75 | 72 | return _inAppWebViewController! | 
| 76 | - .loadUrl(urlRequest: URLRequest(url: Uri.parse(url))); | |
| 73 | + .loadUrl(urlRequest: URLRequest(url: WebUri.uri(Uri.parse(url)))); | |
| 77 | 74 | } | 
| 78 | 75 | |
| 79 | 76 | @override | 
| ... | ... | @@ -198,7 +195,7 @@ class _WowWebViewPageState extends State<WowWebViewPage> { | 
| 198 | 195 | // height: 40.h, | 
| 199 | 196 | // width: 40.w, | 
| 200 | 197 | // ), | 
| 201 | - icon: Icon(Icons.close), | |
| 198 | + icon: const Icon(Icons.close), | |
| 202 | 199 | onPressed: () { | 
| 203 | 200 | Navigator.pop(context); | 
| 204 | 201 | }, | 
| ... | ... | @@ -213,10 +210,10 @@ class _WowWebViewPageState extends State<WowWebViewPage> { | 
| 213 | 210 | child: InAppWebView( | 
| 214 | 211 | key: webViewKey, | 
| 215 | 212 | initialUrlRequest: URLRequest( | 
| 216 | - url: Uri.parse(widget.urlStr), | |
| 213 | + url: WebUri.uri(Uri.parse(widget.urlStr)), | |
| 217 | 214 | ), | 
| 218 | 215 | initialUserScripts: UnmodifiableListView<UserScript>([]), | 
| 219 | - initialOptions: webViewSettings, | |
| 216 | + initialSettings: webViewSettings, | |
| 220 | 217 | onWebViewCreated: (controller) { | 
| 221 | 218 | _inAppWebViewController = controller; | 
| 222 | 219 | }, | 
| ... | ... | @@ -266,13 +263,13 @@ class _WowWebViewPageState extends State<WowWebViewPage> { | 
| 266 | 263 | }) | 
| 267 | 264 | }); | 
| 268 | 265 | }, | 
| 269 | - onLoadError: (controller, request, code, message) { | |
| 266 | + onReceivedError: (controller, request, error) { | |
| 270 | 267 | Log.d( | 
| 271 | - "$TAG onReceivedError request=$request error=$code message=$message"); | |
| 268 | + "$TAG onReceivedError request=$request error=$error"); | |
| 272 | 269 | }, | 
| 273 | - onLoadHttpError: (controller, request, errorResponse, message) { | |
| 270 | + onReceivedHttpError: (controller, request, response) { | |
| 274 | 271 | Log.d( | 
| 275 | - "$TAG onReceivedError request=$request errorResponse=$errorResponse message=$message"); | |
| 272 | + "$TAG onReceivedError request=$request response=$response"); | |
| 276 | 273 | }, | 
| 277 | 274 | onProgressChanged: (controller, progress) { | 
| 278 | 275 | Log.d("$TAG onProgressChanged progress=$progress"); | ... | ... | 
lib/common/widgets/webview_dialog.dart
| ... | ... | @@ -5,15 +5,20 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart'; | 
| 5 | 5 | class WebviewDialog extends StatelessWidget { | 
| 6 | 6 | final String title; | 
| 7 | 7 | final String webUrl; | 
| 8 | - final VoidCallback leftTap; | |
| 9 | - final VoidCallback rightTap; | |
| 8 | + final String? leftButtonText; | |
| 9 | + final String? rightButtonText; | |
| 10 | + final VoidCallback? leftTap; | |
| 11 | + final VoidCallback? rightTap; | |
| 12 | + InAppWebViewController? _inAppWebViewController; | |
| 10 | 13 | |
| 11 | - const WebviewDialog( | |
| 14 | + WebviewDialog( | |
| 12 | 15 | {super.key, | 
| 13 | 16 | required this.title, | 
| 14 | 17 | required this.webUrl, | 
| 15 | - required this.leftTap, | |
| 16 | - required this.rightTap}); | |
| 18 | + this.leftButtonText, | |
| 19 | + this.rightButtonText, | |
| 20 | + this.leftTap, | |
| 21 | + this.rightTap}); | |
| 17 | 22 | |
| 18 | 23 | @override | 
| 19 | 24 | Widget build(BuildContext context) { | 
| ... | ... | @@ -22,8 +27,8 @@ class WebviewDialog extends StatelessWidget { | 
| 22 | 27 | child: Text(title), | 
| 23 | 28 | ), | 
| 24 | 29 | content: SizedBox( | 
| 25 | - width: MediaQuery.of(context).size.height - 100, | |
| 26 | - height: MediaQuery.of(context).size.width - 100, | |
| 30 | + width: MediaQuery.of(context).size.height, | |
| 31 | + height: MediaQuery.of(context).size.height, | |
| 27 | 32 | child: FutureBuilder( | 
| 28 | 33 | // 异步方法 | 
| 29 | 34 | future: buildWebViewController(webUrl), | 
| ... | ... | @@ -40,28 +45,30 @@ class WebviewDialog extends StatelessWidget { | 
| 40 | 45 | return snapshot.data ?? const Text('No data'); | 
| 41 | 46 | } | 
| 42 | 47 | })), | 
| 43 | - actions: <Widget>[ | |
| 44 | - Row( | |
| 45 | - mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| 46 | - children: [ | |
| 47 | - TextButton( | |
| 48 | - child: const Text('同意并继续', | |
| 49 | - style: TextStyle(color: Color(0xFFFBB621))), | |
| 50 | - onPressed: () { | |
| 51 | - // 处理接受按钮的点击事件 | |
| 52 | - leftTap(); // 关闭对话框 | |
| 53 | - }, | |
| 54 | - ), | |
| 55 | - TextButton( | |
| 56 | - child: const Text('不同意,退出应用'), | |
| 57 | - onPressed: () { | |
| 58 | - // 处理拒绝按钮的点击事件 | |
| 59 | - rightTap(); // 关闭对话框 | |
| 60 | - }, | |
| 61 | - ), | |
| 62 | - ], | |
| 63 | - ) | |
| 64 | - ], | |
| 48 | + actions: (leftButtonText == null && rightButtonText == null) | |
| 49 | + ? null | |
| 50 | + : <Widget>[ | |
| 51 | + Row( | |
| 52 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| 53 | + children: [ | |
| 54 | + TextButton( | |
| 55 | + child: Text(leftButtonText!, | |
| 56 | + style: const TextStyle(color: Color(0xFFFBB621))), | |
| 57 | + onPressed: () { | |
| 58 | + // 处理接受按钮的点击事件 | |
| 59 | + leftTap?.call(); // 关闭对话框 | |
| 60 | + }, | |
| 61 | + ), | |
| 62 | + TextButton( | |
| 63 | + child: Text(rightButtonText!), | |
| 64 | + onPressed: () { | |
| 65 | + // 处理拒绝按钮的点击事件 | |
| 66 | + rightTap?.call(); // 关闭对话框 | |
| 67 | + }, | |
| 68 | + ), | |
| 69 | + ], | |
| 70 | + ) | |
| 71 | + ], | |
| 65 | 72 | ); | 
| 66 | 73 | } | 
| 67 | 74 | |
| ... | ... | @@ -94,24 +101,20 @@ class WebviewDialog extends StatelessWidget { | 
| 94 | 101 | // res = WebViewWidget(controller: controller); | 
| 95 | 102 | |
| 96 | 103 | res = InAppWebView( | 
| 97 | - initialUrlRequest: URLRequest(url: Uri.parse(url)), | |
| 98 | - initialUserScripts: UnmodifiableListView<UserScript>([]), | |
| 99 | - initialOptions: InAppWebViewGroupOptions( | |
| 100 | - crossPlatform: InAppWebViewOptions( | |
| 101 | - useShouldOverrideUrlLoading: true, // 是否需要跳转 | |
| 102 | - mediaPlaybackRequiresUserGesture: false, // 设置为true,方式H5的音频自动播放 | |
| 103 | - transparentBackground: true | |
| 104 | - ), | |
| 105 | - android: AndroidInAppWebViewOptions( | |
| 106 | - useHybridComposition: true, | |
| 107 | - ), | |
| 108 | - ios: IOSInAppWebViewOptions( | |
| 109 | - allowsInlineMediaPlayback: true, | |
| 110 | - ), | |
| 111 | - ), | |
| 112 | - onWebViewCreated: (controller) { | |
| 113 | - // _inAppWebViewController = controller; | |
| 114 | - }, | |
| 104 | + initialUrlRequest: URLRequest(url: WebUri.uri(Uri.parse(url))), | |
| 105 | + initialUserScripts: UnmodifiableListView<UserScript>([]), | |
| 106 | + initialSettings: InAppWebViewSettings( | |
| 107 | + javaScriptEnabled: true, | |
| 108 | + javaScriptCanOpenWindowsAutomatically: true, | |
| 109 | + mediaPlaybackRequiresUserGesture: false, | |
| 110 | + transparentBackground: true, | |
| 111 | + useHybridComposition: true, | |
| 112 | + allowsInlineMediaPlayback: true, | |
| 113 | + useShouldOverrideUrlLoading: true, | |
| 114 | + ), | |
| 115 | + onWebViewCreated: (controller) { | |
| 116 | + _inAppWebViewController = controller; | |
| 117 | + }, | |
| 115 | 118 | ); | 
| 116 | 119 | } catch (error) { | 
| 117 | 120 | res = Text("加载失败:${error.toString()}"); | ... | ... | 
pubspec.yaml
| ... | ... | @@ -51,7 +51,7 @@ dependencies: | 
| 51 | 51 | #网页加载 https://pub.dev/packages/webview_flutter | 
| 52 | 52 | # webview_flutter: ^4.8.0 | 
| 53 | 53 | #https://pub.dev/packages/flutter_inappwebview | 
| 54 | - flutter_inappwebview: 5.8.0 | |
| 54 | + flutter_inappwebview: 6.0.0 | |
| 55 | 55 | #下拉刷新 https://pub.dev/packages/pull_to_refresh | 
| 56 | 56 | pull_to_refresh: ^2.0.0 | 
| 57 | 57 | # 数据持久化 https://pub.dev/packages/shared_preferences | ... | ... | 
