import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:webview_flutter_android/webview_flutter_android.dart'; import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart'; import '../../widget/my_txt.dart'; class PrivacyAgreementPage extends StatefulWidget { const PrivacyAgreementPage({super.key}); @override State createState() => _PrivacyAgreementPageState(); } class _PrivacyAgreementPageState extends State { String url = "https://h5.xhrbxxf.com/privacy"; late final WebViewController _controller; var isLoading = true; var progress = 0; @override void initState() { super.initState(); // 平台特定的初始化 late final PlatformWebViewControllerCreationParams params; if (WebViewPlatform.instance is WebKitWebViewPlatform) { params = WebKitWebViewControllerCreationParams( allowsInlineMediaPlayback: true, mediaTypesRequiringUserAction: const {}, ); } else { params = const PlatformWebViewControllerCreationParams(); } final WebViewController controller = WebViewController.fromPlatformCreationParams(params); // 通用设置 controller ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setNavigationDelegate( NavigationDelegate( onProgress: (int progress) { if (!mounted) return; setState(() { this.progress = progress; if (progress == 100) { isLoading = false; } }); }, onPageStarted: (String url) { if (!mounted) return; setState(() => isLoading = true); }, onPageFinished: (String url) { if (!mounted) return; setState(() => isLoading = false); }, onWebResourceError: (WebResourceError error) { if (!mounted) return; debugPrint(''' WebView error: code: ${error.errorCode} description: ${error.description} errorType: ${error.errorType} isForMainFrame: ${error.isForMainFrame} '''); }, ), ) ..loadRequest(Uri.parse(url)); // Android 特定设置 if (controller.platform is AndroidWebViewController) { AndroidWebViewController.enableDebugging(true); (controller.platform as AndroidWebViewController) .setMediaPlaybackRequiresUserGesture(false); } _controller = controller; } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, resizeToAvoidBottomInset: false, appBar: AppBar( title: myTxt(text: '用户协议和隐私政策', fontSize: 18.sp, fontWeight: FontWeight.bold), centerTitle: true, leading: IconButton( icon: const Icon(Icons.arrow_back_ios), onPressed: () async { if (!mounted) return; if (await _controller.canGoBack()) { _controller.goBack(); } else { if(context.mounted){ if (Navigator.canPop(context)) { Navigator.pop(context); } } } }, ), // actions: [ // IconButton( // icon: const Icon(Icons.refresh), // onPressed: () => _controller.reload(), // ), // ], ), body: Stack( children: [ WebViewWidget(controller: _controller), if (isLoading) LinearProgressIndicator( value: progress / 100, backgroundColor: Colors.grey[200], valueColor: AlwaysStoppedAnimation( Theme.of(context).primaryColor, ), ), ], ), ); } }