feat(apps): 重构 UI 架构为 presentation 层并新增 l10n 国际化支持
This commit is contained in:
@@ -2,10 +2,10 @@ import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'tool_result.g.dart';
|
||||
|
||||
/// Schema 版本常量
|
||||
/// Default schema version.
|
||||
const _defaultSchemaVersion = 'v1';
|
||||
|
||||
/// 工具执行结果(给 AI 的原始数据)
|
||||
/// Raw tool execution result used by the assistant runtime.
|
||||
@JsonSerializable()
|
||||
class ToolResult {
|
||||
final String? eventId;
|
||||
@@ -20,7 +20,7 @@ class ToolResult {
|
||||
Map<String, dynamic> toJson() => _$ToolResultToJson(this);
|
||||
}
|
||||
|
||||
/// UI 卡片 Schema(给 UI 渲染)
|
||||
/// UI card schema consumed by frontend renderers.
|
||||
@JsonSerializable()
|
||||
class UiCard {
|
||||
@JsonKey(name: 'type')
|
||||
@@ -44,7 +44,7 @@ class UiCard {
|
||||
Map<String, dynamic> toJson() => _$UiCardToJson(this);
|
||||
}
|
||||
|
||||
/// 卡片操作按钮
|
||||
/// Action button metadata for a UI card.
|
||||
@JsonSerializable()
|
||||
class CardAction {
|
||||
final String type;
|
||||
@@ -65,7 +65,7 @@ class CardAction {
|
||||
Map<String, dynamic> toJson() => _$CardActionToJson(this);
|
||||
}
|
||||
|
||||
/// 日历卡片数据
|
||||
/// Calendar card payload data.
|
||||
@JsonSerializable()
|
||||
class CalendarCardData {
|
||||
final String id;
|
||||
|
||||
@@ -5,7 +5,7 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import 'package:social_app/core/network/i_api_client.dart';
|
||||
|
||||
import '../models/ag_ui_event.dart';
|
||||
|
||||
@@ -155,8 +155,8 @@ class AgUiService {
|
||||
}
|
||||
|
||||
bool hasEarlierHistory(DateTime fromDate) {
|
||||
// 历史是否还有更多由后端 history snapshot 的 hasMore 驱动。
|
||||
// 参数保留是为了兼容 ChatBloc 现有调用签名。
|
||||
// Whether earlier history exists is driven by backend snapshot.hasMore.
|
||||
// Keep the parameter for compatibility with the current ChatBloc signature.
|
||||
final _ = fromDate;
|
||||
return _hasMoreHistory;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import '../../../../core/l10n/l10n.dart';
|
||||
import '../../data/models/ag_ui_event.dart';
|
||||
|
||||
String agUiEventLabel(AgUiEventType type) {
|
||||
final l10n = L10n.current;
|
||||
return switch (type) {
|
||||
AgUiEventType.runStarted => l10n.agUiEventRunStarted,
|
||||
AgUiEventType.runFinished => l10n.agUiEventRunFinished,
|
||||
AgUiEventType.runError => l10n.agUiEventRunError,
|
||||
AgUiEventType.stepStarted => l10n.agUiEventStepStarted,
|
||||
AgUiEventType.stepFinished => l10n.agUiEventStepFinished,
|
||||
AgUiEventType.textMessageEnd => l10n.agUiEventTextMessageEnd,
|
||||
AgUiEventType.toolCallStart => l10n.agUiEventToolCallStart,
|
||||
AgUiEventType.toolCallArgs => l10n.agUiEventToolCallArgs,
|
||||
AgUiEventType.toolCallEnd => l10n.agUiEventToolCallEnd,
|
||||
AgUiEventType.toolCallResult => l10n.agUiEventToolCallResult,
|
||||
AgUiEventType.toolCallError => l10n.agUiEventToolCallError,
|
||||
AgUiEventType.unknown => l10n.agUiEventUnknown,
|
||||
};
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import '../../../../core/l10n/l10n.dart';
|
||||
|
||||
enum AgentStage { routing, execution, memory }
|
||||
|
||||
AgentStage? stageFromStepName(String value) {
|
||||
@@ -15,9 +17,9 @@ AgentStage? stageFromStepName(String value) {
|
||||
|
||||
String stageLabel(AgentStage? stage) {
|
||||
return switch (stage) {
|
||||
AgentStage.routing => '意图识别中',
|
||||
AgentStage.execution => '任务执行中',
|
||||
AgentStage.memory => '记忆提取中',
|
||||
null => '任务处理中',
|
||||
AgentStage.routing => L10n.current.agentStageRouting,
|
||||
AgentStage.execution => L10n.current.agentStageExecution,
|
||||
AgentStage.memory => L10n.current.agentStageMemory,
|
||||
null => L10n.current.agentStageProcessing,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:social_app/core/api/i_api_client.dart';
|
||||
import 'package:social_app/core/network/i_api_client.dart';
|
||||
import 'package:social_app/core/l10n/l10n.dart';
|
||||
|
||||
import '../../data/models/ag_ui_event.dart';
|
||||
import '../../data/models/chat_list_item.dart';
|
||||
@@ -130,7 +131,9 @@ class ChatBloc extends Cubit<ChatState> {
|
||||
).copyWith(
|
||||
items: _markActiveToolCallsFailed(
|
||||
state.items,
|
||||
reason: isCanceledByUser ? '本次运行已取消' : '本次运行已失败',
|
||||
reason: isCanceledByUser
|
||||
? L10n.current.chatRunCanceled
|
||||
: L10n.current.chatRunFailed,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -428,7 +431,9 @@ class ChatBloc extends Cubit<ChatState> {
|
||||
isCancelling: false,
|
||||
currentStage: null,
|
||||
error: sseClosedBeforeTerminal
|
||||
? (recoveredFromHistory ? null : '连接中断,请重试')
|
||||
? (recoveredFromHistory
|
||||
? null
|
||||
: L10n.current.chatSseInterruptedRetry)
|
||||
: error.toString(),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
bool isValidInternalNavigationPath(String path) {
|
||||
if (path.isEmpty || !path.startsWith('/')) {
|
||||
return false;
|
||||
}
|
||||
return !path.startsWith('//') &&
|
||||
!path.contains('://') &&
|
||||
!path.contains('?') &&
|
||||
!path.contains('#') &&
|
||||
!path.contains(':');
|
||||
}
|
||||
|
||||
String buildUiSchemaNavigationTarget({
|
||||
required String path,
|
||||
Map<String, dynamic>? params,
|
||||
}) {
|
||||
final baseUri = Uri.parse(path);
|
||||
final queryParams = <String, String>{};
|
||||
|
||||
if (params != null) {
|
||||
for (final entry in params.entries) {
|
||||
final value = entry.value;
|
||||
if (value is String && value.isNotEmpty) {
|
||||
queryParams[entry.key] = value;
|
||||
} else if (value is num || value is bool) {
|
||||
queryParams[entry.key] = value.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final mergedQueryParams = {...baseUri.queryParameters, ...queryParams};
|
||||
final targetUri = baseUri.replace(
|
||||
queryParameters: mergedQueryParams.isEmpty ? null : mergedQueryParams,
|
||||
);
|
||||
return targetUri.toString();
|
||||
}
|
||||
@@ -1,462 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
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';
|
||||
import '../navigation/ui_schema_navigation.dart';
|
||||
|
||||
class UiSchemaRenderer {
|
||||
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 _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'),
|
||||
};
|
||||
}
|
||||
|
||||
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: _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);
|
||||
return _wrapSurface(
|
||||
node,
|
||||
GridView.count(
|
||||
crossAxisCount: columns,
|
||||
crossAxisSpacing: gap,
|
||||
mainAxisSpacing: gap,
|
||||
childAspectRatio: 1.6,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
children: children,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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: 18,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: AppColors.slate900,
|
||||
height: 1.2,
|
||||
),
|
||||
'subtitle' => const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: AppColors.slate800,
|
||||
),
|
||||
'caption' => const TextStyle(
|
||||
fontSize: 11,
|
||||
color: AppColors.slate500,
|
||||
height: 1.4,
|
||||
),
|
||||
'code' => const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.slate700,
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
_ => const TextStyle(
|
||||
fontSize: 13,
|
||||
color: AppColors.slate700,
|
||||
height: 1.35,
|
||||
),
|
||||
};
|
||||
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: 18));
|
||||
}
|
||||
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: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.md,
|
||||
vertical: AppSpacing.xs,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: bg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
border: Border.all(color: _statusBorder(status)),
|
||||
),
|
||||
child: Text(
|
||||
_asString(node['label']),
|
||||
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w700, color: fg),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
: () {
|
||||
_handleAction(context, action);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
elevation: 0,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.lg,
|
||||
vertical: AppSpacing.sm,
|
||||
),
|
||||
backgroundColor: style == 'primary'
|
||||
? AppColors.authPrimaryButton
|
||||
: AppColors.surfaceInfoLight,
|
||||
foregroundColor: style == 'primary'
|
||||
? AppColors.white
|
||||
: AppColors.slate700,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
side: style == 'primary'
|
||||
? BorderSide.none
|
||||
: const BorderSide(color: AppColors.borderTertiary),
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
_asString(node['label'], fallback: '操作'),
|
||||
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w600),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
static void _handleAction(
|
||||
BuildContext context,
|
||||
Map<String, dynamic>? action,
|
||||
) {
|
||||
final actionType = _asString(action?['type']);
|
||||
switch (actionType) {
|
||||
case 'copy':
|
||||
Toast.show(context, '已复制', type: ToastType.success);
|
||||
return;
|
||||
case 'navigation':
|
||||
_handleNavigationAction(context, action);
|
||||
return;
|
||||
default:
|
||||
Toast.show(context, '该操作暂未接入', type: ToastType.info);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void _handleNavigationAction(
|
||||
BuildContext context,
|
||||
Map<String, dynamic>? action,
|
||||
) {
|
||||
if (action == null) {
|
||||
Toast.show(context, '导航参数无效', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
|
||||
final path = _asString(action['path']).trim();
|
||||
if (!isValidInternalNavigationPath(path)) {
|
||||
Toast.show(context, '导航路径无效', type: ToastType.warning);
|
||||
return;
|
||||
}
|
||||
|
||||
final params = _asMap(action['params']);
|
||||
final shouldReplace = action['replace'] == true;
|
||||
try {
|
||||
final target = buildUiSchemaNavigationTarget(path: path, params: params);
|
||||
if (shouldReplace) {
|
||||
context.replace(target);
|
||||
return;
|
||||
}
|
||||
context.push(target);
|
||||
} on FormatException {
|
||||
Toast.show(context, '导航路径无效', type: ToastType.warning);
|
||||
}
|
||||
}
|
||||
|
||||
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 Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.md,
|
||||
vertical: AppSpacing.xs,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceSecondary,
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 3,
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 11,
|
||||
color: AppColors.slate500,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
Expanded(
|
||||
flex: 5,
|
||||
child: Text(
|
||||
value,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.slate800,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
AppSpacing.xs,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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.surfaceSecondary,
|
||||
'card' => AppColors.white,
|
||||
_ => _statusBackground(status),
|
||||
};
|
||||
final borderColor = switch (status) {
|
||||
'success' => AppColors.feedbackSuccessBorder,
|
||||
'warning' => AppColors.feedbackWarningBorder,
|
||||
'error' => AppColors.feedbackErrorBorder,
|
||||
_ => AppColors.homeConversationBorder,
|
||||
};
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(AppSpacing.md),
|
||||
decoration: BoxDecoration(
|
||||
color: bg,
|
||||
borderRadius: BorderRadius.circular(AppRadius.lg),
|
||||
border: Border.all(color: borderColor),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: AppColors.slate200.withValues(alpha: 0.35),
|
||||
blurRadius: 12,
|
||||
offset: const Offset(0, 6),
|
||||
),
|
||||
],
|
||||
),
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static List<Widget> _withGap(List<Widget> widgets, double gap) {
|
||||
if (widgets.isEmpty) return const [];
|
||||
return [
|
||||
widgets.first,
|
||||
for (int i = 1; i < widgets.length; i++) ...[
|
||||
SizedBox(height: gap),
|
||||
widgets[i],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
static Color _statusBackground(String status) {
|
||||
return switch (status) {
|
||||
'success' => AppColors.feedbackSuccessSurface,
|
||||
'warning' => AppColors.feedbackWarningSurface,
|
||||
'error' => AppColors.feedbackErrorSurface,
|
||||
'pending' => AppColors.feedbackInfoSurface,
|
||||
_ => AppColors.surfaceSecondary,
|
||||
};
|
||||
}
|
||||
|
||||
static Color _statusBorder(String status) {
|
||||
return switch (status) {
|
||||
'success' => AppColors.feedbackSuccessBorder,
|
||||
'warning' => AppColors.feedbackWarningBorder,
|
||||
'error' => AppColors.feedbackErrorBorder,
|
||||
'pending' => AppColors.feedbackInfoBorder,
|
||||
_ => AppColors.borderTertiary,
|
||||
};
|
||||
}
|
||||
|
||||
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) {
|
||||
return Map<String, dynamic>.from(value);
|
||||
}
|
||||
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