111
This commit is contained in:
parent
4b9fa0ee85
commit
3ec968802a
|
|
@ -2,7 +2,9 @@ import 'dart:io';
|
||||||
|
|
||||||
// 确保方法通道实现被导入,以便正确初始化
|
// 确保方法通道实现被导入,以便正确初始化
|
||||||
// ignore: unused_import
|
// ignore: unused_import
|
||||||
import 'app_upgrade_plugin_method_channel.dart';
|
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
|
||||||
import 'app_upgrade_plugin_platform_interface.dart';
|
import 'app_upgrade_plugin_platform_interface.dart';
|
||||||
import 'core/http_config.dart';
|
import 'core/http_config.dart';
|
||||||
import 'models/install_strategy.dart';
|
import 'models/install_strategy.dart';
|
||||||
|
|
@ -14,12 +16,12 @@ export 'app_upgrade_simple.dart';
|
||||||
export 'core/http_config.dart';
|
export 'core/http_config.dart';
|
||||||
// 权限帮助类
|
// 权限帮助类
|
||||||
export 'core/permission_helper.dart';
|
export 'core/permission_helper.dart';
|
||||||
export 'models/install_strategy.dart';
|
|
||||||
export 'models/upgrade_info.dart';
|
|
||||||
// 导出新定义的模型
|
|
||||||
export 'models/app_upgrade_version.dart';
|
|
||||||
// 导出升级方式枚举
|
// 导出升级方式枚举
|
||||||
export 'models/app_upgrade_method.dart';
|
export 'models/app_upgrade_method.dart';
|
||||||
|
// 导出新定义的模型
|
||||||
|
export 'models/app_upgrade_version.dart';
|
||||||
|
export 'models/install_strategy.dart';
|
||||||
|
export 'models/upgrade_info.dart';
|
||||||
// 导出市场选择对话框(其他对话框已整合到简化API中)
|
// 导出市场选择对话框(其他对话框已整合到简化API中)
|
||||||
export 'widgets/widgets.dart';
|
export 'widgets/widgets.dart';
|
||||||
|
|
||||||
|
|
@ -167,8 +169,8 @@ class AppUpgradePlugin {
|
||||||
|
|
||||||
/// 跳转到应用商店
|
/// 跳转到应用商店
|
||||||
/// [url] 应用商店地址
|
/// [url] 应用商店地址
|
||||||
Future<bool> goToAppStore(String url) {
|
Future<bool> goToAppStore(String url, {required BuildContext context}) {
|
||||||
return AppUpgradePluginPlatform.instance.goToAppStore(url);
|
return AppUpgradePluginPlatform.instance.goToAppStore(url, context: context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 获取下载目录路径
|
/// 获取下载目录路径
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,8 @@ import 'dart:io';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:dio/io.dart';
|
import 'package:dio/io.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:fluttertoast/fluttertoast.dart';
|
|
||||||
import 'package:package_info_plus/package_info_plus.dart';
|
import 'package:package_info_plus/package_info_plus.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
@ -487,7 +486,7 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> goToAppStore(String url) async {
|
Future<bool> goToAppStore(String url, {required BuildContext context}) async {
|
||||||
try {
|
try {
|
||||||
final uri = Uri.parse(url);
|
final uri = Uri.parse(url);
|
||||||
final bool flag = await canLaunchUrl(uri);
|
final bool flag = await canLaunchUrl(uri);
|
||||||
|
|
@ -495,7 +494,14 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform {
|
||||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Fluttertoast.showToast(msg: '当前APP没有上架当前设备对应的应用市场');
|
debugPrint('当前APP没有上架当前设备对应的应用市场');
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('当前APP没有上架当前设备对应的应用市场'),
|
||||||
|
duration: Duration(seconds: 2),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// Fluttertoast.showToast(msg: '当前APP没有上架当前设备对应的应用市场');
|
||||||
return false;
|
return false;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint('跳转应用商店失败: $e');
|
debugPrint('跳转应用商店失败: $e');
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||||
|
|
||||||
import 'app_upgrade_plugin_method_channel.dart';
|
import 'app_upgrade_plugin_method_channel.dart';
|
||||||
|
|
@ -82,7 +83,7 @@ abstract class AppUpgradePluginPlatform extends PlatformInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 跳转到应用商店
|
/// 跳转到应用商店
|
||||||
Future<bool> goToAppStore(String url) {
|
Future<bool> goToAppStore(String url, {required BuildContext context}) {
|
||||||
throw UnimplementedError('goToAppStore() has not been implemented.');
|
throw UnimplementedError('goToAppStore() has not been implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,11 +101,11 @@ abstract class AppUpgradePluginPlatform extends PlatformInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 检查应用市场是否可用(仅Android)
|
/// 检查应用市场是否可用(仅Android)
|
||||||
///
|
///
|
||||||
/// [packageName] 应用包名,如果为 null 则使用当前应用包名
|
/// [packageName] 应用包名,如果为 null 则使用当前应用包名
|
||||||
/// [marketPackage] 指定的应用市场包名(可选)
|
/// [marketPackage] 指定的应用市场包名(可选)
|
||||||
/// [url] 备用 URL(可选)
|
/// [url] 备用 URL(可选)
|
||||||
///
|
///
|
||||||
/// 返回 true 表示有应用可以处理应用市场链接
|
/// 返回 true 表示有应用可以处理应用市场链接
|
||||||
Future<bool> checkMarketAvailable({
|
Future<bool> checkMarketAvailable({
|
||||||
String? packageName,
|
String? packageName,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:fluttertoast/fluttertoast.dart';
|
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
import 'app_upgrade_plugin_platform_interface.dart';
|
import 'app_upgrade_plugin_platform_interface.dart';
|
||||||
|
|
@ -33,8 +32,8 @@ class _SimpleAppUpgradePlugin {
|
||||||
return AppUpgradePluginPlatform.instance.installApk(filePath);
|
return AppUpgradePluginPlatform.instance.installApk(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> goToAppStore(String url) {
|
Future<bool> goToAppStore(String url, {required BuildContext context}) {
|
||||||
return AppUpgradePluginPlatform.instance.goToAppStore(url);
|
return AppUpgradePluginPlatform.instance.goToAppStore(url, context: context);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, String>> getAppInfo() {
|
Future<Map<String, String>> getAppInfo() {
|
||||||
|
|
@ -283,7 +282,7 @@ class AppUpgradeSimple {
|
||||||
|
|
||||||
if (!info.hasUpdate) {
|
if (!info.hasUpdate) {
|
||||||
if (finalShowNoUpdateToast && context.mounted) {
|
if (finalShowNoUpdateToast && context.mounted) {
|
||||||
_showToast('已是最新版本', effectiveConfig);
|
_showToast('已是最新版本', context, effectiveConfig);
|
||||||
}
|
}
|
||||||
onComplete?.call();
|
onComplete?.call();
|
||||||
return;
|
return;
|
||||||
|
|
@ -301,7 +300,7 @@ class AppUpgradeSimple {
|
||||||
debugPrint('检查更新失败: $e');
|
debugPrint('检查更新失败: $e');
|
||||||
|
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
_showToast('检查更新遇到问题', effectiveConfig);
|
_showToast('检查更新遇到问题', context, effectiveConfig);
|
||||||
}
|
}
|
||||||
onComplete?.call();
|
onComplete?.call();
|
||||||
}
|
}
|
||||||
|
|
@ -460,7 +459,7 @@ class AppUpgradeSimple {
|
||||||
autoInstall: autoInstall,
|
autoInstall: autoInstall,
|
||||||
onComplete: onComplete,
|
onComplete: onComplete,
|
||||||
config: effectiveConfig,
|
config: effectiveConfig,
|
||||||
showToast: (message) => _showToast(message, effectiveConfig),
|
showToast: (message) => _showToast(message, context, effectiveConfig),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -468,18 +467,16 @@ class AppUpgradeSimple {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 显示Toast提示
|
/// 显示Toast提示
|
||||||
void _showToast(String message, [UpgradeConfig? config]) {
|
void _showToast(String message, BuildContext context, [UpgradeConfig? config]) {
|
||||||
final effectiveConfig = config ?? _config;
|
final effectiveConfig = config ?? _config;
|
||||||
if (effectiveConfig.customToast != null) {
|
if (effectiveConfig.customToast != null) {
|
||||||
effectiveConfig.customToast!(message);
|
effectiveConfig.customToast!(message);
|
||||||
} else {
|
} else {
|
||||||
Fluttertoast.showToast(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
msg: message,
|
SnackBar(
|
||||||
toastLength: Toast.LENGTH_SHORT,
|
content: Text('这是一个SnackBar提示'),
|
||||||
gravity: ToastGravity.CENTER,
|
duration: Duration(seconds: 2),
|
||||||
backgroundColor: Colors.black87,
|
),
|
||||||
textColor: Colors.white,
|
|
||||||
fontSize: 14.0,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1515,15 +1512,15 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
if (Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
_handleAndroidAction();
|
_handleAndroidAction();
|
||||||
} else if (Platform.isIOS) {
|
} else if (Platform.isIOS) {
|
||||||
_handleIosAction();
|
_handleIosAction(context);
|
||||||
} else {
|
} else {
|
||||||
showToast('Unsupported platform');
|
showToast('Unsupported platform');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleIosAction() {
|
void _handleIosAction(BuildContext context) {
|
||||||
if (info.appStoreUrl != null) {
|
if (info.appStoreUrl != null) {
|
||||||
_plugin.goToAppStore(info.appStoreUrl!);
|
_plugin.goToAppStore(info.appStoreUrl!, context: context);
|
||||||
// 移除关闭弹窗代码,始终不关闭
|
// 移除关闭弹窗代码,始终不关闭
|
||||||
onComplete?.call();
|
onComplete?.call();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1581,14 +1578,14 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
|
||||||
context,
|
context,
|
||||||
markets: info.appMarkets!,
|
markets: info.appMarkets!,
|
||||||
onSelected: (market) {
|
onSelected: (market) {
|
||||||
_plugin.goToAppStore(market.url ?? market.packageName ?? '');
|
_plugin.goToAppStore(market.url ?? market.packageName ?? '', context: context);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
final appInfo = await _plugin.getAppInfo();
|
final appInfo = await _plugin.getAppInfo();
|
||||||
final pkg = appInfo['packageName'] ?? '';
|
final pkg = appInfo['packageName'] ?? '';
|
||||||
if (pkg.isNotEmpty) {
|
if (pkg.isNotEmpty) {
|
||||||
_plugin.goToAppStore('market://details?id=$pkg');
|
_plugin.goToAppStore('market://details?id=$pkg', context: context);
|
||||||
// _plugin.goToAppStore('market://details?id=com.yuanxuan.learningOfficerOa');
|
// _plugin.goToAppStore('market://details?id=com.yuanxuan.learningOfficerOa');
|
||||||
} else {
|
} else {
|
||||||
showToast('Could not determine app package name.');
|
showToast('Could not determine app package name.');
|
||||||
|
|
@ -1866,7 +1863,8 @@ class _ForceUpgradeDialogState extends State<_ForceUpgradeDialog> with _UpgradeD
|
||||||
@override
|
@override
|
||||||
UpgradeInfo get info => widget.info;
|
UpgradeInfo get info => widget.info;
|
||||||
@override
|
@override
|
||||||
void Function(String) get showToast => (message) => AppUpgradeSimple.instance._showToast(message, widget.config);
|
void Function(String) get showToast =>
|
||||||
|
(message) => AppUpgradeSimple.instance._showToast(message, context, widget.config);
|
||||||
@override
|
@override
|
||||||
VoidCallback? get onComplete => null;
|
VoidCallback? get onComplete => null;
|
||||||
@override
|
@override
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ dependencies:
|
||||||
shared_preferences: ^2.3.3
|
shared_preferences: ^2.3.3
|
||||||
flutter_local_notifications: ^18.0.1
|
flutter_local_notifications: ^18.0.1
|
||||||
device_info_plus: ^11.2.0
|
device_info_plus: ^11.2.0
|
||||||
fluttertoast: ^9.0.0
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ class MockAppUpgradePluginPlatform extends AppUpgradePluginPlatform with MockPla
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<bool> goToAppStore(String url) async {
|
Future<bool> goToAppStore(String url, {required BuildContext context}) async {
|
||||||
goToAppStoreCalled = true;
|
goToAppStoreCalled = true;
|
||||||
lastUrl = url;
|
lastUrl = url;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue