import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:yx_app_upgrade_flutter/app_upgrade_plugin.dart'; void main() { // 确保Flutter绑定已初始化 WidgetsFlutterBinding.ensureInitialized(); // 插件现在会自动配置证书验证: // - Debug模式:自动绕过证书验证 // - Release模式:严格证书验证 // 如需强制绕过证书(不推荐),可以使用: // AppUpgradePlugin.ignoreCertificate = true; // 或手动配置HTTP设置: // AppUpgradePlugin().configureHttp(HttpConfig.unsafe); // 延迟插件配置到Flutter完全初始化后 runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( // 这里是国际化支持,确保添加flutter_localizations依赖 supportedLocales: const [Locale('zh', 'CN')], localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], home: const HomePage(), ); } } class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { @override void initState() { super.initState(); // Use addPostFrameCallback to ensure the context is valid and mounted // after the first frame has been rendered. WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted) { _testNetworkFunctionality(); } }); } Future _testNetworkFunctionality() async { await AppUpgradeSimple.instance.checkUpdate( context: context, future: () async { final updateAppEvent = await _getUpdateAppEvent(); print("获取最新版本: $updateAppEvent"); if (updateAppEvent == null) return null; return _convertToAppUpgradeVersion(updateAppEvent); }, onComplete: (bool val) { print("更新插件执行完成....: $val"); }, config: UpgradeConfig.development, // showNoUpdateToast: false, // autoDownload: false, // autoInstall: true, ); } /// 获取最新版本 Future?> _getUpdateAppEvent() async { // 获取设备信息 // String deviceInfo; /// 1:安卓,2:IOS int? deviceType = Platform.isAndroid ? 1 : Platform.isIOS ? 2 : null; if (deviceType == null) return null; /// 支持更新内容 加粗、高亮、斜体、等 return { "id": 708608950206533, "version": 307, "versionName": "1.0.9", "remark": """ **布置工作:**上级可以向分属团队`布置任务`,设定类型和时间。\n **工作管理上:**__级可集中查看下级`所有工作`的详情与进度。__\n **学生详情:**__在[学生管理]中添加详情页,支持记录家长信息、学生备注等,完善学生档案。__\n""", "imageBase": null, "updatetype": 1, "isActive": 1, "fileid": 708608942190661, "fileName": "app-release(62).apk", "filePath": AppUpgradeVersion.getAppStoreByUrl('6747421483'), // "filePath": // "https://quanxue-oa.oss-cn-chengdu.aliyuncs.com/20251106/1762422545956.apk.1", "fileSize": 144214, "isforce": true }; } /// 将 UpdateappResult 转换为 AppUpgradeVersion AppUpgradeVersion _convertToAppUpgradeVersion(Map model) { // 将文件大小从 KB 转换为字节 final int? apkSizeBytes = model['fileSize'] != null ? model['fileSize'] * 1024 : null; final filePath = model['filePath']; final appUpgradeVersion = AppUpgradeVersion( versionName: model['versionName'], versionBuildNumber: model['version'], isForce: model['isforce'], updateContent: model['remark'], downloadUrl: filePath, appStoreUrl: filePath, apkSize: apkSizeBytes, apkMd5: null, // UpdateappResult 中没有 MD5 字段 // appMarkets: null, // UpdateappResult 中没有应用商店列表字段 supportedMethods: [AppUpgradeMethod.browser, AppUpgradeMethod.inApp, AppUpgradeMethod.market], // appMarkets: [ // AppMarket.huawei, // // AppMarket.xiaomi, // ], ); return appUpgradeVersion; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('App Upgrade Plugin 示例'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const SizedBox(height: 16), ElevatedButton( onPressed: () async { // 1. 静默检查更新(不显示任何 UI) final upgradeInfo = await AppUpgradeSimple.instance.silentCheckUpdate( future: () async { final updateAppEvent = await _getUpdateAppEvent(); print("获取最新版本: $updateAppEvent"); if (updateAppEvent == null) return null; return _convertToAppUpgradeVersion(updateAppEvent); }, ); // 2. 根据结果处理 if (upgradeInfo != null && upgradeInfo.hasUpdate) { // 有新版本,可以显示红点或在用户点击时调用弹窗 print('发现新版本: ${upgradeInfo.versionName}'); // 在需要展示弹窗的时机(如用户点击按钮): AppUpgradeSimple.instance.showPreparedUpgrade( context: context, info: upgradeInfo, // 传入刚才获取的 info ); } }, child: const Text('检查更新'), ), ], ), ), ); } }