Files
monisuo/flutter_monisuo/lib/ui/shared/ui_constants.dart

96 lines
2.3 KiB
Dart
Raw Normal View History

2026-04-07 01:05:05 +08:00
/// UI 常量整合導出
2026-03-23 02:43:35 +08:00
///
2026-04-07 01:05:05 +08:00
/// 統一導出所有設計 token方便使用
2026-03-23 02:43:35 +08:00
/// 使用方式: import 'ui/shared/ui_constants.dart';
2026-04-07 01:05:05 +08:00
// 導出顏色系統
export '../../core/theme/app_color_scheme.dart';
2026-03-23 02:43:35 +08:00
2026-04-07 01:05:05 +08:00
// 導出主題配置 (包含 AppTextStyles, AppSpacing, AppRadius, AppBreakpoints)
2026-03-23 02:43:35 +08:00
export '../../core/theme/app_theme.dart';
2026-03-23 00:43:19 +08:00
2026-04-07 01:05:05 +08:00
/// 表單驗證器
2026-03-23 02:43:35 +08:00
///
2026-04-07 01:05:05 +08:00
/// 提供常用的表單驗證方法
2026-03-23 00:43:19 +08:00
class Validators {
Validators._();
2026-04-07 01:05:05 +08:00
/// 金額驗證
2026-03-23 00:43:19 +08:00
static String? amount(String? value) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入金額';
2026-03-23 00:43:19 +08:00
}
final amount = double.tryParse(value);
if (amount == null || amount <= 0) {
2026-04-07 01:05:05 +08:00
return '請輸入有效金額';
2026-03-23 00:43:19 +08:00
}
return null;
}
2026-04-07 01:05:05 +08:00
/// 價格驗證
2026-03-23 00:43:19 +08:00
static String? price(String? value) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入價格';
2026-03-23 00:43:19 +08:00
}
final price = double.tryParse(value);
if (price == null || price <= 0) {
2026-04-07 01:05:05 +08:00
return '請輸入有效價格';
2026-03-23 00:43:19 +08:00
}
return null;
}
2026-04-07 01:05:05 +08:00
/// 數量驗證
2026-03-23 00:43:19 +08:00
static String? quantity(String? value) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入數量';
2026-03-23 00:43:19 +08:00
}
final quantity = double.tryParse(value);
if (quantity == null || quantity <= 0) {
2026-04-07 01:05:05 +08:00
return '請輸入有效數量';
2026-03-23 00:43:19 +08:00
}
return null;
}
2026-04-07 01:05:05 +08:00
/// 必填字段驗證
2026-03-23 00:43:19 +08:00
static String? required(String? value, String fieldName) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入$fieldName';
2026-03-23 00:43:19 +08:00
}
return null;
}
2026-03-23 02:43:35 +08:00
2026-04-07 01:05:05 +08:00
/// 用戶名驗證
2026-03-23 02:43:35 +08:00
static String? username(String? value) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入用戶名';
2026-03-23 02:43:35 +08:00
}
if (value.length < 3) {
2026-04-07 01:05:05 +08:00
return '用戶名至少 3 個字符';
2026-03-23 02:43:35 +08:00
}
return null;
}
2026-04-07 01:05:05 +08:00
/// 密碼驗證
2026-03-23 02:43:35 +08:00
static String? password(String? value) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入密碼';
2026-03-23 02:43:35 +08:00
}
if (value.length < 6) {
2026-04-07 01:05:05 +08:00
return '密碼至少 6 個字符';
2026-03-23 02:43:35 +08:00
}
return null;
}
2026-04-07 01:05:05 +08:00
/// 郵箱驗證
2026-03-23 02:43:35 +08:00
static String? email(String? value) {
if (value == null || value.isEmpty) {
2026-04-07 01:05:05 +08:00
return '請輸入郵箱';
2026-03-23 02:43:35 +08:00
}
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailRegex.hasMatch(value)) {
2026-04-07 01:05:05 +08:00
return '請輸入有效的郵箱地址';
2026-03-23 02:43:35 +08:00
}
return null;
}
2026-03-23 00:43:19 +08:00
}