-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.dart
130 lines (119 loc) · 3.62 KB
/
main.dart
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import 'package:flutter/material.dart';
import 'package:flutter_app_update/flutter_app_update.dart';
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('一个简单好用的版本更新库')),
body: const HomePage(),
),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String url =
"https://imtt.dd.qq.com/16891/apk/FA48766BA12A41A1D619CB4B152889C6.apk?fsname=com.estrongs.android.pop_4.2.3.3_10089.apk&csr=1bbd";
@override
void initState() {
super.initState();
AzhonAppUpdate.listener((map) {
debugPrint(map['type']);
});
}
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_item('更新', () {
_showUpdateDialog(false);
}),
_item('强制更新', () {
_showUpdateDialog(true);
}),
_item('取消下载', () {
AzhonAppUpdate.cancel.then((value) {
debugPrint('取消下载结果 = $value');
});
}),
const Divider(height: 10),
_item('获取VersionCode', () {
AzhonAppUpdate.getVersionCode.then((value) {
debugPrint('获取到的versionCode = $value');
});
}),
_item('获取VersionName', () {
AzhonAppUpdate.getVersionName.then((value) {
debugPrint('获取到的versionName = $value');
});
}),
],
),
);
}
///Flutter侧处理升级对话框
///[forcedUpgrade] 是否强制升级
_showUpdateDialog(bool forcedUpgrade) {
showDialog(
context: context,
barrierDismissible: !forcedUpgrade,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () => Future.value(!forcedUpgrade),
child: AlertDialog(
title: const Text('发现新版本'),
content: const Text(
'1.支持Android4.1及以上版本\n2.支持自定义下载过程\n3.支持通知栏进度条展示\n4.支持文字国际化\n5.使用Kotlin协程重构'),
actions: <Widget>[
if (!forcedUpgrade)
TextButton(
child: const Text('取消'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: const Text('升级'),
onPressed: () {
_appUpdate();
if (!forcedUpgrade) {
Navigator.of(context).pop();
}
},
),
],
),
);
},
);
}
_appUpdate() {
UpdateModel model = UpdateModel(
url,
"flutterUpdate.apk",
"ic_launcher",
'https://itunes.apple.com/cn/app/抖音/id1142110895',
);
AzhonAppUpdate.update(model).then((value) => debugPrint('$value'));
}
Widget _item(String text, VoidCallback onPressed) {
return SizedBox(
width: double.infinity,
child: TextButton(
style: TextButton.styleFrom(backgroundColor: Colors.blue),
child: Text(text, style: const TextStyle(color: Colors.white)),
onPressed: () => onPressed.call(),
),
);
}
}