Flutter App Updater
轻量级的Flutter应用内更新框架,专为不同项目需求设计,支持完全自定义UI和下载功能。
特点
- 轻量级:不依赖第三方库,完全使用Flutter原生功能
- 模块化:各个功能模块分离,易于维护和扩展
- 可定制:更新对话框UI可以完全自定义
- 功能完整:
- 支持强制更新和可选更新
- 下载进度显示
- 适应不同API响应格式
安装
在pubspec.yaml文件中添加依赖:
dependencies:
flutter_app_updater: ^2.1.0
然后运行:
flutter pub get
基本使用
import 'package:flutter_app_updater/flutter_app_updater.dart';
// 创建更新服务
final updater = FlutterAppUpdater(
updateUrl: "https://your-api.com/update.json",
versionKey: "newVersionCode", // 版本号字段
downloadUrlKey: "apkUrl", // 下载链接字段
changeLogKey: "updateMessage", // 更新日志字段
isForceUpdateKey: "forceUpdate" // 是否强制更新字段
);
// 初始化
avoid main() {
updater.init();
// ...
}
// 检查更新
void checkUpdate() async {
try {
// 检查更新,设置showDialogIfAvailable为false,手动控制对话框显示
final updateInfo = await updater.checkForUpdate(
showDialogIfAvailable: false,
);
if (updateInfo != null) {
print('新版本: ${updateInfo.newVersion}');
print('下载链接: ${updateInfo.downloadUrl}');
print('更新日志: ${updateInfo.changelog}');
// 显示更新对话框
await updater.showUpdateDialog(
context: context,
updateInfo: updateInfo,
);
} else {
print('已经是最新版本');
}
} catch (e) {
print('检查更新错误: $e');
}
}
自定义更新对话框
您可以完全自定义更新对话框的UI:
updater.showUpdateDialog(
context: context,
updateInfo: updateInfo,
dialogBuilder: (context, updateInfo) {
return AlertDialog(
title: Text('发现新版本 ${updateInfo.newVersion}'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('更新内容:'),
Text(updateInfo.changelog),
],
),
actions: [
if (!updateInfo.isForceUpdate)
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('稍后再说'),
),
TextButton(
onPressed: () async {
Navigator.pop(context);
// 开始下载更新
final file = await updater.downloadUpdate(
autoInstall: true, // 下载完成后自动安装
);
},
child: Text('立即更新'),
),
],
);
},
);
高级配置
配置重试策略
配置下载失败时的重试行为:
import 'package:flutter_app_updater/flutter_app_updater.dart';
// 使用预定义策略
final controller = UpdateController(
updateUrl: 'https://api.example.com/update.json',
);
final downloader = UpdateDownloader(
url: 'https://example.com/app.apk',
savePath: '/path/to/save/app.apk',
// 使用快速重试策略(5次,0.5秒起始延迟)
retryStrategy: RetryStrategy.fast,
);
// 自定义重试策略
final customDownloader = UpdateDownloader(
url: 'https://example.com/app.apk',
savePath: '/path/to/save/app.apk',
retryStrategy: const RetryStrategy(
maxAttempts: 5, // 最多重试5次
initialDelay: Duration(seconds: 2), // 首次重试延迟2秒
backoffFactor: 2.0, // 指数退避因子
maxDelay: Duration(minutes: 1), // 最大延迟1分钟
enableJitter: true, // 启用抖动避免同时重试
),
);
// 禁用重试
final noRetryDownloader = UpdateDownloader(
url: 'https://example.com/app.apk',
savePath: '/path/to/save/app.apk',
retryStrategy: RetryStrategy.disabled,
);
配置日志级别
控制日志输出级别:
import 'package:flutter_app_updater/flutter_app_updater.dart';
void main() {
// 设置日志级别
// none - 不输出任何日志
// error - 仅输出错误
// warning - 输出警告和错误
// info - 输出信息、警告和错误(推荐)
// debug - 输出所有日志(用于调试)
// 生产环境:仅错误
UpdateLogger.setLogLevel(LogLevel.error);
// 开发环境:详细调试
UpdateLogger.setLogLevel(LogLevel.debug);
runApp(MyApp());
}
// 也可以在代码中手动记录日志
void customLogging() {
UpdateLogger.debug('调试信息', tag: 'MyApp');
UpdateLogger.info('普通信息', tag: 'MyApp');
UpdateLogger.warning('警告信息', tag: 'MyApp');
UpdateLogger.error('错误信息', error: Exception('Something went wrong'), tag: 'MyApp');
}
初始化参数
// 初始化时可配置自动检查
await updater.init(
checkOnInit: true, // 初始化时检查更新
checkInterval: 24, // 每24小时自动检查一次(设为null禁用自动检查)
);
下载更新
final file = await updater.downloadUpdate(
savePath: "/storage/download/", // 保存路径
autoInstall: true, // 下载后自动安装
showNotification: true, // 显示下载通知
);
手动安装
await updater.installUpdate();
获取版本信息
String? platformVersion = await updater.getPlatformVersion();
String? appVersionCode = await updater.getAppVersionCode();
String? appVersionName = await updater.getAppVersionName();
资源释放
@override
void dispose() {
updater.dispose(); // 释放资源(定时器等)
super.dispose();
}
自定义API处理
您可以在FlutterAppUpdater构造函数中传入自定义的onCheckUpdate函数来覆盖默认的更新检查逻辑:
final updater = FlutterAppUpdater(
onCheckUpdate: () async {
// 自定义更新检查逻辑
final response = await customApiCall();
return UpdateInfo(
newVersion: response['version'],
downloadUrl: response['downloadUrl'],
changelog: response['releaseNotes'],
isForceUpdate: response['mandatory'] ?? false,
);
},
);
示例项目
查看example目录获取完整的示例应用。
贡献
欢迎提交问题和拉取请求,我们将尽快回应。
许可
MIT License