import 'package:app_upgrade_plugin/app_upgrade_simple.dart'; import 'package:flutter/material.dart'; /// 最简单的使用示例 /// 展示如何用最少的代码实现App升级功能 void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'App Upgrade Simple Example', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { // 模拟的更新检查URL(实际使用时替换为真实的API地址) static const String checkUpdateUrl = 'https://api.example.com/check-update'; @override void initState() { super.initState(); // 可选:启动时自动检查更新(静默检查) _checkUpdateOnStart(); } /// 启动时静默检查更新 void _checkUpdateOnStart() async { // 延迟2秒,避免启动时界面还未完全加载 await Future.delayed(const Duration(seconds: 2)); if (mounted) { // 静默检查,有更新才显示对话框 final info = await AppUpgradeSimple.instance.checkUpdateSilent( url: checkUpdateUrl, ); if (info != null && mounted) { // 有更新时显示对话框 AppUpgradeSimple.instance.checkUpdate( context: context, url: checkUpdateUrl, showNoUpdateToast: false, // 不显示"已是最新版本"提示 autoDownload: false, // 不自动下载,让用户选择 ); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('App升级 - 极简示例'), backgroundColor: Theme.of(context).colorScheme.inversePrimary, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.rocket_launch, size: 80, color: Colors.blue, ), const SizedBox(height: 24), const Text( '最简单的App升级实现', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), const SizedBox(height: 8), const Text( '一行代码搞定升级功能', style: TextStyle(fontSize: 16, color: Colors.grey), ), const SizedBox(height: 48), // 示例1:最简单的使用方式(一行代码) ElevatedButton.icon( onPressed: () { // 🚀 一行代码检查更新! AppUpgradeSimple.instance.checkUpdate( context: context, url: checkUpdateUrl, ); }, icon: const Icon(Icons.flash_on), label: const Text('一键检查更新(最简单)'), style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12), ), ), const SizedBox(height: 16), const SizedBox(height: 16), const SizedBox(height: 48), // 使用说明 Container( margin: const EdgeInsets.symmetric(horizontal: 32), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.blue.shade50, borderRadius: BorderRadius.circular(12), border: Border.all(color: Colors.blue.shade200), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: const [ Text( '💡 使用提示', style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Colors.blue, ), ), SizedBox(height: 8), Text('1. 替换 checkUpdateUrl 为您的API地址'), Text('2. API返回格式请参考文档'), Text('3. Android需要配置权限和FileProvider'), Text('4. iOS需要配置App Store地址'), ], ), ), ], ), ), ); } } /// API返回格式示例: /// ```json /// { /// "hasUpdate": true, /// "isForceUpdate": false, /// "versionCode": "2", /// "versionName": "1.1.0", /// "updateContent": "1. 修复已知问题\n2. 优化用户体验", /// "downloadUrl": "https://example.com/app-v1.1.0.apk", // Android /// "appStoreUrl": "https://apps.apple.com/app/id123456", // iOS /// "apkSize": 26214400, /// "apkMd5": "abc123def456" /// } /// ```