dreampad/lib/app/shared/utils/version_utils.dart
2023-11-28 10:45:09 +08:00

40 lines
1.1 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}