905086
2026-11-04 9593973fb0ff112cf26266bfedbf45e6460b10bf
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import 'dart:io';
 
import 'package:flutter/material.dart';
import 'package:fluttergamebox/api/api_client.dart';
import 'package:fluttergamebox/common/theme.dart';
import 'package:fluttergamebox/store/app_state.dart';
import 'package:fluttergamebox/utils/util.dart';
import 'package:path/path.dart' as p;
 
/// 下载管理弹窗(安装任务)
void showDownloadDialog(BuildContext context) {
  showDialog(
    context: context,
    barrierColor: Colors.black.withOpacity(0.6),
    builder: (context) => const _DownloadDialog(),
  );
}
 
class _DownloadDialog extends StatelessWidget {
  const _DownloadDialog();
  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: DownloadManager.instance,
      builder: (context, _) {
        final dm = DownloadManager.instance;
        final task = dm.activeTask;
        if (task == null) {
          return Dialog(
            backgroundColor: const Color(0xFF1E2130),
            child: Container(
              width: 560,
              padding: const EdgeInsets.all(32),
              child: const Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(Icons.download_done, color: AppColors.textGray, size: 48),
                  SizedBox(height: 16),
                  Text('暂无下载任务',
                      style: TextStyle(color: AppColors.textGray)),
                ],
              ),
            ),
          );
        }
        return Dialog(
          backgroundColor: const Color(0xFF1E2130),
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
          child: Container(
            width: 680,
            padding: const EdgeInsets.all(24),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题
                Row(
                  children: [
                    if (task.finished)
                      const Icon(Icons.check_circle,
                          color: AppColors.green, size: 20)
                    else
                      const Icon(Icons.download,
                          color: AppColors.accent, size: 20),
                    const SizedBox(width: 8),
                    const Text('安装任务',
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 15,
                            fontWeight: FontWeight.bold)),
                    const SizedBox(width: 10),
                    const Expanded(
                      child: Text('如长时间无速度,请取消安装保留文件尝试再次安装',
                          style:
                              TextStyle(color: AppColors.red, fontSize: 11)),
                    ),
                    const SizedBox(width: 10),
                    // 自动关机
                    _AutoShutdownToggle(task: task),
                    const SizedBox(width: 12),
                    GestureDetector(
                      onTap: () => Navigator.pop(context),
                      child: const Icon(Icons.close,
                          color: AppColors.textGray, size: 18),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                // 游戏名
                Text(task.title,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(
                        color: Colors.white,
                        fontSize: 14,
                        fontWeight: FontWeight.w500)),
                const SizedBox(height: 18),
                // 进度区
                Row(
                  children: [
                    // 环形进度
                    SizedBox(
                      width: 86,
                      height: 86,
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          SizedBox(
                            width: 86,
                            height: 86,
                            child: CircularProgressIndicator(
                              value: task.finished ? 1 : task.progress,
                              color: task.paused
                                  ? AppColors.yellow
                                  : AppColors.accent,
                              backgroundColor: AppColors.bgLighter,
                              strokeWidth: 5,
                            ),
                          ),
                          Text(
                            '${(task.progress * 100).toStringAsFixed(task.progress < 0.1 ? 2 : 0)}%',
                            style: const TextStyle(
                                color: Colors.white,
                                fontSize: 15,
                                fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),
                    ),
                    const SizedBox(width: 22),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text(
                                task.extracting
                                    ? '正在解压安装...'
                                    : task.extractFailed
                                        ? '解压失败'
                                        : task.finished
                                            ? '下载完成'
                                            : task.paused
                                                ? '已暂停'
                                                : '正在下载',
                                style: TextStyle(
                                    color: task.extractFailed
                                        ? AppColors.red
                                        : task.paused
                                            ? AppColors.yellow
                                            : task.finished
                                                ? AppColors.green
                                                : AppColors.accent,
                                    fontSize: 14,
                                    fontWeight: FontWeight.w500),
                              ),
                            ],
                          ),
                          const SizedBox(height: 8),
                          Text(
                            task.extracting
                                ? '解压中,请勿关闭软件'
                                : task.extractFailed
                                    ? '压缩包保留在下载目录,可手动解压'
                                    : '${formatBytes(task.speed)}/s',
                            style: const TextStyle(
                                color: AppColors.textGray, fontSize: 13),
                          ),
                          const SizedBox(height: 4),
                          Text(
                            '${formatBytes(task.receivedBytes)} / ${formatBytes(task.totalBytes)}',
                            style: const TextStyle(
                                color: AppColors.textGray, fontSize: 13),
                          ),
                          const SizedBox(height: 4),
                          Text(
                            task.finished
                                ? '已完成'
                                : '剩余时间 ${formatDuration(task.etaSeconds)}',
                            style: const TextStyle(
                                color: AppColors.textGray, fontSize: 13),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                // 网络信息条
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                  decoration: BoxDecoration(
                    color: AppColors.bg,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: Row(
                    children: [
                      const Text('网络运营商:联通',
                          style: TextStyle(
                              color: AppColors.textGray, fontSize: 12)),
                      const Spacer(),
                      const Text('点击切换线路:自动',
                          style: TextStyle(
                              color: AppColors.textGray, fontSize: 12)),
                    ],
                  ),
                ),
                const SizedBox(height: 10),
                // 文件列表
                Container(
                  constraints: const BoxConstraints(maxHeight: 180),
                  decoration: BoxDecoration(
                    color: AppColors.bg,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: task.files.length,
                    itemBuilder: (context, i) {
                      final f = task.files[i];
                      Color stColor = AppColors.textDim;
                      String stLabel = '等待下载';
                      switch (f.status) {
                        case DownloadStatus.preparing:
                          stColor = AppColors.yellow;
                          stLabel = '正在准备';
                        case DownloadStatus.downloading:
                          stColor = AppColors.green;
                          stLabel = '正在下载';
                        case DownloadStatus.paused:
                          stColor = AppColors.yellow;
                          stLabel = '已暂停';
                        case DownloadStatus.done:
                          stColor = AppColors.green;
                          stLabel = '已完成';
                        case DownloadStatus.failed:
                          stColor = AppColors.red;
                          stLabel = '下载失败';
                        default:
                          stLabel = '等待下载';
                      }
                      return Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 12, vertical: 6),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 200,
                              child: Text(f.name,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                  style: const TextStyle(
                                      color: AppColors.textWhite,
                                      fontSize: 12)),
                            ),
                            Expanded(
                              child: LinearProgressIndicator(
                                value: f.progress,
                                backgroundColor: AppColors.bgLighter,
                                color: AppColors.accent,
                                minHeight: 3,
                              ),
                            ),
                            const SizedBox(width: 10),
                            SizedBox(
                              width: 130,
                              child: Text(
                                  '${formatBytes(f.received)}/${formatBytes(f.totalBytes)}',
                                  style: const TextStyle(
                                      color: AppColors.textGray,
                                      fontSize: 11)),
                            ),
                            SizedBox(
                              width: 60,
                              child: Text(stLabel,
                                  style: TextStyle(
                                      color: stColor, fontSize: 11)),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ),
                const SizedBox(height: 18),
                // 操作按钮
                Row(
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        onPressed: task.finished || task.extracting
                            ? null
                            : () {
                                if (task.paused) {
                                  DownloadManager.instance.resume();
                                } else {
                                  DownloadManager.instance.pause();
                                }
                              },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: task.paused
                              ? AppColors.accent
                              : const Color(0xFF3A3F52),
                          foregroundColor: Colors.white,
                          disabledBackgroundColor:
                              AppColors.green.withOpacity(0.3),
                          padding: const EdgeInsets.symmetric(vertical: 12),
                        ),
                        child: Text(task.finished
                            ? '下载完成'
                            : task.paused
                                ? '继续下载'
                                : '暂停下载'),
                      ),
                    ),
                    const SizedBox(width: 12),
                    Expanded(
                      child: ElevatedButton(
                        onPressed: () {
                          DownloadManager.instance.cancelTask();
                          Navigator.pop(context);
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: const Color(0xFF3A3F52),
                          foregroundColor: Colors.white,
                          padding: const EdgeInsets.symmetric(vertical: 12),
                        ),
                        child: const Text('取消安装'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
 
class _AutoShutdownToggle extends StatefulWidget {
  final DownloadTask task;
  const _AutoShutdownToggle({required this.task});
  @override
  State<_AutoShutdownToggle> createState() => _AutoShutdownToggleState();
}
 
class _AutoShutdownToggleState extends State<_AutoShutdownToggle> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => setState(
          () => widget.task.autoShutdown = !widget.task.autoShutdown),
      child: Row(
        children: [
          Text('自动关机',
              style: const TextStyle(
                  color: AppColors.textGray, fontSize: 11)),
          const SizedBox(width: 6),
          Container(
            width: 28,
            height: 15,
            decoration: BoxDecoration(
              color: widget.task.autoShutdown
                  ? AppColors.accent
                  : AppColors.bgLighter,
              borderRadius: BorderRadius.circular(8),
            ),
            child: AnimatedAlign(
              duration: const Duration(milliseconds: 150),
              alignment: widget.task.autoShutdown
                  ? Alignment.centerRight
                  : Alignment.centerLeft,
              child: Container(
                width: 11,
                height: 11,
                margin: const EdgeInsets.all(2),
                decoration:
                    const BoxDecoration(color: Colors.white, shape: BoxShape.circle),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
 
/// 打开下载/安装目录
Future<void> openDirectory(String path) async {
  final dir = Directory(path);
  if (!await dir.exists()) {
    await dir.create(recursive: true);
  }
  Process.run('explorer.exe', [p.normalize(dir.path)]);
}