dreampad/lib/app/shared/utils/version_utils.dart

40 lines
1.1 KiB
Dart
Raw Permalink Normal View History

2023-11-28 10:44:58 +08:00
import 'dart:math';
import 'package:flutter/services.dart';
class VersionUtils {
static const MethodChannel _kChannel = MethodChannel('version');
/// 应用安装
static void install(String path) {
_kChannel.invokeMethod<void>('install', {'path': path});
}
/// AppStore跳转
static void jumpAppStore() {
_kChannel.invokeMethod<void>('jumpAppStore');
}
static bool compareVersion(curV, reqV) {
if (curV != null && reqV != null) {
//将两个版本号拆成数字
var arr1 = curV.split('.'), arr2 = reqV.split('.');
int arr1Len = arr1.length;
int arr2Len = arr2.length;
int minLength = min(arr1Len, arr2Len), position = 0, diff = 0;
// 依次比较版本号每一位大小,当对比得出结果后跳出循环(后文有简单介绍)
while (position < minLength &&
((diff = int.parse(arr1[position]) - int.parse(arr2[position])) ==
0)) {
position++;
}
diff = (diff != 0) ? diff : (arr1.length - arr2.length);
//若curV大于reqV则返回true
return diff > 0;
} else {
//输入为空
return false;
}
}
}