feat(core): add theme, shared widgets and validators

This commit is contained in:
qzl
2026-02-25 10:52:18 +08:00
parent febd10d64e
commit 8c0b0a25c3
8 changed files with 389 additions and 2 deletions
+41
View File
@@ -0,0 +1,41 @@
class Validators {
Validators._();
static String? email(String? value) {
if (value == null || value.isEmpty) {
return '请输入邮箱';
}
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailRegex.hasMatch(value)) {
return '请输入有效的邮箱地址';
}
return null;
}
static String? password(String? value) {
if (value == null || value.isEmpty) {
return '请输入密码';
}
if (value.length < 8) {
return '密码至少需要8位';
}
return null;
}
static String? required(String? value, [String? fieldName]) {
if (value == null || value.isEmpty) {
return '请输入${fieldName ?? '内容'}';
}
return null;
}
static String? nickname(String? value) {
if (value == null || value.isEmpty) {
return '请输入昵称';
}
if (value.length < 2) {
return '昵称至少需要2个字符';
}
return null;
}
}
+77
View File
@@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import '../../core/theme/design_tokens.dart';
class AppButton extends StatelessWidget {
final String text;
final VoidCallback? onPressed;
final bool isPrimary;
final bool isOutlined;
final double height;
final bool isLoading;
const AppButton({
super.key,
required this.text,
this.onPressed,
this.isPrimary = true,
this.isOutlined = false,
this.height = 44,
this.isLoading = false,
});
@override
Widget build(BuildContext context) {
if (isOutlined) {
return SizedBox(
height: height,
child: OutlinedButton(
onPressed: isLoading ? null : onPressed,
style: OutlinedButton.styleFrom(
backgroundColor: AppColors.background,
foregroundColor: AppColors.slate500,
side: const BorderSide(color: AppColors.input),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppRadius.sm),
),
),
child: isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: Text(
text,
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
),
),
),
);
}
return SizedBox(
height: height,
child: ElevatedButton(
onPressed: isLoading ? null : onPressed,
child: isLoading
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(
strokeWidth: 2,
color: AppColors.primaryForeground,
),
)
: Text(
text,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
+51
View File
@@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import '../../core/theme/design_tokens.dart';
class AppInput extends StatelessWidget {
final String label;
final String hint;
final TextEditingController? controller;
final bool obscureText;
final TextInputType? keyboardType;
final Widget? suffix;
final int? maxLines;
final bool enabled;
const AppInput({
super.key,
required this.label,
required this.hint,
this.controller,
this.obscureText = false,
this.keyboardType,
this.suffix,
this.maxLines = 1,
this.enabled = true,
});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: AppColors.slate600,
),
),
const SizedBox(height: 6),
TextField(
controller: controller,
obscureText: obscureText,
keyboardType: keyboardType,
maxLines: maxLines,
enabled: enabled,
decoration: InputDecoration(hintText: hint, suffixIcon: suffix),
),
],
);
}
}
+53
View File
@@ -0,0 +1,53 @@
import 'package:flutter/material.dart';
class PageHeader extends StatelessWidget {
final Widget? leading;
final Widget? trailing;
final double height;
const PageHeader({super.key, this.leading, this.trailing, this.height = 64});
@override
Widget build(BuildContext context) {
return SizedBox(
height: height,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
leading ?? const SizedBox.shrink(),
trailing ?? const SizedBox.shrink(),
],
),
),
);
}
}
class BackButton extends StatelessWidget {
final VoidCallback? onPressed;
const BackButton({super.key, this.onPressed});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPressed ?? () => Navigator.of(context).pop(),
child: Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: const Color(0xFFF8FAFF),
borderRadius: BorderRadius.circular(18),
border: Border.all(color: const Color(0xFFDEE7F6)),
),
child: const Icon(
Icons.chevron_left,
size: 18,
color: Color(0xFF334155),
),
),
);
}
}
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import '../../core/theme/design_tokens.dart';
class WarningBanner extends StatelessWidget {
final String message;
final bool visible;
const WarningBanner({super.key, required this.message, this.visible = true});
@override
Widget build(BuildContext context) {
if (!visible) return const SizedBox.shrink();
return Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
decoration: BoxDecoration(
color: AppColors.warningBackground,
borderRadius: BorderRadius.circular(AppRadius.sm),
),
child: Row(
children: [
const Icon(
Icons.warning_amber_rounded,
size: 16,
color: AppColors.warningText,
),
const SizedBox(width: 8),
Expanded(
child: Text(
message,
style: const TextStyle(
fontSize: 13,
color: AppColors.warningText,
),
),
),
],
),
);
}
}