初始化
This commit is contained in:
83
lib/widgets/base/button/index.dart
Normal file
83
lib/widgets/base/button/index.dart
Normal file
@@ -0,0 +1,83 @@
|
||||
import 'package:app/core/theme/theme.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../config/config.dart';
|
||||
|
||||
class Button extends StatelessWidget {
|
||||
final double? width;
|
||||
final double? height;
|
||||
final Widget child;
|
||||
final ThemeType type;
|
||||
final BorderRadius radius;
|
||||
final VoidCallback? onPressed;
|
||||
final bool loading;
|
||||
final bool disabled;
|
||||
final Widget? icon;
|
||||
|
||||
const Button({
|
||||
super.key,
|
||||
this.icon,
|
||||
this.width,
|
||||
this.height,
|
||||
this.radius = const BorderRadius.all(Radius.circular(80)),
|
||||
required this.child,
|
||||
this.onPressed,
|
||||
this.type = ThemeType.primary,
|
||||
this.loading = false,
|
||||
this.disabled = false,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final bgDecoration = switch (type) {
|
||||
ThemeType.primary => BoxDecoration(color: Theme.of(context).primaryColor),
|
||||
ThemeType.success => BoxDecoration(color: context.success),
|
||||
ThemeType.danger => BoxDecoration(color: context.danger),
|
||||
ThemeType.warning => BoxDecoration(color: context.warning),
|
||||
ThemeType.info => BoxDecoration(color: context.info),
|
||||
};
|
||||
|
||||
return Opacity(
|
||||
opacity: disabled || loading ? 0.5 : 1,
|
||||
child: Container(
|
||||
width: width,
|
||||
height: height,
|
||||
decoration: bgDecoration.copyWith(borderRadius: radius),
|
||||
child: Material(
|
||||
type: MaterialType.transparency, // 让波纹依附在上层容器
|
||||
child: InkWell(
|
||||
borderRadius: radius,
|
||||
onTap: disabled || loading ? null : onPressed,
|
||||
splashColor: Colors.white.withValues(alpha: 0.2),
|
||||
highlightColor: Colors.white.withValues(alpha: 0.2),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 7),
|
||||
child: DefaultTextStyle(
|
||||
style: Theme.of(context).textTheme.bodySmall!.copyWith(
|
||||
color: Colors.white
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
child: Row(
|
||||
spacing: 10,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
if (loading)
|
||||
const SizedBox(
|
||||
width: 15,
|
||||
height: 15,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
valueColor: AlwaysStoppedAnimation(Colors.white),
|
||||
),
|
||||
),
|
||||
child,
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
21
lib/widgets/base/config/color.dart
Normal file
21
lib/widgets/base/config/color.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'config.dart';
|
||||
|
||||
class ColorResolver {
|
||||
final Color bg;
|
||||
final Color font;
|
||||
final Color border;
|
||||
|
||||
ColorResolver(this.bg, this.font, this.border);
|
||||
}
|
||||
|
||||
///返回颜色
|
||||
ColorResolver resolveEffectColors(Color color, Effect effect) => switch (effect) {
|
||||
Effect.dark => ColorResolver(color, Colors.white, color),
|
||||
Effect.light => () {
|
||||
final pale = color.withValues(alpha: 0.2);
|
||||
return ColorResolver(pale, color, pale);
|
||||
}(), // 注意这里多了 (),表示立即调用
|
||||
Effect.plain => ColorResolver(Colors.white, color, color),
|
||||
};
|
||||
15
lib/widgets/base/config/config.dart
Normal file
15
lib/widgets/base/config/config.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
///主题风格
|
||||
enum Effect {
|
||||
dark,
|
||||
light,
|
||||
plain,
|
||||
}
|
||||
|
||||
///主题类型
|
||||
enum ThemeType {
|
||||
primary,
|
||||
success,
|
||||
warning,
|
||||
danger,
|
||||
info,
|
||||
}
|
||||
53
lib/widgets/base/empty/index.dart
Normal file
53
lib/widgets/base/empty/index.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
enum EmptyType {
|
||||
data,
|
||||
}
|
||||
|
||||
class Empty extends StatelessWidget {
|
||||
final EmptyType type;
|
||||
final String? text;
|
||||
final Widget? child;
|
||||
|
||||
const Empty({
|
||||
super.key,
|
||||
this.type = EmptyType.data,
|
||||
this.text,
|
||||
this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var emptyImg = switch (type) {
|
||||
EmptyType.data => Image.asset('assets/image/empty_data.png'),
|
||||
};
|
||||
var emptyText = switch (type) {
|
||||
EmptyType.data => '暂无数据',
|
||||
};
|
||||
return Container(
|
||||
padding: EdgeInsets.all(0),
|
||||
child: Column(
|
||||
children: [
|
||||
FractionallySizedBox(
|
||||
widthFactor: 0.5,
|
||||
child: Container(
|
||||
margin: EdgeInsets.only(bottom: 15),
|
||||
child: emptyImg,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
text ?? emptyText,
|
||||
style: Theme.of(context).textTheme.labelLarge,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
child != null
|
||||
? Container(
|
||||
margin: EdgeInsets.only(top: 15),
|
||||
child: child,
|
||||
)
|
||||
: SizedBox(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
58
lib/widgets/base/tag/index.dart
Normal file
58
lib/widgets/base/tag/index.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'package:app/core/theme/base/app_theme_ext.dart';
|
||||
import 'package:app/widgets/base/config/color.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../config/config.dart';
|
||||
|
||||
class Tag extends StatelessWidget {
|
||||
final String text;
|
||||
final Color? color;
|
||||
final ThemeType type;
|
||||
final Effect effect;
|
||||
final EdgeInsets padding;
|
||||
|
||||
const Tag({
|
||||
super.key,
|
||||
required this.text,
|
||||
this.color,
|
||||
this.type = ThemeType.primary,
|
||||
this.effect = Effect.dark,
|
||||
this.padding = const EdgeInsets.symmetric(vertical: 3, horizontal: 6),
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
///颜色
|
||||
var baseColor = switch (type) {
|
||||
ThemeType.primary => Theme.of(context).primaryColor,
|
||||
ThemeType.success => context.success,
|
||||
ThemeType.warning => context.warning,
|
||||
ThemeType.danger => context.danger,
|
||||
ThemeType.info => Color(0xFF8F9298),
|
||||
};
|
||||
if (color != null) {
|
||||
baseColor = color!;
|
||||
}
|
||||
|
||||
ColorResolver colorResolver = resolveEffectColors(baseColor, effect);
|
||||
|
||||
return Container(
|
||||
padding: padding,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: colorResolver.bg,
|
||||
border: Border.all(
|
||||
color: colorResolver.border,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
color: colorResolver.font,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
39
lib/widgets/version/check_version_update.dart
Normal file
39
lib/widgets/version/check_version_update.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:app/core/config/global.dart';
|
||||
import 'package:app/core/event/event_bus.dart';
|
||||
import 'package:app/core/event/global_event.dart';
|
||||
import 'package:app/core/utils/system.dart';
|
||||
import 'package:app/data/api/common_api.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
|
||||
///检查版本更新
|
||||
void checkUpdate() async {
|
||||
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
||||
var versionRes = await getAppVersionApi(isIos: !isAndroid());
|
||||
//判断版本是不是小于最低版本,如果不是,不弹更新
|
||||
bool isMin = _compareVersions(packageInfo.version, versionRes.lowVersion) == -1;
|
||||
if (!isMin) {
|
||||
return;
|
||||
}
|
||||
Future.delayed(const Duration(seconds: 2), () async {
|
||||
EventBus().publish(VersionUpdateEvent(versionRes));
|
||||
});
|
||||
}
|
||||
|
||||
///比较版本号
|
||||
/// 1:表示当前版本号比传入的版本号高
|
||||
/// -1:表示当前版本号比传入的版本号低
|
||||
/// 0 表示版本号相同
|
||||
int _compareVersions(String version1, String version2) {
|
||||
List<String> v1Parts = version1.split('.');
|
||||
List<String> v2Parts = version2.split('.');
|
||||
int length = v1Parts.length > v2Parts.length ? v1Parts.length : v2Parts.length;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int v1Part = i < v1Parts.length ? int.tryParse(v1Parts[i]) ?? 0 : 0;
|
||||
int v2Part = i < v2Parts.length ? int.tryParse(v2Parts[i]) ?? 0 : 0;
|
||||
|
||||
if (v1Part > v2Part) return 1;
|
||||
if (v1Part < v2Part) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
213
lib/widgets/version/version_ui.dart
Normal file
213
lib/widgets/version/version_ui.dart
Normal file
@@ -0,0 +1,213 @@
|
||||
import 'package:app/core/utils/system.dart';
|
||||
import 'package:app/core/utils/transfer/download.dart';
|
||||
import 'package:app/data/models/common/version_dto.dart';
|
||||
import 'package:app_installer/app_installer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
import '../base/button/index.dart';
|
||||
import '../base/config/config.dart';
|
||||
import '../base/tag/index.dart';
|
||||
|
||||
|
||||
void showUpdateDialog(BuildContext context, VersionDto data){
|
||||
//如果是最新版本
|
||||
showGeneralDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
barrierLabel: "Update",
|
||||
transitionDuration: const Duration(milliseconds: 300),
|
||||
pageBuilder: (context, animation, secondaryAnimation) {
|
||||
return PopScope(
|
||||
canPop: false,
|
||||
child: AppUpdateUi(
|
||||
version: data.latestVersion,
|
||||
updateNotice: data.updateContent,
|
||||
uploadUrl: data.downloadUrl,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
///下载状态枚举
|
||||
enum UploadState {
|
||||
notStarted, //未开始下载
|
||||
downloading, //下载中
|
||||
completed, //下载完毕
|
||||
}
|
||||
|
||||
class AppUpdateUi extends StatefulWidget {
|
||||
final String version;
|
||||
final List<String> updateNotice;
|
||||
final String uploadUrl; //下载地址
|
||||
|
||||
const AppUpdateUi({
|
||||
super.key,
|
||||
required this.version,
|
||||
required this.updateNotice,
|
||||
required this.uploadUrl,
|
||||
});
|
||||
|
||||
@override
|
||||
State<AppUpdateUi> createState() => _UpdateUiState();
|
||||
}
|
||||
|
||||
class _UpdateUiState extends State<AppUpdateUi> {
|
||||
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 {
|
||||
//如果是ios
|
||||
if(!isAndroid()){
|
||||
_launchAppStore();
|
||||
return;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
///跳转到appstore商店
|
||||
void _launchAppStore() async{
|
||||
final appStoreUrl = Uri.parse('itms-apps://itunes.apple.com/app/6757746410');
|
||||
if (await canLaunchUrl(appStoreUrl)) {
|
||||
print("qq");
|
||||
await launchUrl(appStoreUrl);
|
||||
} else {
|
||||
EasyLoading.showToast("无法打开App Store");
|
||||
}
|
||||
}
|
||||
|
||||
@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(
|
||||
onPressed: _uploadState == UploadState.downloading
|
||||
? null
|
||||
: _handUploadApk,
|
||||
child: Text(text),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user