feat: 添加 App 检查更新功能
- 前端:设置页面添加检查更新菜单项,从 pubspec.yaml 动态获取版本号 - 后端:新增 /api/v1/app/check-updates 接口,自动扫描 releases 目录对比版本 - 配置:新增 AppVersionSettings,支持通过环境变量配置版本和下载链接 - Docker:添加 releases 目录挂载
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
|
||||
class AppVersionResponse {
|
||||
final bool hasUpdate;
|
||||
final String latestVersion;
|
||||
final int latestBuild;
|
||||
final String minRequiredVersion;
|
||||
final String updateType;
|
||||
final String? downloadUrl;
|
||||
final String? releaseNotes;
|
||||
|
||||
AppVersionResponse({
|
||||
required this.hasUpdate,
|
||||
required this.latestVersion,
|
||||
required this.latestBuild,
|
||||
required this.minRequiredVersion,
|
||||
required this.updateType,
|
||||
this.downloadUrl,
|
||||
this.releaseNotes,
|
||||
});
|
||||
|
||||
factory AppVersionResponse.fromJson(Map<String, dynamic> json) {
|
||||
return AppVersionResponse(
|
||||
hasUpdate: json['has_update'] as bool,
|
||||
latestVersion: json['latest_version'] as String,
|
||||
latestBuild: json['latest_build'] as int,
|
||||
minRequiredVersion: json['min_required_version'] as String,
|
||||
updateType: json['update_type'] as String,
|
||||
downloadUrl: json['download_url'] as String?,
|
||||
releaseNotes: json['release_notes'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsApi {
|
||||
final IApiClient _client;
|
||||
static const _prefix = '/api/v1/app';
|
||||
|
||||
SettingsApi(this._client);
|
||||
|
||||
Future<AppVersionResponse> checkUpdates({
|
||||
required int currentBuild,
|
||||
String? currentVersion,
|
||||
String platform = 'android',
|
||||
}) async {
|
||||
final params = <String, String>{
|
||||
'platform': platform,
|
||||
'current_build': currentBuild.toString(),
|
||||
};
|
||||
if (currentVersion != null) {
|
||||
params['current_version'] = currentVersion;
|
||||
}
|
||||
final queryString = params.entries
|
||||
.map((e) => '${e.key}=${e.value}')
|
||||
.join('&');
|
||||
final response = await _client.get('$_prefix/check-updates?$queryString');
|
||||
return AppVersionResponse.fromJson(response.data);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../../../core/di/injection.dart';
|
||||
import '../../../../core/theme/design_tokens.dart';
|
||||
import '../../../../shared/widgets/page_header.dart' as widgets;
|
||||
import '../../../friends/data/friends_api.dart';
|
||||
import '../../../users/data/models/user_response.dart';
|
||||
import '../../../users/data/users_api.dart';
|
||||
import 'package:social_app/core/constants/app_constants.dart';
|
||||
import 'package:social_app/core/di/injection.dart';
|
||||
import 'package:social_app/core/theme/design_tokens.dart';
|
||||
import 'package:social_app/shared/widgets/app_loading_indicator.dart';
|
||||
import 'package:social_app/shared/widgets/page_header.dart' as widgets;
|
||||
import 'package:social_app/shared/widgets/toast/toast.dart';
|
||||
import 'package:social_app/shared/widgets/toast/toast_type.dart';
|
||||
import 'package:social_app/features/friends/data/friends_api.dart';
|
||||
import 'package:social_app/features/settings/data/settings_api.dart';
|
||||
import 'package:social_app/features/users/data/models/user_response.dart';
|
||||
import 'package:social_app/features/users/data/users_api.dart';
|
||||
|
||||
class SettingsScreen extends StatefulWidget {
|
||||
const SettingsScreen({super.key});
|
||||
@@ -68,7 +73,12 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
const widgets.PageHeader(leading: widgets.BackButton()),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.fromLTRB(20, 8, 20, 20),
|
||||
padding: const EdgeInsets.fromLTRB(
|
||||
AppSpacing.xl,
|
||||
AppSpacing.sm,
|
||||
AppSpacing.xl,
|
||||
AppSpacing.xl,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -93,13 +103,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
if (_isLoading) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
height: 100,
|
||||
padding: const EdgeInsets.all(20),
|
||||
height: 120,
|
||||
padding: const EdgeInsets.all(AppSpacing.xl),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
),
|
||||
child: const Center(child: CircularProgressIndicator()),
|
||||
child: const Center(child: AppLoadingIndicator(size: 22)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -108,82 +118,101 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(20),
|
||||
padding: const EdgeInsets.all(AppSpacing.xl),
|
||||
decoration: BoxDecoration(
|
||||
gradient: const LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
stops: [0, 1],
|
||||
colors: [Color(0xFFFFFFFF), AppColors.surfaceInfoLight],
|
||||
transform: GradientRotation(35 * 3.14159 / 180),
|
||||
colors: [AppColors.white, Color(0xF8F9FCFF)],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
border: Border.all(color: const Color(0xFFE5ECF8)),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
border: Border.all(color: AppColors.borderSecondary),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Color(0x1A0F172A),
|
||||
blurRadius: 14,
|
||||
offset: Offset(0, 4),
|
||||
color: Color(0x05000000),
|
||||
blurRadius: 12,
|
||||
offset: Offset(0, 3),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 72,
|
||||
height: 72,
|
||||
width: 64,
|
||||
height: 64,
|
||||
decoration: BoxDecoration(
|
||||
gradient: const LinearGradient(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [Color(0xFFEAF1FF), Color(0xFFF8FBFF)],
|
||||
colors: [AppColors.blue100, AppColors.blue50],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(36),
|
||||
border: Border.all(color: const Color(0xFFD9E5FA)),
|
||||
borderRadius: BorderRadius.circular(32),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Color.fromRGBO(
|
||||
AppColors.blue400.r.toInt(),
|
||||
AppColors.blue400.g.toInt(),
|
||||
AppColors.blue400.b.toInt(),
|
||||
0.2,
|
||||
),
|
||||
blurRadius: 12,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Icon(Icons.person, size: 30, color: AppColors.blue500),
|
||||
child: const Icon(Icons.person, size: 28, color: AppColors.blue600),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
const SizedBox(width: AppSpacing.lg),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
username,
|
||||
style: const TextStyle(
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
Expanded(
|
||||
child: Text(
|
||||
username,
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 10,
|
||||
vertical: 4,
|
||||
vertical: 5,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceTertiary,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: const Color(0xFFDEE7F6)),
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
AppColors.blue50,
|
||||
AppColors.surfaceInfoLight,
|
||||
],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: AppColors.borderQuaternary),
|
||||
),
|
||||
child: const Text(
|
||||
'Free',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate500,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.blue600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
email,
|
||||
style: const TextStyle(
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate500,
|
||||
@@ -208,49 +237,34 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
}
|
||||
|
||||
Widget _buildQuickActions(BuildContext context) {
|
||||
return Container(
|
||||
height: 120,
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
borderRadius: BorderRadius.circular(18),
|
||||
border: Border.all(color: const Color(0xFFE7EDF6)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildQuickActionCard(
|
||||
icon: Icons.people,
|
||||
iconColor: AppColors.blue600,
|
||||
iconBg: AppColors.surfaceTertiary,
|
||||
iconBorder: const Color(0xFFE6ECF7),
|
||||
title: '联系人',
|
||||
subtitle: _buildFriendsSubtitle(),
|
||||
onTap: () => context.push('/contacts'),
|
||||
),
|
||||
return Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildActionCard(
|
||||
icon: Icons.people,
|
||||
iconColor: AppColors.blue500,
|
||||
title: '联系人',
|
||||
subtitle: _buildFriendsSubtitle(),
|
||||
onTap: () => context.push('/contacts'),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: _buildQuickActionCard(
|
||||
icon: Icons.auto_awesome,
|
||||
iconColor: const Color(0xFF0EA5A4),
|
||||
iconBg: const Color(0xFFF7FAFF),
|
||||
iconBorder: const Color(0xFFE6ECF7),
|
||||
title: '常用功能',
|
||||
subtitle: '已启用:会议提醒',
|
||||
onTap: () => context.push('/settings/features'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.md),
|
||||
Expanded(
|
||||
child: _buildActionCard(
|
||||
icon: Icons.auto_awesome,
|
||||
iconColor: const Color(0xFF8B5CF6),
|
||||
title: '常用功能',
|
||||
subtitle: '已启用:会议提醒',
|
||||
onTap: () => context.push('/settings/features'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildQuickActionCard({
|
||||
Widget _buildActionCard({
|
||||
required IconData icon,
|
||||
required Color iconColor,
|
||||
required Color iconBg,
|
||||
required Color iconBorder,
|
||||
required String title,
|
||||
required String subtitle,
|
||||
required VoidCallback onTap,
|
||||
@@ -258,47 +272,56 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: iconBg,
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
border: Border.all(color: iconBorder),
|
||||
color: AppColors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all(color: AppColors.borderSecondary),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Color(0x04000000),
|
||||
blurRadius: 6,
|
||||
offset: Offset(0, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(icon, size: 18, color: iconColor),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF1E293B),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Icon(
|
||||
Icons.chevron_right,
|
||||
size: 16,
|
||||
color: AppColors.slate400,
|
||||
Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceTertiary,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Icon(icon, size: 18, color: iconColor),
|
||||
),
|
||||
const Spacer(),
|
||||
Icon(Icons.chevron_right, size: 16, color: AppColors.slate300),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
subtitle,
|
||||
style: const TextStyle(
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -308,65 +331,95 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
|
||||
Widget _buildSubscriptionCard() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(14),
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: const Color(0xFFE3EAF6)),
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [AppColors.white, const Color(0xFFFAFBFF)],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all(color: AppColors.borderSecondary),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Color(0x03000000),
|
||||
blurRadius: 6,
|
||||
offset: Offset(0, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [AppColors.blue100, AppColors.blue50],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: AppColors.blue200.withValues(alpha: 0.45),
|
||||
blurRadius: 6,
|
||||
offset: const Offset(0, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.workspace_premium,
|
||||
size: 22,
|
||||
color: AppColors.blue600,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.md),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'套餐等级 Free',
|
||||
'升级到 Pro',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'∞ / ∞',
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
'解锁更多高级功能',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(999),
|
||||
child: LinearProgressIndicator(
|
||||
value: 0,
|
||||
backgroundColor: const Color(0xFFE8EEF8),
|
||||
valueColor: const AlwaysStoppedAnimation(AppColors.blue400),
|
||||
minHeight: 8,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Container(
|
||||
width: 72,
|
||||
height: 32,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFE2E8F0),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: const Color(0xFFCBD5E1)),
|
||||
),
|
||||
child: const Center(
|
||||
child: Text(
|
||||
'升级',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF94A3B8),
|
||||
gradient: const LinearGradient(
|
||||
colors: [AppColors.blue500, AppColors.blue600],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: const Color(0x4D60A5FA),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Text(
|
||||
'升级',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -401,6 +454,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
title: '我的账户',
|
||||
onTap: () => context.push('/settings/account'),
|
||||
),
|
||||
_buildDivider(),
|
||||
_buildMenuItem(
|
||||
icon: Icons.system_update,
|
||||
title: '检查更新',
|
||||
trailing: 'v${AppConstants.version}',
|
||||
onTap: () => _checkForUpdates(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -464,7 +524,59 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
return Container(
|
||||
height: 1,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 14),
|
||||
color: const Color(0xFFEEF2F7),
|
||||
color: AppColors.slate100,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _checkForUpdates(BuildContext context) async {
|
||||
try {
|
||||
final settingsApi = sl<SettingsApi>();
|
||||
final result = await settingsApi.checkUpdates(
|
||||
currentBuild: AppConstants.build,
|
||||
currentVersion: AppConstants.version,
|
||||
platform: 'android',
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
if (!result.hasUpdate) {
|
||||
Toast.show(context, '当前已是最新版本', type: ToastType.success);
|
||||
return;
|
||||
}
|
||||
|
||||
final message = result.updateType == 'required'
|
||||
? '有新版本可用 (${result.latestVersion}),请立即更新'
|
||||
: '发现新版本 (${result.latestVersion}),是否更新?';
|
||||
|
||||
final shouldUpdate = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('检查更新'),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
if (result.downloadUrl != null)
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('更新'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (shouldUpdate == true && result.downloadUrl != null && mounted) {
|
||||
Toast.show(
|
||||
context,
|
||||
'下载链接: ${result.downloadUrl}',
|
||||
type: ToastType.info,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
Toast.show(context, '检查更新失败', type: ToastType.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user