privacy_agreement_page.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:webview_flutter/webview_flutter.dart';
  4. import 'package:webview_flutter_android/webview_flutter_android.dart';
  5. import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
  6. import '../../widget/my_txt.dart';
  7. class PrivacyAgreementPage extends StatefulWidget {
  8. const PrivacyAgreementPage({super.key});
  9. @override
  10. State<PrivacyAgreementPage> createState() => _PrivacyAgreementPageState();
  11. }
  12. class _PrivacyAgreementPageState extends State<PrivacyAgreementPage> {
  13. String url = "https://h5.xhrbxxf.com/privacy";
  14. late final WebViewController _controller;
  15. var isLoading = true;
  16. var progress = 0;
  17. @override
  18. void initState() {
  19. super.initState();
  20. // 平台特定的初始化
  21. late final PlatformWebViewControllerCreationParams params;
  22. if (WebViewPlatform.instance is WebKitWebViewPlatform) {
  23. params = WebKitWebViewControllerCreationParams(
  24. allowsInlineMediaPlayback: true,
  25. mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
  26. );
  27. } else {
  28. params = const PlatformWebViewControllerCreationParams();
  29. }
  30. final WebViewController controller =
  31. WebViewController.fromPlatformCreationParams(params);
  32. // 通用设置
  33. controller
  34. ..setJavaScriptMode(JavaScriptMode.unrestricted)
  35. ..setNavigationDelegate(
  36. NavigationDelegate(
  37. onProgress: (int progress) {
  38. if (!mounted) return;
  39. setState(() {
  40. this.progress = progress;
  41. if (progress == 100) {
  42. isLoading = false;
  43. }
  44. });
  45. },
  46. onPageStarted: (String url) {
  47. if (!mounted) return;
  48. setState(() => isLoading = true);
  49. },
  50. onPageFinished: (String url) {
  51. if (!mounted) return;
  52. setState(() => isLoading = false);
  53. },
  54. onWebResourceError: (WebResourceError error) {
  55. if (!mounted) return;
  56. debugPrint('''
  57. WebView error:
  58. code: ${error.errorCode}
  59. description: ${error.description}
  60. errorType: ${error.errorType}
  61. isForMainFrame: ${error.isForMainFrame}
  62. ''');
  63. },
  64. ),
  65. )
  66. ..loadRequest(Uri.parse(url));
  67. // Android 特定设置
  68. if (controller.platform is AndroidWebViewController) {
  69. AndroidWebViewController.enableDebugging(true);
  70. (controller.platform as AndroidWebViewController)
  71. .setMediaPlaybackRequiresUserGesture(false);
  72. }
  73. _controller = controller;
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return Scaffold(
  78. backgroundColor: Colors.white,
  79. resizeToAvoidBottomInset: false,
  80. appBar: AppBar(
  81. title: myTxt(text: '用户协议和隐私政策', fontSize: 18.sp, fontWeight: FontWeight.bold),
  82. centerTitle: true,
  83. leading: IconButton(
  84. icon: const Icon(Icons.arrow_back_ios),
  85. onPressed: () async {
  86. if (!mounted) return;
  87. if (await _controller.canGoBack()) {
  88. _controller.goBack();
  89. } else {
  90. if(context.mounted){
  91. if (Navigator.canPop(context)) {
  92. Navigator.pop(context);
  93. }
  94. }
  95. }
  96. },
  97. ),
  98. // actions: [
  99. // IconButton(
  100. // icon: const Icon(Icons.refresh),
  101. // onPressed: () => _controller.reload(),
  102. // ),
  103. // ],
  104. ),
  105. body: Stack(
  106. children: [
  107. WebViewWidget(controller: _controller),
  108. if (isLoading)
  109. LinearProgressIndicator(
  110. value: progress / 100,
  111. backgroundColor: Colors.grey[200],
  112. valueColor: AlwaysStoppedAnimation<Color>(
  113. Theme.of(context).primaryColor,
  114. ),
  115. ),
  116. ],
  117. ),
  118. );
  119. }
  120. }