main.dart
2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'dart:convert';
import 'dart:io';
import 'package:appframe/config/constant.dart';
import 'package:appframe/config/env_config.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/services/im_service.dart';
import 'package:appframe/ui/widgets/ios_edge_swipe_detector.dart';
import 'package:archive/archive.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'app.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await setupLocator();
await _initH5Version();
await _initImSdk();
await _initIOSGesture();
runApp(const App());
}
Future<void> _initH5Version() async {
// 1 读取保存的H5版本
var h5Version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey);
if (h5Version != null && h5Version.isNotEmpty) {
Constant.h5Version = h5Version;
return;
}
// 2 读取基准包
try {
String path = EnvConfig.isDev() ? "assets/base-dev.zip" : "assets/base-pro.zip";
final byteData = await rootBundle.load(path);
final archive = ZipDecoder().decodeBytes(byteData.buffer.asUint8List());
// 查找 package.json 文件
for (final file in archive) {
if (file.name == 'package.json') {
// 将内容转换成字符串
final content = utf8.decode(file.content as List<int>);
var json = jsonDecode(content);
var h5Version = json['version'] ?? Constant.h5Version;
Constant.h5Version = h5Version;
getIt.get<SharedPreferences>().setString(Constant.h5ShowVersionKey, h5Version);
return;
}
}
} catch (e) {
Constant.h5Version = '0.0.0';
getIt.get<SharedPreferences>().setString(Constant.h5ShowVersionKey, '0.0.0');
debugPrint(e.toString());
}
}
Future<void> _initImSdk() async {
if (Constant.needIM) {
await getIt.get<ImService>().initSdk();
}
}
Future<void> _initIOSGesture() async {
if (Platform.isIOS) {
// ios边缘滑动检测
await IosEdgeSwipeDetector.init();
}
}