Skip to content

Commit 05dd5fa

Browse files
committed
优化更新时安装包下载进度展示
1 parent eeb904a commit 05dd5fa

File tree

3 files changed

+61
-92
lines changed

3 files changed

+61
-92
lines changed

CHANGELOG.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# 20.2.12
66

77
- 修复更新时头像闪动的问题
8+
- 优化更新时安装包下载进度展示
89

910
# 20.2.11
1011

lib/component/setting.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Setting extends StatelessWidget {
2222
// 标题部分,字体加重
2323
Text(
2424
title,
25-
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
25+
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
2626
),
2727
// 副标题部分,较小的字体
2828
Text(
2929
subtitle,
30-
style: TextStyle(fontSize: 14, color: Colors.grey),
30+
style: const TextStyle(fontSize: 14, color: Colors.grey),
3131
),
3232
],
3333
),

lib/user/user.dart

Lines changed: 58 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,20 @@ class UserPage extends StatefulWidget {
3030
}
3131

3232
class _UserPageState extends State<UserPage> {
33-
late String _appVersion;
34-
late SettingConfig config = SettingConfig();
33+
String? _appVersion;
34+
SettingConfig config = SettingConfig();
3535
User? _me;
36-
late String _baseUrl;
36+
String _baseUrl = "";
3737
final FocusNode _proxyUrlFocusNode = FocusNode();
3838
final TextEditingController _proxyUrlController = TextEditingController();
3939
String _filePath = "";
40-
double _updateDownloadProgress = 0;
40+
final ValueNotifier<double> _updateDownloadProgress = ValueNotifier(0);
4141

4242
Future<void> _fetchAppVersion() async {
4343
PackageInfo packageInfo = await PackageInfo.fromPlatform();
4444
_appVersion = packageInfo.version;
4545
}
4646

47-
Future<String> _getAppVersion() async {
48-
await _fetchAppVersion();
49-
return _appVersion;
50-
}
51-
5247
Future<void> _loadSettingConfig() async {
5348
config = await SharedPrefsUtils.getSettingConfig();
5449
if (config.proxyUrl != "") {
@@ -75,6 +70,7 @@ class _UserPageState extends State<UserPage> {
7570
_fetchAppVersion();
7671
_loadSettingConfig();
7772
_fetchMe();
73+
_fetchAppVersion();
7874
}
7975

8076
Future<void> _fetchMe() async {
@@ -83,10 +79,10 @@ class _UserPageState extends State<UserPage> {
8379
setState(() {});
8480
}
8581

86-
Future<String> _fetchBaseUrl() async {
82+
Future<void> _fetchBaseUrl() async {
8783
AuthParams authParams = await AuthApi().getAuthParams();
8884
_baseUrl = authParams.baseUrl;
89-
return _baseUrl;
85+
setState(() {});
9086
}
9187

9288
String getAvatarTitle(User? user) {
@@ -98,13 +94,13 @@ class _UserPageState extends State<UserPage> {
9894
}
9995

10096
Widget _buildAppbarUser() {
101-
if (_me == null) return const CircularProgressIndicator();
97+
if (_me == null || _baseUrl == "") return const CircularProgressIndicator();
10298
return Row(
10399
children: [
104100
CircleAvatar(
105101
radius: 28, // 半径控制头像的大小
106-
backgroundImage: NetworkImage(
107-
UrlUtils.getCoverUrl(_baseUrl, _me?.avatar ?? "")),
102+
backgroundImage:
103+
NetworkImage(UrlUtils.getCoverUrl(_baseUrl, _me?.avatar ?? "")),
108104
),
109105
const SizedBox(
110106
width: 10,
@@ -131,7 +127,8 @@ class _UserPageState extends State<UserPage> {
131127
],
132128
),
133129
],
134-
);;
130+
);
131+
;
135132
}
136133

137134
@override
@@ -140,10 +137,6 @@ class _UserPageState extends State<UserPage> {
140137
appBar: AppBar(
141138
backgroundColor: Colors.white,
142139
title: _buildAppbarUser(),
143-
// title: const CircleAvatar(
144-
// radius: 32.5, // 半径控制头像的大小
145-
// backgroundImage: NetworkImage('https://ikaros.run/img/favicon.ico'),
146-
// ),
147140
actionsIconTheme: const IconThemeData(
148141
color: Colors.black,
149142
size: 35,
@@ -168,79 +161,59 @@ class _UserPageState extends State<UserPage> {
168161
),
169162
],
170163
),
171-
// appBar: PreferredSize(
172-
// preferredSize: const DartUi.Size.fromHeight(70.0),
173-
// child: AppBar(
174-
// backgroundColor: Colors.white,
175-
// title: const CircleAvatar(
176-
// radius: 32.5, // 半径控制头像的大小
177-
// backgroundImage: NetworkImage('https://ikaros.run/img/favicon.ico'),
178-
// ),
179-
// actionsIconTheme: const IconThemeData(
180-
// color: Colors.black,
181-
// size: 35,
182-
// ),
183-
// actions: <Widget>[
184-
// PopupMenuButton<String>(
185-
// itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
186-
// _selectView(Icons.update, "更新", "app_update"),
187-
// _selectView(Icons.exit_to_app_rounded, '退出', 'user_logout'),
188-
// ],
189-
// onSelected: (String action) {
190-
// // 点击选项的时候
191-
// switch (action) {
192-
// case 'user_logout':
193-
// _userLogout();
194-
// break;
195-
// case 'app_update':
196-
// _checkAppUpdate();
197-
// break;
198-
// }
199-
// },
200-
// ),
201-
// ],
202-
// ),
203-
// ),
204164
body: ListView(
205165
children: [
206166
Setting(
207167
title: "版本号",
208168
subtitle: "这是APP当前的版本号",
209-
rightWidget: FutureBuilder(
210-
future: _getAppVersion(),
211-
builder: (context, snapshot) {
212-
if (snapshot.connectionState == ConnectionState.done) {
213-
if (snapshot.hasError) {
214-
return Text(
215-
"Load app version error: ${snapshot.error ?? ""}");
216-
} else {
217-
if (_updateDownloadProgress > 0 &&
218-
_updateDownloadProgress < 100) {
219-
return Center(
169+
rightWidget: ValueListenableBuilder<double>(
170+
valueListenable: _updateDownloadProgress,
171+
builder: (context, progress, child) {
172+
if (_updateDownloadProgress.value > 0 &&
173+
_updateDownloadProgress.value < 100) {
174+
return Stack(
175+
alignment: Alignment.center,
176+
children: [
177+
Center(
220178
child: CircularProgressIndicator(
221-
value: _updateDownloadProgress / 100, // 设置进度
222-
strokeWidth: 6.0, // 设置圆圈的宽度
179+
value: _updateDownloadProgress.value / 100, // 设置进度
180+
strokeWidth: 12.0, // 设置圆圈的宽度
223181
valueColor: const AlwaysStoppedAnimation<Color>(
224-
Colors.pink), // 设置颜色
182+
Colors.blueAccent), // 设置颜色
225183
),
226-
);
227-
} else if (_updateDownloadProgress == 100) {
228-
return OutlinedButton(
229-
onPressed: () {
230-
_installPackage();
231-
},
232-
child: const Text("点击安装"));
233-
}
234-
return Text(
235-
"v${snapshot.data ?? "0.0.0"}",
236-
style: const TextStyle(fontSize: 20),
237-
);
238-
}
239-
} else {
240-
return const Center(
241-
child: CircularProgressIndicator(),
184+
),
185+
Text(
186+
'${_updateDownloadProgress.value.toInt()}%', // 显示百分比
187+
style: const TextStyle(
188+
fontSize: 16.0, fontWeight: FontWeight.bold),
189+
),
190+
],
191+
);
192+
return Center(
193+
child: CircularProgressIndicator(
194+
value: _updateDownloadProgress.value / 100,
195+
// 设置进度
196+
strokeWidth: 12.0,
197+
// 设置圆圈的宽度
198+
semanticsLabel: '安装包下载中',
199+
semanticsValue: '已完成 ${_updateDownloadProgress.value}%',
200+
valueColor: const AlwaysStoppedAnimation<Color>(
201+
Colors.blueAccent), // 设置颜色
202+
),
242203
);
204+
} else if (_updateDownloadProgress.value == 100) {
205+
return OutlinedButton(
206+
onPressed: () {
207+
_installPackage();
208+
},
209+
child: const Text("点击安装"));
243210
}
211+
return _appVersion != null
212+
? Text(
213+
"v$_appVersion",
214+
style: const TextStyle(fontSize: 20),
215+
)
216+
: const CircularProgressIndicator();
244217
}),
245218
),
246219
Setting(
@@ -443,14 +416,11 @@ class _UserPageState extends State<UserPage> {
443416
if (!tmpUpdateFile.existsSync()) {
444417
_configProxy(Dio()).download(downloadUrl, _filePath,
445418
onReceiveProgress: (received, total) {
446-
double progress = (received / total) * 100;
447-
setState(() {
448-
_updateDownloadProgress = progress;
449-
});
419+
_updateDownloadProgress.value = (received / total) * 100;
450420
});
451421
} else {
452422
setState(() {
453-
_updateDownloadProgress = 100;
423+
_updateDownloadProgress.value = 100;
454424
});
455425
}
456426
}
@@ -471,9 +441,7 @@ class _UserPageState extends State<UserPage> {
471441
if (Platform.isWindows) {
472442
_startUpdateProcess(_filePath);
473443
}
474-
setState(() {
475-
_updateDownloadProgress = 0;
476-
});
444+
_updateDownloadProgress.value = 0;
477445
}
478446

479447
void _startUpdateProcess(String zipPath) async {

0 commit comments

Comments
 (0)