import 'package:app/widgets/base/button/index.dart'; import 'package:app/widgets/base/tag/index.dart'; import 'package:app_installer/app_installer.dart'; import 'package:flutter/material.dart'; import '../../utils/transfer/download.dart'; import '../base/config/config.dart'; ///下载状态枚举 enum UploadState { notStarted, //未开始下载 downloading, //下载中 completed, //下载完毕 } class AppUpdateUi extends StatefulWidget { final String version; final List updateNotice; final String uploadUrl; //下载地址 const AppUpdateUi({ super.key, required this.version, required this.updateNotice, required this.uploadUrl, }); @override State createState() => _UpdateUiState(); } class _UpdateUiState extends State { int _uploadProgress = 0; //下载进度 UploadState _uploadState = UploadState.notStarted; @override void initState() { super.initState(); getLocalApk(); } ///读取本地是否有下载记录 void getLocalApk() async { String url = await LocalDownload.getFilePath(url: widget.uploadUrl, path: '/apk'); if (url.isNotEmpty) { setState(() { _uploadState = UploadState.completed; }); } } ///下载apk void _handUploadApk() async { if (_uploadState == UploadState.notStarted) { setState(() { _uploadState = UploadState.downloading; }); LocalDownload.downLoadFile( url: widget.uploadUrl, path: "/apk", onProgress: (double double) { setState(() { _uploadProgress = double.toInt(); }); }, onDone: (apk) async { setState(() { _uploadState = UploadState.completed; }); AppInstaller.installApk(apk); }, ); } else if (_uploadState == UploadState.completed) { String url = await LocalDownload.getFilePath(url: widget.uploadUrl, path: '/apk'); AppInstaller.installApk(url); } } @override Widget build(BuildContext context) { String text; if (_uploadState == UploadState.downloading) { text = "$_uploadProgress%"; } else if (_uploadState == UploadState.completed) { text = '安装'; } else { text = '立即升级'; } return IntrinsicHeight( child: Container( color: Colors.transparent, padding: EdgeInsets.symmetric(horizontal: 40), alignment: Alignment.center, child: ConstrainedBox( constraints: BoxConstraints( maxWidth: 500, ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Stack( fit: StackFit.passthrough, children: [ Image.asset("assets/image/version_bg.png"), Positioned( top: 0, left: 0, right: 0, bottom: 0, child: DefaultTextStyle( style: TextStyle( fontSize: 16, color: Colors.white, fontWeight: FontWeight.w700, ), child: FractionalTranslation( translation: Offset(0.35, 0.3), child: Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("发现新版本"), SizedBox(width: 10), Tag( text: "V ${widget.version}", type: ThemeType.warning, ), ], ), ), ), ), ], ), Material( borderRadius: BorderRadius.only( bottomLeft: Radius.circular(20), bottomRight: Radius.circular(20), ), child: Container( padding: EdgeInsets.all(15), width: double.infinity, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ ...widget.updateNotice.asMap().entries.map((entry) { final index = entry.key; final item = entry.value; return Padding( padding: const EdgeInsets.symmetric(vertical: 5), child: Text("${index + 1}.$item"), ); }), Container( margin: EdgeInsets.only(top: 20), height: 40, child: Button( text: text, onPressed: _uploadState == UploadState.downloading ? null : _handUploadApk, ), ), ], ), ), ), ], ), ), ), ); } }