yx_app_upgrade_flutter/example/lib/main_enhanced.dart

350 lines
10 KiB
Dart

import 'package:app_upgrade_plugin/app_upgrade_plugin.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App Upgrade Plugin Enhanced Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'App升级插件完整示例'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _status = '准备就绪';
bool _isLoading = false;
@override
void initState() {
super.initState();
_initializePlugin();
}
/// 初始化插件
void _initializePlugin() {
// 配置插件
AppUpgradeSimple.instance.configure(const UpgradeConfig(
enableDebugLog: true,
installTimeout: 60,
customToast: null, // 使用默认Toast
));
}
/// 基础检查更新
Future<void> _checkUpdate() async {
setState(() {
_isLoading = true;
_status = '检查更新中...';
});
try {
await AppUpgradeSimple.instance.checkUpdate(
context: context,
url: 'https://api.example.com/check-update',
params: {
'platform': 'android',
'channel': 'official',
},
onComplete: () {
setState(() {
_isLoading = false;
_status = '检查完成';
});
},
);
} catch (e) {
setState(() {
_isLoading = false;
_status = '检查失败: $e';
});
}
}
/// 自动更新
Future<void> _autoUpdate() async {
setState(() {
_isLoading = true;
_status = '自动更新中...';
});
try {
await AppUpgradeSimple.instance.checkUpdate(
context: context,
url: 'https://api.example.com/check-update',
config: UpgradeConfig.auto,
onComplete: () {
setState(() {
_isLoading = false;
_status = '自动更新完成';
});
},
);
} catch (e) {
setState(() {
_isLoading = false;
_status = '自动更新失败: $e';
});
}
}
/// 静默检查
Future<void> _silentCheck() async {
setState(() {
_isLoading = true;
_status = '静默检查中...';
});
final info = await AppUpgradeSimple.instance.checkUpdateSilent(
url: 'https://api.example.com/check-update',
);
setState(() {
_isLoading = false;
if (info != null && info.hasUpdate) {
_status = '发现新版本: ${info.versionName}';
} else {
_status = '已是最新版本';
}
});
}
/// 清理缓存
Future<void> _clearCache() async {
setState(() {
_isLoading = true;
_status = '清理缓存中...';
});
await AppUpgradeSimple.instance.clearDownloadCache();
setState(() {
_isLoading = false;
_status = '缓存清理完成';
});
}
/// 检查网络状态
Future<void> _checkNetwork() async {
setState(() {
_isLoading = true;
_status = '检查网络中...';
});
final hasNetwork = await AppUpgradeSimple.instance.checkNetworkStatus();
setState(() {
_isLoading = false;
_status = hasNetwork ? '网络连接正常' : '网络连接异常';
});
}
/// 获取应用信息
Future<void> _getAppInfo() async {
setState(() {
_isLoading = true;
_status = '获取应用信息中...';
});
final appInfo = await AppUpgradeSimple.instance.getAppInfo();
setState(() {
_isLoading = false;
_status = '应用信息: ${appInfo['appName']} v${appInfo['version']}';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
elevation: 2,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// 状态显示卡片
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'当前状态',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Row(
children: [
if (_isLoading)
const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
),
if (_isLoading) const SizedBox(width: 8),
Expanded(
child: Text(
_status,
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
],
),
),
),
const SizedBox(height: 20),
// 基础功能
Text(
'基础功能',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _isLoading ? null : _checkUpdate,
icon: const Icon(Icons.system_update),
label: const Text('检查更新'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading ? null : _autoUpdate,
icon: const Icon(Icons.auto_awesome),
label: const Text('自动更新'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading ? null : _silentCheck,
icon: const Icon(Icons.visibility_off),
label: const Text('静默检查'),
),
const SizedBox(height: 20),
// 工具功能
Text(
'工具功能',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _isLoading ? null : _checkNetwork,
icon: const Icon(Icons.wifi),
label: const Text('检查网络'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading ? null : _getAppInfo,
icon: const Icon(Icons.info),
label: const Text('应用信息'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading ? null : _clearCache,
icon: const Icon(Icons.cleaning_services),
label: const Text('清理缓存'),
),
const SizedBox(height: 20),
// 配置示例
Text(
'配置示例',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 12),
ElevatedButton.icon(
onPressed: _isLoading
? null
: () {
AppUpgradeSimple.instance.enableAutoUpdate();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已启用自动更新模式')),
);
},
icon: const Icon(Icons.auto_mode),
label: const Text('启用自动更新'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading
? null
: () {
AppUpgradeSimple.instance.enableSilentCheck();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已启用静默检查模式')),
);
},
icon: const Icon(Icons.volume_off),
label: const Text('启用静默模式'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading
? null
: () {
AppUpgradeSimple.instance.configure(UpgradeConfig(
autoDownload: true,
autoInstall: false,
installTimeout: 60,
requireInstallPermission: false, // 不需要权限
customToast: (message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: Colors.deepPurple,
),
);
},
));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已配置自定义设置(无权限模式)')),
);
},
icon: const Icon(Icons.settings),
label: const Text('自定义配置'),
),
const SizedBox(height: 8),
ElevatedButton.icon(
onPressed: _isLoading
? null
: () {
AppUpgradeSimple.instance.configure(UpgradeConfig.withPermission);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('已启用权限模式')),
);
},
icon: const Icon(Icons.security),
label: const Text('启用权限模式'),
),
],
),
),
);
}
}