qhd-prevention-flutter/lib/tools/tools.dart

134 lines
3.4 KiB
Dart
Raw Normal View History

2025-07-03 09:45:15 +08:00
import 'dart:ui';
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2025-07-07 16:49:05 +08:00
import 'package:package_info_plus/package_info_plus.dart';
2025-07-03 09:45:15 +08:00
int getRandomWithNum(int min, int max) {
final random = Random();
return random.nextInt(max) + min; // 生成随机数
}
double screenWidth(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
return screenWidth;
}
void pushPage(Widget page, BuildContext context) {
Navigator.push(context, MaterialPageRoute(builder: (context) => page));
}
2025-07-07 16:49:05 +08:00
void present(Widget page, BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(
fullscreenDialog: true,
builder: (context) => page,
),
);
}
2025-07-03 09:45:15 +08:00
/// 文本样式工具类
/// 文本样式工具类
/// 文本样式工具类,返回 Text Widget
class HhTextStyleUtils {
/// 主要标题,返回 Text
/// [text]: 文本内容
/// [color]: 文本颜色,默认黑色
/// [fontSize]: 字体大小默认16.0
/// [bold]: 是否加粗默认true
static Text mainTitle(
String text, {
Color color = Colors.black,
double fontSize = 16.0,
bool bold = true,
}) {
return Text(
text,
style: TextStyle(
color: color,
fontSize: fontSize,
fontWeight: bold ? FontWeight.bold : FontWeight.normal,
),
);
}
2025-07-07 16:49:05 +08:00
static TextStyle secondaryTitleStyle = TextStyle(color:Colors.black54, fontSize: 15.0);
2025-07-03 09:45:15 +08:00
/// 次要标题,返回 Text
/// [text]: 文本内容
/// [color]: 文本颜色,默认深灰
/// [fontSize]: 字体大小默认14.0
/// [bold]: 是否加粗默认false
static Text secondaryTitle(
String text, {
Color color = Colors.black54,
double fontSize = 14.0,
bool bold = false,
}) {
return Text(
text,
style: TextStyle(
2025-07-07 16:49:05 +08:00
2025-07-03 09:45:15 +08:00
color: color,
fontSize: fontSize,
fontWeight: bold ? FontWeight.bold : FontWeight.normal,
),
);
}
/// 小文字,返回 Text
/// [text]: 文本内容
/// [color]: 文本颜色,默认灰色
/// [fontSize]: 字体大小默认12.0
/// [bold]: 是否加粗默认false
static Text smallText(
String text, {
Color color = Colors.black54,
double fontSize = 12.0,
bool bold = false,
}) {
return Text(
text,
style: TextStyle(
color: color,
fontSize: fontSize,
fontWeight: bold ? FontWeight.bold : FontWeight.normal,
),
);
}
}
2025-07-07 16:49:05 +08:00
/// 版本信息模型类
class AppVersionInfo {
final String versionName; // 版本名称(如 1.0.0
final String buildNumber; // 构建号(如 1
final String fullVersion; // 完整版本(如 1.0.0+1
AppVersionInfo({
required this.versionName,
required this.buildNumber,
required this.fullVersion,
});
@override
String toString() {
return fullVersion;
}
}
// 获取应用版本信息的方法
Future<AppVersionInfo> getAppVersion() async {
try {
final packageInfo = await PackageInfo.fromPlatform();
return AppVersionInfo(
versionName: packageInfo.version,
buildNumber: packageInfo.buildNumber,
fullVersion: '${packageInfo.version}+${packageInfo.buildNumber}',
);
} catch (e) {
// 获取失败时返回默认值
return AppVersionInfo(
versionName: '1.0.0',
buildNumber: '1',
fullVersion: '1.0.0+0',
);
}
}