feat: 优化前端 UI 组件与交互体验
- 优化日历、待办、消息等页面交互 - 更新 ChatBloc 与 UI Schema 渲染 - 优化联系人、首页、设置页面体验
This commit is contained in:
@@ -1,339 +1,398 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:social_app/core/theme/design_tokens.dart';
|
||||
import '../../data/models/tool_result.dart';
|
||||
|
||||
/// 卡片类型常量
|
||||
const _calendarCardType = 'calendar_card.v1';
|
||||
const _calendarListType = 'calendar_event_list.v1';
|
||||
const _calendarOperationType = 'calendar_operation.v1';
|
||||
const _errorCardType = 'error_card.v1';
|
||||
const _aiGeneratedSource = 'ai_generated';
|
||||
const _agentGeneratedSource = 'agent_generated';
|
||||
const _primaryActionType = 'primary';
|
||||
import 'package:social_app/core/theme/design_tokens.dart';
|
||||
import 'package:social_app/shared/widgets/toast/toast.dart';
|
||||
import 'package:social_app/shared/widgets/toast/toast_type.dart';
|
||||
|
||||
class UiSchemaRenderer {
|
||||
static Widget render(UiCard card) {
|
||||
return switch (card.cardType) {
|
||||
_calendarCardType => _renderCalendarCard(card),
|
||||
_calendarListType => _renderCalendarList(card),
|
||||
_calendarOperationType => _renderCalendarOperation(card),
|
||||
_errorCardType => _renderErrorCard(card),
|
||||
_ => _renderUnknownCard(card),
|
||||
static Widget renderSchema(Map<String, dynamic>? schema) {
|
||||
if (schema == null || schema.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
final root = _asMap(schema['root']);
|
||||
if (root == null) {
|
||||
return _fallback('无效 UI Schema');
|
||||
}
|
||||
return _renderLayoutNode(root);
|
||||
}
|
||||
|
||||
static Widget _renderLayoutNode(Map<String, dynamic> node) {
|
||||
final type = _asString(node['type']);
|
||||
return switch (type) {
|
||||
'stack' => _renderStack(node),
|
||||
'grid' => _renderGrid(node),
|
||||
_ => _fallback('不支持的布局节点: $type'),
|
||||
};
|
||||
}
|
||||
|
||||
static Widget _renderCalendarCard(UiCard card) {
|
||||
final data = CalendarCardData.fromJson(card.data);
|
||||
final color = data.color != null
|
||||
? Color(int.parse(data.color!.replaceFirst('#', '0xFF')))
|
||||
: AppColors.blue500;
|
||||
final isAiGenerated =
|
||||
data.sourceType == _aiGeneratedSource ||
|
||||
data.sourceType == _agentGeneratedSource;
|
||||
static Widget _renderNode(Map<String, dynamic> node) {
|
||||
final type = _asString(node['type']);
|
||||
if (node['visible'] == false) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return switch (type) {
|
||||
'text' => _renderText(node),
|
||||
'icon' => _renderIcon(node),
|
||||
'badge' => _renderBadge(node),
|
||||
'button' => _renderButton(node),
|
||||
'kv' => _renderKv(node),
|
||||
'divider' => _renderDivider(node),
|
||||
'stack' => _renderStack(node),
|
||||
'grid' => _renderGrid(node),
|
||||
_ => _fallback('未知节点: $type'),
|
||||
};
|
||||
}
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.messageCardBg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||||
border: Border.all(color: AppColors.messageCardBorder),
|
||||
),
|
||||
child: Column(
|
||||
static Widget _renderStack(Map<String, dynamic> node) {
|
||||
final children = _asList(
|
||||
node['children'],
|
||||
).whereType<Map<String, dynamic>>().map(_renderNode).toList();
|
||||
final gap = _asDouble(node['gap'], fallback: AppSpacing.sm);
|
||||
final direction = _asString(node['direction'], fallback: 'vertical');
|
||||
|
||||
Widget content;
|
||||
if (direction == 'horizontal') {
|
||||
content = Wrap(
|
||||
direction: Axis.horizontal,
|
||||
spacing: gap,
|
||||
runSpacing: gap,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: children,
|
||||
);
|
||||
} else {
|
||||
content = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
height: AppSpacing.sm,
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(AppRadius.lg),
|
||||
topRight: Radius.circular(AppRadius.lg),
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.all(AppSpacing.lg),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (isAiGenerated) ...[
|
||||
_buildAiTag(),
|
||||
SizedBox(height: AppSpacing.sm),
|
||||
],
|
||||
Text(
|
||||
_formatTime(data.startAt, data.endAt),
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
),
|
||||
SizedBox(height: AppSpacing.sm),
|
||||
Text(
|
||||
data.title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
),
|
||||
if (data.description != null) ...[
|
||||
SizedBox(height: AppSpacing.xs),
|
||||
Text(
|
||||
data.description!,
|
||||
style: TextStyle(fontSize: 14, color: AppColors.slate600),
|
||||
),
|
||||
],
|
||||
if (data.location != null) ...[
|
||||
SizedBox(height: AppSpacing.sm),
|
||||
_buildLocation(data.location!),
|
||||
],
|
||||
if (card.actions != null && card.actions!.isNotEmpty) ...[
|
||||
SizedBox(height: AppSpacing.md),
|
||||
_buildActions(card.actions!),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
children: _withGap(children, gap),
|
||||
);
|
||||
}
|
||||
return _wrapSurface(node, content);
|
||||
}
|
||||
|
||||
static Widget _renderGrid(Map<String, dynamic> node) {
|
||||
final children = _asList(
|
||||
node['children'],
|
||||
).whereType<Map<String, dynamic>>().map(_renderNode).toList();
|
||||
final columns = _asInt(node['columns'], fallback: 2).clamp(1, 3);
|
||||
final gap = _asDouble(node['gap'], fallback: AppSpacing.sm);
|
||||
final tiles = List.generate(children.length, (index) => children[index]);
|
||||
return _wrapSurface(
|
||||
node,
|
||||
GridView.count(
|
||||
crossAxisCount: columns,
|
||||
crossAxisSpacing: gap,
|
||||
mainAxisSpacing: gap,
|
||||
childAspectRatio: 1.6,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
children: tiles,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _buildAiTag() {
|
||||
static Widget _renderText(Map<String, dynamic> node) {
|
||||
final role = _asString(node['role'], fallback: 'body');
|
||||
final status = _asString(node['status']);
|
||||
final style = switch (role) {
|
||||
'title' => const TextStyle(
|
||||
fontSize: 17,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppColors.slate900,
|
||||
height: 1.25,
|
||||
),
|
||||
'subtitle' => const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate800,
|
||||
),
|
||||
'caption' => const TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
'code' => const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.slate700,
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
_ => const TextStyle(
|
||||
fontSize: 14,
|
||||
color: AppColors.slate700,
|
||||
height: 1.45,
|
||||
),
|
||||
};
|
||||
return Text(
|
||||
_asString(node['content']),
|
||||
maxLines: _asIntOrNull(node['maxLines']),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: style.copyWith(color: _statusTextColor(status, style.color)),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _renderIcon(Map<String, dynamic> node) {
|
||||
final value = _asString(node['value']);
|
||||
if (_asString(node['source']) == 'emoji' && value.isNotEmpty) {
|
||||
return Text(value, style: const TextStyle(fontSize: 20));
|
||||
}
|
||||
return Icon(Icons.bubble_chart_rounded, color: _statusTextColor('', null));
|
||||
}
|
||||
|
||||
static Widget _renderBadge(Map<String, dynamic> node) {
|
||||
final status = _asString(node['status']);
|
||||
final fg =
|
||||
_statusTextColor(status, AppColors.slate700) ?? AppColors.slate700;
|
||||
final bg = _statusBackground(status);
|
||||
return Container(
|
||||
padding: EdgeInsets.symmetric(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.sm,
|
||||
vertical: AppSpacing.xs,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.messageTagBg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.sm),
|
||||
color: bg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
),
|
||||
child: Text(
|
||||
'AI生成',
|
||||
style: TextStyle(fontSize: 10, color: AppColors.blue600),
|
||||
_asString(node['label']),
|
||||
style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: fg),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _buildLocation(String location) {
|
||||
return Row(
|
||||
children: [
|
||||
Icon(Icons.location_on_outlined, size: 16, color: AppColors.slate500),
|
||||
SizedBox(width: AppSpacing.xs),
|
||||
Text(
|
||||
location,
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
),
|
||||
],
|
||||
static Widget _renderButton(Map<String, dynamic> node) {
|
||||
final style = _asString(node['style'], fallback: 'secondary');
|
||||
final action = _asMap(node['action']);
|
||||
final disabled = node['disabled'] == true;
|
||||
return Builder(
|
||||
builder: (context) {
|
||||
return ElevatedButton(
|
||||
onPressed: disabled
|
||||
? null
|
||||
: () {
|
||||
final actionType = _asString(action?['type']);
|
||||
if (actionType == 'copy') {
|
||||
Toast.show(context, '已复制', type: ToastType.success);
|
||||
} else {
|
||||
Toast.show(context, '该操作暂未接入', type: ToastType.info);
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
elevation: 0,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.md,
|
||||
vertical: AppSpacing.sm,
|
||||
),
|
||||
backgroundColor: style == 'primary'
|
||||
? AppColors.blue600
|
||||
: AppColors.homeComposerAccent,
|
||||
foregroundColor: style == 'primary'
|
||||
? AppColors.white
|
||||
: AppColors.slate700,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
_asString(node['label'], fallback: '操作'),
|
||||
style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _buildActions(List<CardAction> actions) {
|
||||
return Wrap(
|
||||
spacing: AppSpacing.sm,
|
||||
children: actions.map((action) => _buildActionButton(action)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _buildActionButton(CardAction action) {
|
||||
final isPrimary = action.type == _primaryActionType;
|
||||
return GestureDetector(
|
||||
onTap: () => _handleAction(action),
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.md,
|
||||
vertical: AppSpacing.sm,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: isPrimary ? AppColors.blue500 : AppColors.messageBtnWrap,
|
||||
borderRadius: BorderRadius.circular(AppRadius.sm),
|
||||
border: Border.all(
|
||||
color: isPrimary ? AppColors.blue500 : AppColors.messageBtnBorder,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
action.label,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: isPrimary ? AppColors.white : AppColors.slate600,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _renderCalendarList(UiCard card) {
|
||||
final rawItems = card.data['items'];
|
||||
final items = rawItems is List ? rawItems : const [];
|
||||
final paginationRaw = card.data['pagination'];
|
||||
final pagination = paginationRaw is Map<String, dynamic>
|
||||
? paginationRaw
|
||||
: const <String, dynamic>{};
|
||||
final page = pagination['page'];
|
||||
final total = pagination['total'];
|
||||
|
||||
return Container(
|
||||
padding: EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.messageCardBg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||||
border: Border.all(color: AppColors.messageCardBorder),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'日程列表',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate900,
|
||||
),
|
||||
),
|
||||
if (page != null || total != null) ...[
|
||||
SizedBox(height: AppSpacing.xs),
|
||||
Text(
|
||||
'第${page ?? '-'}页 · 共${total ?? '-'}条',
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
),
|
||||
],
|
||||
SizedBox(height: AppSpacing.sm),
|
||||
if (items.isEmpty)
|
||||
Text(
|
||||
'暂无日程',
|
||||
style: TextStyle(fontSize: 14, color: AppColors.slate500),
|
||||
),
|
||||
for (final item in items)
|
||||
if (item is Map<String, dynamic>)
|
||||
Padding(
|
||||
padding: EdgeInsets.only(bottom: AppSpacing.xs),
|
||||
static Widget _renderKv(Map<String, dynamic> node) {
|
||||
final items = _asList(
|
||||
node['items'],
|
||||
).whereType<Map<String, dynamic>>().toList();
|
||||
if (items.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: _withGap(
|
||||
items.map((item) {
|
||||
final label = _asString(
|
||||
item['label'],
|
||||
fallback: _asString(item['key']),
|
||||
);
|
||||
final value = item['value']?.toString() ?? '-';
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: Text(
|
||||
item['title']?.toString() ?? '未命名日程',
|
||||
style: TextStyle(fontSize: 14, color: AppColors.slate700),
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
Expanded(
|
||||
flex: 5,
|
||||
child: Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: AppColors.slate800,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList(),
|
||||
AppSpacing.xs,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _renderCalendarOperation(UiCard card) {
|
||||
final ok = card.data['ok'] == true;
|
||||
final operation = card.data['operation']?.toString() ?? 'operation';
|
||||
final message = card.data['message']?.toString() ?? (ok ? '操作成功' : '操作失败');
|
||||
static Widget _renderDivider(Map<String, dynamic> node) {
|
||||
final inset = _asDouble(node['inset'], fallback: 0);
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: inset),
|
||||
child: const Divider(height: 1, color: AppColors.slate200),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _wrapSurface(Map<String, dynamic> node, Widget child) {
|
||||
final appearance = _asString(node['appearance'], fallback: 'plain');
|
||||
final status = _asString(node['status']);
|
||||
if (appearance == 'plain') {
|
||||
return child;
|
||||
}
|
||||
final bg = switch (appearance) {
|
||||
'section' => AppColors.homeComposerInner,
|
||||
_ => _statusBackground(status),
|
||||
};
|
||||
final borderColor = switch (status) {
|
||||
'success' => AppColors.feedbackSuccessBorder,
|
||||
'warning' => AppColors.feedbackWarningBorder,
|
||||
'error' => AppColors.feedbackErrorBorder,
|
||||
_ => AppColors.homeConversationBorder,
|
||||
};
|
||||
return Container(
|
||||
padding: EdgeInsets.all(AppSpacing.lg),
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: ok ? AppColors.messageCardBg : AppColors.warningBackground,
|
||||
color: bg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||||
border: Border.all(
|
||||
color: ok ? AppColors.messageCardBorder : AppColors.red400,
|
||||
border: Border.all(color: borderColor),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: AppColors.blue100.withValues(alpha: 0.35),
|
||||
blurRadius: 18,
|
||||
offset: const Offset(0, 8),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _fallback(String text) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(AppSpacing.md),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.feedbackWarningSurface,
|
||||
border: Border.all(color: AppColors.feedbackWarningBorder),
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
),
|
||||
child: Text(
|
||||
text,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.feedbackWarningText,
|
||||
),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'日程$operation结果',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: ok ? AppColors.slate900 : AppColors.red600,
|
||||
),
|
||||
),
|
||||
SizedBox(height: AppSpacing.xs),
|
||||
Text(
|
||||
message,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: ok ? AppColors.slate600 : AppColors.red600,
|
||||
),
|
||||
),
|
||||
if (card.actions != null && card.actions!.isNotEmpty) ...[
|
||||
SizedBox(height: AppSpacing.md),
|
||||
_buildActions(card.actions!),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _renderErrorCard(UiCard card) {
|
||||
final message = card.data['message'] as String? ?? '发生错误';
|
||||
|
||||
return Container(
|
||||
padding: EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.warningBackground,
|
||||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||||
border: Border.all(color: AppColors.red400),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.error_outline, size: 20, color: AppColors.red600),
|
||||
SizedBox(width: AppSpacing.sm),
|
||||
Expanded(
|
||||
child: Text(
|
||||
message,
|
||||
style: TextStyle(fontSize: 14, color: AppColors.red600),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _renderUnknownCard(UiCard card) {
|
||||
return Container(
|
||||
padding: EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.messageCardBg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||||
border: Border.all(color: AppColors.border),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'未知卡片类型: ${card.cardType}',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.slate600,
|
||||
),
|
||||
),
|
||||
SizedBox(height: AppSpacing.sm),
|
||||
Text(
|
||||
card.data.toString(),
|
||||
style: TextStyle(fontSize: 12, color: AppColors.slate500),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static String _formatTime(String startAt, String? endAt) {
|
||||
try {
|
||||
final start = DateTime.parse(startAt);
|
||||
final buffer = StringBuffer();
|
||||
|
||||
buffer.write('${start.month}月${start.day}日 ');
|
||||
buffer.write(
|
||||
'${start.hour.toString().padLeft(2, '0')}:${start.minute.toString().padLeft(2, '0')}',
|
||||
);
|
||||
|
||||
if (endAt != null) {
|
||||
final end = DateTime.parse(endAt);
|
||||
buffer.write(
|
||||
' - ${end.hour.toString().padLeft(2, '0')}:${end.minute.toString().padLeft(2, '0')}',
|
||||
);
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
} catch (e) {
|
||||
return startAt;
|
||||
static List<Widget> _withGap(List<Widget> widgets, double gap) {
|
||||
if (widgets.isEmpty) {
|
||||
return const [];
|
||||
}
|
||||
final result = <Widget>[];
|
||||
for (var i = 0; i < widgets.length; i++) {
|
||||
if (i > 0) {
|
||||
result.add(SizedBox(height: gap));
|
||||
}
|
||||
result.add(widgets[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void _handleAction(CardAction action) {
|
||||
// TODO: 实现 action 处理
|
||||
static Color _statusBackground(String status) {
|
||||
return switch (status) {
|
||||
'success' => AppColors.feedbackSuccessSurface,
|
||||
'warning' => AppColors.feedbackWarningSurface,
|
||||
'error' => AppColors.feedbackErrorSurface,
|
||||
'pending' => AppColors.feedbackInfoSurface,
|
||||
_ => AppColors.homeConversationSurface,
|
||||
};
|
||||
}
|
||||
|
||||
static Color? _statusTextColor(String status, Color? fallback) {
|
||||
return switch (status) {
|
||||
'success' => AppColors.feedbackSuccessText,
|
||||
'warning' => AppColors.feedbackWarningText,
|
||||
'error' => AppColors.feedbackErrorText,
|
||||
'pending' => AppColors.feedbackInfoText,
|
||||
_ => fallback,
|
||||
};
|
||||
}
|
||||
|
||||
static Map<String, dynamic>? _asMap(Object? value) {
|
||||
if (value is Map<String, dynamic>) {
|
||||
return value;
|
||||
}
|
||||
if (value is Map) {
|
||||
final result = <String, dynamic>{};
|
||||
for (final entry in value.entries) {
|
||||
if (entry.key is String) {
|
||||
result[entry.key as String] = entry.value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static List<dynamic> _asList(Object? value) {
|
||||
return value is List ? value : const [];
|
||||
}
|
||||
|
||||
static String _asString(Object? value, {String fallback = ''}) {
|
||||
return value is String ? value : fallback;
|
||||
}
|
||||
|
||||
static int _asInt(Object? value, {int fallback = 0}) {
|
||||
if (value is int) {
|
||||
return value;
|
||||
}
|
||||
if (value is double) {
|
||||
return value.toInt();
|
||||
}
|
||||
if (value is String) {
|
||||
return int.tryParse(value) ?? fallback;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
static int? _asIntOrNull(Object? value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return _asInt(value);
|
||||
}
|
||||
|
||||
static double _asDouble(Object? value, {double fallback = 0}) {
|
||||
if (value is double) {
|
||||
return value;
|
||||
}
|
||||
if (value is int) {
|
||||
return value.toDouble();
|
||||
}
|
||||
if (value is String) {
|
||||
return double.tryParse(value) ?? fallback;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user