import 'dart:async'; import 'package:ef/ef.dart'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:vbvs_app/common/color/appConstants.dart'; import 'package:vbvs_app/common/util/FitTool.dart'; import 'package:vbvs_app/common/util/MyUtils.dart'; import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart'; import 'package:vbvs_app/controller/device/device_type_controller.dart'; import 'package:vbvs_app/controller/main_bottom/global_controller.dart'; import 'package:vbvs_app/controller/theme_controller/ThemeController.dart'; import 'package:vbvs_app/controller/user_info_controller.dart'; import 'package:vbvs_app/enum/APPPackageType.dart'; import 'package:flutter/services.dart'; class PrivacyPolicyNewPage extends StatefulWidget { var sleepUri; bool showAppbar; PrivacyPolicyNewPage( {super.key, required this.sleepUri, this.showAppbar = true}); @override State createState() => _PrivacyPolicyNewPageState(); } class _PrivacyPolicyNewPageState extends State { GlobalController globalController = Get.find(); UserInfoController userInfoController = Get.find(); BlueteethBindController blueteethBindController = Get.find(); ThemeController themeController = Get.find(); DeviceTypeController deviceTypeController = Get.find(); ValueNotifier isPageLoading = ValueNotifier(true); ValueNotifier loadFailed = ValueNotifier(false); Timer? _timeoutTimer; InAppWebViewController? _webViewController; @override void initState() { super.initState(); // 启动5秒超时计时器 _startTimeoutTimer(); } void _startTimeoutTimer() { // 先清除之前的计时器 _timeoutTimer?.cancel(); // 设置5秒超时 _timeoutTimer = Timer(Duration(seconds: 5), () { if (isPageLoading.value) { // 5秒后仍在加载,显示加载失败 isPageLoading.value = false; loadFailed.value = true; } }); } void _retryLoad() { // 重置状态 loadFailed.value = false; isPageLoading.value = true; // 重新启动超时计时器 _startTimeoutTimer(); // 重新加载页面 if (_webViewController != null) { _webViewController!.reload(); } else { // 如果控制器还未初始化,需要重新构建webview setState(() {}); } } @override void dispose() { // 清理所有资源 _timeoutTimer?.cancel(); isPageLoading.dispose(); loadFailed.dispose(); _webViewController = null; super.dispose(); } @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, bodySize) => GestureDetector( child: Container( color: Colors.white, child: Scaffold( backgroundColor: Colors.transparent, appBar: (widget.showAppbar != null && widget.showAppbar == false) ? null : AppBar( systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: Colors.transparent, // 状态栏背景色 statusBarIconBrightness: Brightness.light, // 图标颜色(Android) statusBarBrightness: Brightness.light, // 图标颜色(iOS) ), backgroundColor: themeController.currentColor.sc17, automaticallyImplyLeading: false, iconTheme: IconThemeData(color: themeController.currentColor.sc3), titleSpacing: 0, title: Container( width: double.infinity, height: 180.rpx, child: Stack( alignment: Alignment.center, children: [ /// 居中标题 Text( '隐私协议'.tr, style: TextStyle( fontFamily: 'Readex Pro', color: themeController.currentColor.sc3, letterSpacing: 0, fontSize: 30.rpx, ), ), /// 左边返回按钮 Positioned( left: 0, child: returnIconButtomNew(), ), ], ), ), ), body: SafeArea( top: true, child: ValueListenableBuilder( valueListenable: loadFailed, builder: (context, failed, child) { if (failed) { // 显示加载失败页面 if (AppConstants().ent_type == APPPackageType.MHT.code) { return _buildErrorView(); } return _buildTHErrorView(); } else { // 显示WebView或加载指示器 return _buildWebView(); } }, ), ), ), ), ), ); } Widget _buildWebView() { return Stack( children: [ Padding( padding: EdgeInsets.fromLTRB(0, 0, 0.rpx, 0), child: InAppWebView( key: UniqueKey(), initialUrlRequest: URLRequest(url: WebUri(widget.sleepUri)), onWebViewCreated: (controller) { _webViewController = controller; }, onLoadStart: (controller, url) { // 页面开始加载时显示加载指示器 isPageLoading.value = true; loadFailed.value = false; }, onLoadStop: (controller, url) { // 页面加载完成后隐藏加载指示器 isPageLoading.value = false; loadFailed.value = false; _timeoutTimer?.cancel(); // 加载成功时取消计时器 }, onLoadError: (controller, url, code, message) { // 加载出错时显示失败页面 isPageLoading.value = false; loadFailed.value = true; _timeoutTimer?.cancel(); // 出错时取消计时器 }, ), ), ValueListenableBuilder( valueListenable: isPageLoading, builder: (context, isLoading, child) { return isLoading ? Center( child: CircularProgressIndicator( strokeWidth: 2, valueColor: AlwaysStoppedAnimation( themeController.currentColor.sc1, ), ), ) : SizedBox.shrink(); }, ), ], ); } //眠花糖 Widget _buildErrorView() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.wifi_off, size: 60.rpx, color: stringToColor("84F5FF"), ), SizedBox(height: 20.rpx), Text( '隐私协议加载失败'.tr, style: TextStyle( fontSize: 28.rpx, color: Colors.black, ), ), Text( '请检查网络连接后重试'.tr, style: TextStyle( fontSize: 24.rpx, color: Colors.black, ), ), SizedBox(height: 30.rpx), ElevatedButton( onPressed: _retryLoad, style: ElevatedButton.styleFrom( backgroundColor: stringToColor("84F5FF"), foregroundColor: Colors.white, padding: EdgeInsets.symmetric( horizontal: 40.rpx, vertical: 15.rpx, ), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(25.rpx), ), ), child: Text( '重试'.tr, style: TextStyle( fontSize: 26.rpx, color: Colors.black, ), ), ), ], ), ); } //太和 Widget _buildTHErrorView() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.wifi_off, size: 60.rpx, // color: stringToColor("84F5FF"), color: themeController.currentColor.sc2, ), SizedBox(height: 20.rpx), Text( '隐私协议加载失败'.tr, style: TextStyle( fontSize: AppConstants().title_text_fontSize, color: Colors.black, ), ), Text( '请检查网络连接后重试'.tr, style: TextStyle( fontSize: 24.rpx, color: Colors.black, ), ), SizedBox(height: 30.rpx), ElevatedButton( onPressed: _retryLoad, style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.transparent), foregroundColor: MaterialStateProperty.all(Colors.white), padding: MaterialStateProperty.all( EdgeInsets.symmetric( horizontal: 40.rpx, vertical: 15.rpx, ), ), shape: MaterialStateProperty.all( RoundedRectangleBorder( borderRadius: BorderRadius.circular(25.rpx), ), ), overlayColor: MaterialStateProperty.all(Colors.white.withOpacity(0.2)), elevation: MaterialStateProperty.all(0), ), child: Container( decoration: BoxDecoration( gradient: LinearGradient( colors: [ // 这里设置您的渐变色 themeController.currentColor.sc2, themeController.currentColor.sc1, // 假设这是另一种颜色 ], begin: Alignment.topLeft, end: Alignment.bottomRight, ), borderRadius: BorderRadius.circular(25.rpx), ), padding: EdgeInsets.symmetric( horizontal: 40.rpx, vertical: 15.rpx, ), child: Text( '重试'.tr, style: TextStyle( fontSize: 26.rpx, color: Colors.white, // 建议白色文字,渐变背景下更清晰 ), ), ), ), ], ), ); } }