Flutter强制更新插件flutter_force_update的使用
Flutter强制更新插件flutter_force_update的使用
特性
A flutter widget to show the user if there is an update available of your app from an api endpoint.
特性
- 一个用于显示应用是否有更新的widget。
- 注意:这是一个widget,而不是页面(不是路由),所以请将此widget作为叠加层使用(详见底部示例)。
开始使用
无需额外依赖。
只需安装包即可开始使用。
使用方法
通过提供的ForceUpdatePrompt()
widget来展示应用是否有更新。
示例
ForceUpdatePrompt(
androidVersionName: "2.33.4533",
androidVersionCode: "22",
iosVersionName: "4.34.5434",
iosVersionCode: "43",
androidPriorityIsHigh: true,
iosPriorityIsHigh: false,
backgroundColor: Colors.purple,
logo: Image.asset("asset/new/icon_app.png"),
transparentBackground: true,
message: "New version available",
updateButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: MyButton(
text: "Update Now",
),
),
skipButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: MyButton(
text: "Skip",
),
),
);
实现validate()
方法
它有一个辅助方法ForceUpdateUtils.getRemoteUpdateStatus()
,如果您想从API端点获取最新版本信息。
示例
ForceUpdateUtils.getRemoteUpdateStatus("https://62b43f19530b26da4cb9fcc0.mockapi.io/check-update")
如果要使用此内置助手,您的响应必须如下所示:
[
{
"message": "New Update is available!",
"android_version_name": "1.1.20220624",
"android_version_code": "17",
"android_priority_is_high": false,
"ios_version_name": "1.1.20220624",
"ios_version_code": "17",
"ios_priority_is_high": true
}
]
由于助手使用了特定JSON格式的数据模型类。
完整示例
注意,该widget被用作叠加层。
如果您想在客户端决定是否更新
Stack(
children: [
isLoading
? Splash()
: isRegistered
? Home()
: isAuth
? ProfileNameImageDOB()
: IntroPage(),
FutureBuilder(
future: ForceUpdateUtils.getRemoteUpdateStatus("https://62b43f19530b26da4cb9fcc0.mockapi.io/check-update"),
builder: (context, AsyncSnapshot<ForceUpdateRemoteData?> snapshot){
if(snapshot.hasData && snapshot.data != null){
return ForceUpdatePrompt(
androidVersionName: snapshot.data!.androidVersionName,
androidVersionCode: snapshot.data!.androidVersionCode,
iosVersionName: snapshot.data!.iosVersionName,
iosVersionCode: snapshot.data!.iosVersionCode,
androidPriorityIsHigh: snapshot.data!.androidPriorityIsHigh,
iosPriorityIsHigh: snapshot.data!.iosPriorityIsHigh,
backgroundColor: Colors.purple,
logo: Image.asset("asset/new/icon_app.png"),
transparentBackground: true,
message: snapshot.data!.message ?? "New version available",
updateButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: MyButton(
text: "Update Now",
),
),
skipButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: MyButton(
text: "Skip",
),
),
);
}
return const Center(child: CircularProgressIndicator());
} ,
),
],
);
如果您想在服务器端决定是否更新
FutureBuilder(
future: ForceUpdateUtils.postLocalInfoForRemoteDecision(
"https://youryrlhere.com/check-for-update"),
builder: (context, sn) {
if (sn.hasData && sn.data != null) {
ForceUpdateRemoteDecision data = sn.data as ForceUpdateRemoteDecision;
return UpdateWidget(
transparentBackground: true,
backgroundColor: Colors.red,
logo: Image.network("https://picsum.photos/200"),
message: data.message ?? "New version available",
textColor: Colors.purple,
updateButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: MyButton(
text: "Update Now",
),
),
skipButton: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: MyButton(
text: "Skip",
),
),
isPriorityHigh: data.isMajor ?? false,
);
}
return Text("Loading");
},
)
更多关于Flutter强制更新插件flutter_force_update的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter强制更新插件flutter_force_update的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_force_update
是一个用于强制更新 Flutter 应用的插件。它通常用于在应用发布新版本后,强制用户更新到最新版本,以确保他们使用的是最新的功能和安全补丁。
安装
首先,你需要在 pubspec.yaml
文件中添加 flutter_force_update
插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_force_update: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
使用
-
初始化插件
在你的
main.dart
文件中,初始化flutter_force_update
插件。通常你需要在应用启动时检查是否需要强制更新。import 'package:flutter/material.dart'; import 'package:flutter_force_update/flutter_force_update.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); // 初始化插件 final forceUpdate = ForceUpdate( // 设置你的应用在应用商店的 URL appStoreUrl: 'https://apps.apple.com/app/idYOUR_APP_ID', playStoreUrl: 'market://details?id=YOUR_PACKAGE_NAME', // 设置当前应用版本 currentVersion: '1.0.0', // 设置最低要求的版本 minimumVersion: '1.0.1', ); // 检查是否需要强制更新 final isUpdateRequired = await forceUpdate.isUpdateRequired(); runApp(MyApp(isUpdateRequired: isUpdateRequired)); } class MyApp extends StatelessWidget { final bool isUpdateRequired; MyApp({required this.isUpdateRequired}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Force Update Example', home: isUpdateRequired ? ForceUpdateScreen() : HomeScreen(), ); } }
-
强制更新界面
如果检测到需要强制更新,你可以显示一个强制更新界面,提示用户更新应用。
class ForceUpdateScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('请更新应用以继续使用'), ElevatedButton( onPressed: () { // 打开应用商店进行更新 ForceUpdate.launchStore(); }, child: Text('更新'), ), ], ), ), ); } }
-
普通界面
如果不需要强制更新,可以显示正常的应用界面。
class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home'), ), body: Center( child: Text('欢迎使用应用!'), ), ); } }