## 样式主题重点优化 ### 颜色映射(注重主题一致性) - mutedForeground → onSurfaceVariant - border → outline - card → surfaceContainer - destructive → error - 保留所有 AppColorScheme 自定义颜色 ### 文本样式映射 - theme.textTheme.h1/muted/large → AppTextStyles.xxx(context) - 统一使用项目定义的文本样式系统 ### 组件替换(20个文件) - ShadApp → MaterialApp(移除 ShadThemeData) - ShadButton → ElevatedButton/OutlinedButton - ShadDialog → AlertDialog - ShadInputFormField → MaterialInput - ShadSelect → DropdownButtonFormField - ShadCard → Card - showShadDialog → showDialog ### 依赖变更 - 移除:shadcn_ui: ^0.52.1 - 添加:lucide_icons_flutter: ^2.0.0 ### 业务逻辑保护 ✅ 所有 onPressed/onChanged/validator 回调保持不变 ✅ 所有 controller/focusNode 数据绑定保持不变 ✅ 所有布局结构(Column/Row/Padding)保持不变 ✅ 仅替换 UI 组件层,业务逻辑完全保留
210 lines
5.5 KiB
Dart
210 lines
5.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../core/theme/app_spacing.dart';
|
|
import '../../core/theme/app_theme.dart';
|
|
|
|
/// 現代彈窗模板 - 基於 modernization-v2.md 規範
|
|
///
|
|
/// 使用方法:
|
|
/// ```dart
|
|
/// ModernDialog.show(
|
|
/// context: context,
|
|
/// title: '確認操作',
|
|
/// description: '確定要執行此操作嗎?',
|
|
/// actions: [
|
|
/// ModernDialogAction(label: '取消', isDestructive: false),
|
|
/// ModernDialogAction(label: '確認', isPrimary: true),
|
|
/// ],
|
|
/// );
|
|
/// ```
|
|
class ModernDialog extends StatelessWidget {
|
|
final String? title;
|
|
final Widget? titleWidget;
|
|
final String? description;
|
|
final Widget? content;
|
|
final List<ModernDialogAction>? actions;
|
|
final VoidCallback? onClose;
|
|
|
|
const ModernDialog({
|
|
super.key,
|
|
this.title,
|
|
this.titleWidget,
|
|
this.description,
|
|
this.content,
|
|
this.actions,
|
|
this.onClose,
|
|
});
|
|
|
|
/// 顯示現代彈窗
|
|
static Future<T?> show<T>({
|
|
required BuildContext context,
|
|
String? title,
|
|
Widget? titleWidget,
|
|
String? description,
|
|
Widget? content,
|
|
List<ModernDialogAction>? actions,
|
|
bool barrierDismissible = true,
|
|
}) {
|
|
return showDialog<T>(
|
|
context: context,
|
|
barrierDismissible: barrierDismissible,
|
|
builder: (context) => ModernDialog(
|
|
title: title,
|
|
titleWidget: titleWidget,
|
|
description: description,
|
|
content: content,
|
|
actions: actions,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 顯示確認彈窗
|
|
static Future<bool> confirm({
|
|
required BuildContext context,
|
|
required String title,
|
|
String? description,
|
|
String confirmText = '確認',
|
|
String cancelText = '取消',
|
|
bool isDestructive = false,
|
|
}) async {
|
|
final result = await show<bool>(
|
|
context: context,
|
|
title: title,
|
|
description: description,
|
|
actions: [
|
|
ModernDialogAction(label: cancelText, returnValue: false),
|
|
ModernDialogAction(
|
|
label: confirmText,
|
|
returnValue: true,
|
|
isPrimary: true,
|
|
isDestructive: isDestructive,
|
|
),
|
|
],
|
|
);
|
|
return result ?? false;
|
|
}
|
|
|
|
/// 顯示信息彈窗
|
|
static Future<void> info({
|
|
required BuildContext context,
|
|
required String title,
|
|
String? description,
|
|
String buttonText = '知道了',
|
|
}) {
|
|
return show(
|
|
context: context,
|
|
title: title,
|
|
description: description,
|
|
actions: [
|
|
ModernDialogAction(label: buttonText, isPrimary: true),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
|
|
return Dialog(
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(AppRadius.xl),
|
|
),
|
|
backgroundColor: theme.colorScheme.surfaceContainer,
|
|
child: Container(
|
|
padding: const EdgeInsets.all(AppSpacing.lg),
|
|
constraints: const BoxConstraints(maxWidth: 400),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// 標題
|
|
if (titleWidget != null)
|
|
titleWidget!
|
|
else if (title != null)
|
|
Text(
|
|
title!,
|
|
style: AppTextStyles.headlineSmall(context).copyWith(
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
if (title != null || titleWidget != null)
|
|
const SizedBox(height: AppSpacing.md),
|
|
// 描述
|
|
if (description != null) ...[
|
|
Text(
|
|
description!,
|
|
style: AppTextStyles.bodyMedium(context).copyWith(color: theme.colorScheme.onSurfaceVariant),
|
|
),
|
|
const SizedBox(height: AppSpacing.md),
|
|
],
|
|
// 自定義內容
|
|
if (content != null) ...[
|
|
content!,
|
|
const SizedBox(height: AppSpacing.lg),
|
|
],
|
|
// 按鈕
|
|
if (actions != null && actions!.isNotEmpty)
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: _buildActions(context),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
List<Widget> _buildActions(BuildContext context) {
|
|
return actions!.asMap().entries.map((entry) {
|
|
final index = entry.key;
|
|
final action = entry.value;
|
|
|
|
return Padding(
|
|
padding: EdgeInsets.only(left: index > 0 ? AppSpacing.sm : 0),
|
|
child: _buildActionButton(context, action),
|
|
);
|
|
}).toList();
|
|
}
|
|
|
|
Widget _buildActionButton(BuildContext context, ModernDialogAction action) {
|
|
final theme = Theme.of(context);
|
|
|
|
if (action.isPrimary) {
|
|
return ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: action.isDestructive ? theme.colorScheme.error : theme.colorScheme.primary,
|
|
),
|
|
onPressed: () {
|
|
Navigator.of(context).pop(action.returnValue);
|
|
action.onPressed?.call();
|
|
},
|
|
child: Text(action.label),
|
|
);
|
|
}
|
|
|
|
return OutlinedButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop(action.returnValue);
|
|
action.onPressed?.call();
|
|
},
|
|
child: Text(action.label),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 彈窗按鈕配置
|
|
class ModernDialogAction {
|
|
final String label;
|
|
final dynamic returnValue;
|
|
final bool isPrimary;
|
|
final bool isDestructive;
|
|
final VoidCallback? onPressed;
|
|
|
|
const ModernDialogAction({
|
|
required this.label,
|
|
this.returnValue,
|
|
this.isPrimary = false,
|
|
this.isDestructive = false,
|
|
this.onPressed,
|
|
});
|
|
}
|