2026-02-28 13:38:58 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2026-03-30 09:06:10 +08:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2026-02-28 13:38:58 +08:00
|
|
|
|
2026-03-27 14:05:03 +08:00
|
|
|
import 'package:social_app/core/l10n/l10n.dart';
|
2026-03-30 09:06:10 +08:00
|
|
|
import 'package:social_app/core/ui_schema/navigation/ui_schema_navigation.dart';
|
2026-03-16 16:11:28 +08:00
|
|
|
import 'package:social_app/core/theme/design_tokens.dart';
|
2026-03-30 09:06:10 +08:00
|
|
|
import 'package:social_app/shared/widgets/toast/toast.dart';
|
|
|
|
|
import 'package:social_app/shared/widgets/toast/toast_type.dart';
|
|
|
|
|
|
|
|
|
|
const _titleFontSize = AppSpacing.lg + 1;
|
|
|
|
|
const _bodyFontSize = AppSpacing.md;
|
|
|
|
|
const _captionFontSize = AppSpacing.sm + AppSpacing.xs / 2;
|
|
|
|
|
const _codeFontSize = AppSpacing.sm + AppSpacing.xs;
|
|
|
|
|
const _buttonFontSize = AppSpacing.sm + AppSpacing.xs;
|
|
|
|
|
const _statusLabelTokenPrefix = 'ui.status.';
|
2026-02-28 14:41:21 +08:00
|
|
|
|
2026-02-28 13:38:58 +08:00
|
|
|
class UiSchemaRenderer {
|
2026-03-30 09:06:10 +08:00
|
|
|
final BuildContext context;
|
2026-03-27 19:07:39 +08:00
|
|
|
final ColorScheme colorScheme;
|
|
|
|
|
|
2026-03-30 09:06:10 +08:00
|
|
|
UiSchemaRenderer(this.context, this.colorScheme);
|
2026-03-27 19:07:39 +08:00
|
|
|
|
|
|
|
|
Widget renderSchema(Map<String, dynamic>? schema) {
|
2026-03-16 16:11:28 +08:00
|
|
|
if (schema == null || schema.isEmpty) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
final root = _asMap(schema['root']);
|
|
|
|
|
if (root == null) {
|
2026-03-27 14:05:03 +08:00
|
|
|
return _fallback(L10n.current.uiSchemaInvalid);
|
2026-03-16 16:11:28 +08:00
|
|
|
}
|
|
|
|
|
return _renderLayoutNode(root);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderLayoutNode(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final type = _asString(node['type']);
|
|
|
|
|
return switch (type) {
|
|
|
|
|
'stack' => _renderStack(node),
|
|
|
|
|
'grid' => _renderGrid(node),
|
2026-03-27 14:05:03 +08:00
|
|
|
_ => _fallback(L10n.current.uiSchemaUnsupportedLayout(type)),
|
2026-03-16 16:11:28 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderNode(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
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),
|
2026-03-27 14:05:03 +08:00
|
|
|
_ => _fallback(L10n.current.uiSchemaUnknownNode(type)),
|
2026-02-28 14:41:21 +08:00
|
|
|
};
|
2026-02-28 13:38:58 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderStack(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
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');
|
2026-02-28 13:38:58 +08:00
|
|
|
|
2026-03-16 16:11:28 +08:00
|
|
|
Widget content;
|
|
|
|
|
if (direction == 'horizontal') {
|
|
|
|
|
content = Wrap(
|
|
|
|
|
direction: Axis.horizontal,
|
|
|
|
|
spacing: gap,
|
|
|
|
|
runSpacing: gap,
|
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
|
children: children,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
content = Column(
|
2026-02-28 13:38:58 +08:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2026-03-16 16:11:28 +08:00
|
|
|
children: _withGap(children, gap),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return _wrapSurface(node, content);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderGrid(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
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,
|
2026-03-18 17:03:22 +08:00
|
|
|
children: children,
|
2026-02-28 13:38:58 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderText(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final role = _asString(node['role'], fallback: 'body');
|
|
|
|
|
final status = _asString(node['status']);
|
|
|
|
|
final style = switch (role) {
|
2026-03-27 19:07:39 +08:00
|
|
|
'title' => TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: _titleFontSize,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.onSurface,
|
2026-03-30 09:06:10 +08:00
|
|
|
height: 1.25,
|
2026-02-28 14:41:21 +08:00
|
|
|
),
|
2026-03-27 19:07:39 +08:00
|
|
|
'subtitle' => TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: AppSpacing.md,
|
2026-03-16 16:11:28 +08:00
|
|
|
fontWeight: FontWeight.w600,
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.onSurface,
|
2026-02-28 14:41:21 +08:00
|
|
|
),
|
2026-03-27 19:07:39 +08:00
|
|
|
'caption' => TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: _captionFontSize,
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.onSurfaceVariant,
|
2026-03-16 18:32:09 +08:00
|
|
|
height: 1.4,
|
|
|
|
|
),
|
2026-03-27 19:07:39 +08:00
|
|
|
'code' => TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: _codeFontSize,
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.onSurfaceVariant,
|
2026-03-16 16:11:28 +08:00
|
|
|
fontFamily: 'monospace',
|
|
|
|
|
),
|
2026-03-27 19:07:39 +08:00
|
|
|
_ => TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: _bodyFontSize,
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.onSurfaceVariant,
|
2026-03-30 09:06:10 +08:00
|
|
|
height: 1.4,
|
2026-02-28 14:41:21 +08:00
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
};
|
|
|
|
|
return Text(
|
|
|
|
|
_asString(node['content']),
|
|
|
|
|
maxLines: _asIntOrNull(node['maxLines']),
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: style.copyWith(color: _statusTextColor(status, style.color)),
|
2026-02-28 14:41:21 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderIcon(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final value = _asString(node['value']);
|
|
|
|
|
if (_asString(node['source']) == 'emoji' && value.isNotEmpty) {
|
2026-03-17 00:13:41 +08:00
|
|
|
return Text(value, style: const TextStyle(fontSize: 18));
|
2026-03-16 16:11:28 +08:00
|
|
|
}
|
|
|
|
|
return Icon(Icons.bubble_chart_rounded, color: _statusTextColor('', null));
|
2026-02-28 14:41:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderBadge(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final status = _asString(node['status']);
|
2026-03-30 09:06:10 +08:00
|
|
|
final resolvedLabel = _resolveStatusLabel(
|
|
|
|
|
rawLabel: _asString(node['label']),
|
|
|
|
|
status: status,
|
|
|
|
|
);
|
|
|
|
|
final statusDotColor = _statusBorder(status);
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.only(left: AppSpacing.xs / 2),
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
width: AppSpacing.xs,
|
|
|
|
|
height: AppSpacing.xs,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: statusDotColor,
|
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: AppSpacing.xs),
|
|
|
|
|
Text(
|
|
|
|
|
resolvedLabel,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: _captionFontSize,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
color: colorScheme.onSurfaceVariant,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
2026-03-16 16:11:28 +08:00
|
|
|
),
|
2026-02-28 14:41:21 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderButton(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final style = _asString(node['style'], fallback: 'secondary');
|
|
|
|
|
final action = _asMap(node['action']);
|
|
|
|
|
final disabled = node['disabled'] == true;
|
2026-03-30 09:06:10 +08:00
|
|
|
final isPrimary = style == 'primary';
|
|
|
|
|
final isGhost = style == 'ghost';
|
|
|
|
|
final isDanger = style == 'danger';
|
|
|
|
|
final backgroundColor = isPrimary
|
|
|
|
|
? colorScheme.primaryContainer
|
|
|
|
|
: isGhost
|
|
|
|
|
? colorScheme.surface
|
|
|
|
|
: isDanger
|
|
|
|
|
? colorScheme.errorContainer
|
|
|
|
|
: colorScheme.surfaceContainerLow;
|
|
|
|
|
final foregroundColor = isPrimary
|
|
|
|
|
? colorScheme.onPrimaryContainer
|
|
|
|
|
: isDanger
|
|
|
|
|
? colorScheme.onErrorContainer
|
|
|
|
|
: isGhost
|
|
|
|
|
? colorScheme.onSurfaceVariant
|
|
|
|
|
: colorScheme.onSurfaceVariant;
|
|
|
|
|
final borderColor = isPrimary
|
|
|
|
|
? colorScheme.primary.withValues(alpha: 0.3)
|
|
|
|
|
: isGhost
|
|
|
|
|
? colorScheme.outlineVariant.withValues(alpha: 0.4)
|
|
|
|
|
: isDanger
|
|
|
|
|
? colorScheme.error.withValues(alpha: 0.35)
|
|
|
|
|
: colorScheme.outlineVariant.withValues(alpha: 0.4);
|
2026-03-27 19:07:39 +08:00
|
|
|
return ElevatedButton(
|
|
|
|
|
onPressed: disabled
|
|
|
|
|
? null
|
2026-03-30 09:06:10 +08:00
|
|
|
: () async {
|
|
|
|
|
await _handleAction(action);
|
2026-03-27 19:07:39 +08:00
|
|
|
},
|
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
|
elevation: 0,
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
2026-03-30 09:06:10 +08:00
|
|
|
horizontal: AppSpacing.md + AppSpacing.xs,
|
2026-03-27 19:07:39 +08:00
|
|
|
vertical: AppSpacing.sm,
|
|
|
|
|
),
|
2026-03-30 09:06:10 +08:00
|
|
|
minimumSize: const Size(0, AppSpacing.xxl + AppSpacing.xs),
|
|
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
|
foregroundColor: foregroundColor,
|
2026-03-27 19:07:39 +08:00
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppRadius.full),
|
2026-03-30 09:06:10 +08:00
|
|
|
side: BorderSide(color: borderColor),
|
2026-03-27 19:07:39 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
_asString(node['label'], fallback: L10n.current.uiSchemaActionFallback),
|
2026-03-30 09:06:10 +08:00
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: _buttonFontSize,
|
|
|
|
|
fontWeight: isPrimary ? FontWeight.w700 : FontWeight.w600,
|
|
|
|
|
),
|
2026-03-27 19:07:39 +08:00
|
|
|
),
|
2026-02-28 13:38:58 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 09:06:10 +08:00
|
|
|
Future<void> _handleAction(Map<String, dynamic>? action) async {
|
|
|
|
|
if (action == null || action.isEmpty) {
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaActionNotImplemented,
|
|
|
|
|
type: ToastType.warning,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final type = _asString(action['type']);
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'navigation':
|
|
|
|
|
_handleNavigationAction(action);
|
|
|
|
|
return;
|
|
|
|
|
case 'url':
|
|
|
|
|
await _handleUrlAction(action);
|
|
|
|
|
return;
|
|
|
|
|
case 'copy':
|
|
|
|
|
_handleCopyAction(action);
|
|
|
|
|
return;
|
|
|
|
|
case 'event':
|
|
|
|
|
case 'tool':
|
|
|
|
|
case 'payload':
|
|
|
|
|
default:
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaActionNotImplemented,
|
|
|
|
|
type: ToastType.info,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _handleUrlAction(Map<String, dynamic> action) async {
|
|
|
|
|
final rawUrl = _asString(action['url']).trim();
|
|
|
|
|
if (!_isValidExternalUrl(rawUrl)) {
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaUrlInvalid,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final uri = Uri.tryParse(rawUrl);
|
|
|
|
|
if (uri == null) {
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaUrlInvalid,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final target = _asString(action['target'], fallback: '_blank');
|
|
|
|
|
final mode = target == '_self'
|
|
|
|
|
? LaunchMode.platformDefault
|
|
|
|
|
: LaunchMode.externalApplication;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final launched = await launchUrl(uri, mode: mode);
|
|
|
|
|
if (!context.mounted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!launched) {
|
|
|
|
|
debugPrint('UiSchemaRenderer: failed to launch URL: $rawUrl');
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaUrlOpenFailed,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
debugPrint('UiSchemaRenderer: URL launch error for $rawUrl: $error');
|
|
|
|
|
if (!context.mounted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaUrlOpenFailed,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleNavigationAction(Map<String, dynamic> action) {
|
|
|
|
|
final path = _asString(action['path']).trim();
|
|
|
|
|
if (!isValidInternalNavigationPath(path)) {
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaNavigationInvalidPath,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final params = _asMap(action['params']);
|
|
|
|
|
if (params != null && !_areScalarNavigationParams(params)) {
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaNavigationInvalidParams,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final target = buildUiSchemaNavigationTarget(path: path, params: params);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
context.push(target);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
debugPrint('UiSchemaRenderer: navigation failed for $target: $error');
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaNavigationInvalidPath,
|
|
|
|
|
type: ToastType.error,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleCopyAction(Map<String, dynamic> action) {
|
|
|
|
|
final content = _asString(action['content']);
|
|
|
|
|
if (content.isEmpty) {
|
|
|
|
|
Toast.show(
|
|
|
|
|
context,
|
|
|
|
|
L10n.current.uiSchemaActionNotImplemented,
|
|
|
|
|
type: ToastType.warning,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Clipboard.setData(ClipboardData(text: content));
|
|
|
|
|
final successMessage = _asString(
|
|
|
|
|
action['successMessage'],
|
|
|
|
|
fallback: L10n.current.commonCopySuccess,
|
|
|
|
|
);
|
|
|
|
|
Toast.show(context, successMessage, type: ToastType.success);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool _isValidExternalUrl(String url) {
|
|
|
|
|
if (url.isEmpty) return false;
|
|
|
|
|
final uri = Uri.tryParse(url);
|
|
|
|
|
if (uri == null || !uri.hasScheme) return false;
|
|
|
|
|
final scheme = uri.scheme.toLowerCase();
|
|
|
|
|
if (scheme == 'http' || scheme == 'https') {
|
|
|
|
|
if (uri.host.isEmpty || uri.userInfo.isNotEmpty) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (_isLocalOrPrivateHost(uri.host)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return scheme == 'mailto' || scheme == 'tel' || scheme == 'sms';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool _areScalarNavigationParams(Map<String, dynamic> params) {
|
|
|
|
|
for (final value in params.values) {
|
|
|
|
|
if (value is String || value is num || value is bool) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool _isLocalOrPrivateHost(String host) {
|
|
|
|
|
final normalizedHost = host.toLowerCase();
|
|
|
|
|
if (normalizedHost.contains(':')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (int.tryParse(normalizedHost) != null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (normalizedHost == 'localhost' ||
|
|
|
|
|
normalizedHost == '127.0.0.1' ||
|
|
|
|
|
normalizedHost == '::1') {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final ipv4 = normalizedHost.split('.');
|
|
|
|
|
if (ipv4.length != 4) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
final octets = <int>[];
|
|
|
|
|
for (final part in ipv4) {
|
|
|
|
|
final parsed = int.tryParse(part);
|
|
|
|
|
if (parsed == null || parsed < 0 || parsed > 255) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
octets.add(parsed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final first = octets[0];
|
|
|
|
|
final second = octets[1];
|
|
|
|
|
if (first == 10 || first == 127 || first == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (first == 169 && second == 254) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (first == 192 && second == 168) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (first == 172 && second >= 16 && second <= 31) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-18 13:35:25 +08:00
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderKv(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
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() ?? '-';
|
2026-03-16 18:32:09 +08:00
|
|
|
return Container(
|
|
|
|
|
width: double.infinity,
|
2026-03-30 09:06:10 +08:00
|
|
|
padding: const EdgeInsets.symmetric(vertical: AppSpacing.xs),
|
2026-03-16 18:32:09 +08:00
|
|
|
decoration: BoxDecoration(
|
2026-03-30 09:06:10 +08:00
|
|
|
border: Border(
|
|
|
|
|
bottom: BorderSide(
|
|
|
|
|
color: colorScheme.outlineVariant.withValues(alpha: 0.2),
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-03-16 18:32:09 +08:00
|
|
|
),
|
|
|
|
|
child: Row(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
flex: 3,
|
|
|
|
|
child: Text(
|
|
|
|
|
label,
|
2026-03-27 19:07:39 +08:00
|
|
|
style: TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: _captionFontSize,
|
|
|
|
|
color: colorScheme.onSurfaceVariant.withValues(
|
|
|
|
|
alpha: 0.72,
|
|
|
|
|
),
|
2026-03-16 18:32:09 +08:00
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
),
|
2026-03-09 00:10:09 +08:00
|
|
|
),
|
2026-03-16 18:32:09 +08:00
|
|
|
const SizedBox(width: AppSpacing.sm),
|
|
|
|
|
Expanded(
|
|
|
|
|
flex: 5,
|
|
|
|
|
child: Text(
|
|
|
|
|
value,
|
2026-03-27 19:07:39 +08:00
|
|
|
style: TextStyle(
|
2026-03-30 09:06:10 +08:00
|
|
|
fontSize: _bodyFontSize,
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.onSurface,
|
2026-03-30 09:06:10 +08:00
|
|
|
fontWeight: FontWeight.w400,
|
2026-03-16 18:32:09 +08:00
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
),
|
|
|
|
|
),
|
2026-03-16 18:32:09 +08:00
|
|
|
],
|
|
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
AppSpacing.xs,
|
2026-03-09 00:10:09 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _renderDivider(Map<String, dynamic> node) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final inset = _asDouble(node['inset'], fallback: 0);
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: inset),
|
2026-03-27 19:07:39 +08:00
|
|
|
child: Divider(height: 1, color: colorScheme.outlineVariant),
|
2026-03-09 00:10:09 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _wrapSurface(Map<String, dynamic> node, Widget child) {
|
2026-03-16 16:11:28 +08:00
|
|
|
final appearance = _asString(node['appearance'], fallback: 'plain');
|
|
|
|
|
final status = _asString(node['status']);
|
|
|
|
|
if (appearance == 'plain') {
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
final bg = switch (appearance) {
|
2026-03-30 09:06:10 +08:00
|
|
|
'section' => colorScheme.surfaceContainerLow,
|
|
|
|
|
'card' =>
|
|
|
|
|
status == 'success'
|
|
|
|
|
? colorScheme.primaryContainer.withValues(alpha: 0.05)
|
|
|
|
|
: colorScheme.surfaceContainerLow.withValues(alpha: 0.5),
|
2026-03-16 16:11:28 +08:00
|
|
|
_ => _statusBackground(status),
|
|
|
|
|
};
|
|
|
|
|
final borderColor = switch (status) {
|
2026-03-30 09:06:10 +08:00
|
|
|
'success' => colorScheme.primary.withValues(alpha: 0.1),
|
|
|
|
|
'warning' => colorScheme.outlineVariant,
|
|
|
|
|
'error' => colorScheme.error.withValues(alpha: 0.22),
|
2026-03-27 19:07:39 +08:00
|
|
|
_ => colorScheme.outlineVariant,
|
2026-03-16 16:11:28 +08:00
|
|
|
};
|
2026-02-28 13:38:58 +08:00
|
|
|
return Container(
|
2026-03-16 16:11:28 +08:00
|
|
|
width: double.infinity,
|
2026-03-17 00:13:41 +08:00
|
|
|
padding: const EdgeInsets.all(AppSpacing.md),
|
2026-02-28 13:38:58 +08:00
|
|
|
decoration: BoxDecoration(
|
2026-03-16 16:11:28 +08:00
|
|
|
color: bg,
|
2026-03-30 09:06:10 +08:00
|
|
|
borderRadius: BorderRadius.circular(AppRadius.xl),
|
|
|
|
|
border: Border.all(color: borderColor.withValues(alpha: 0.4)),
|
2026-02-28 13:38:58 +08:00
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
child: child,
|
2026-02-28 13:38:58 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Widget _fallback(String text) {
|
2026-02-28 13:38:58 +08:00
|
|
|
return Container(
|
2026-03-16 16:11:28 +08:00
|
|
|
padding: const EdgeInsets.all(AppSpacing.md),
|
2026-02-28 13:38:58 +08:00
|
|
|
decoration: BoxDecoration(
|
2026-03-27 19:07:39 +08:00
|
|
|
color: colorScheme.secondaryContainer,
|
|
|
|
|
border: Border.all(color: colorScheme.secondary),
|
2026-03-16 16:11:28 +08:00
|
|
|
borderRadius: BorderRadius.circular(AppRadius.md),
|
2026-02-28 13:38:58 +08:00
|
|
|
),
|
2026-03-16 16:11:28 +08:00
|
|
|
child: Text(
|
|
|
|
|
text,
|
2026-03-30 09:06:10 +08:00
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: _buttonFontSize,
|
|
|
|
|
color: colorScheme.onSecondaryContainer,
|
|
|
|
|
),
|
2026-02-28 13:38:58 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
List<Widget> _withGap(List<Widget> widgets, double gap) {
|
2026-03-19 00:51:57 +08:00
|
|
|
if (widgets.isEmpty) return const [];
|
|
|
|
|
return [
|
|
|
|
|
widgets.first,
|
|
|
|
|
for (int i = 1; i < widgets.length; i++) ...[
|
|
|
|
|
SizedBox(height: gap),
|
|
|
|
|
widgets[i],
|
|
|
|
|
],
|
|
|
|
|
];
|
2026-03-16 16:11:28 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Color _statusBackground(String status) {
|
2026-03-16 16:11:28 +08:00
|
|
|
return switch (status) {
|
2026-03-30 09:06:10 +08:00
|
|
|
'success' => colorScheme.primaryContainer.withValues(alpha: 0.3),
|
2026-03-27 19:07:39 +08:00
|
|
|
'warning' => colorScheme.secondaryContainer,
|
|
|
|
|
'error' => colorScheme.errorContainer,
|
|
|
|
|
'pending' => colorScheme.primaryContainer,
|
|
|
|
|
_ => colorScheme.surfaceContainerHighest,
|
2026-03-16 18:32:09 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Color _statusBorder(String status) {
|
2026-03-16 18:32:09 +08:00
|
|
|
return switch (status) {
|
2026-03-30 09:06:10 +08:00
|
|
|
'success' => colorScheme.primary,
|
2026-03-27 19:07:39 +08:00
|
|
|
'warning' => colorScheme.secondary,
|
|
|
|
|
'error' => colorScheme.error,
|
|
|
|
|
'pending' => colorScheme.primary,
|
|
|
|
|
_ => colorScheme.outlineVariant,
|
2026-03-16 16:11:28 +08:00
|
|
|
};
|
|
|
|
|
}
|
2026-02-28 13:38:58 +08:00
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
Color? _statusTextColor(String status, Color? fallback) {
|
2026-03-16 16:11:28 +08:00
|
|
|
return switch (status) {
|
2026-03-30 09:06:10 +08:00
|
|
|
'success' => colorScheme.onSurfaceVariant,
|
2026-03-27 19:07:39 +08:00
|
|
|
'warning' => colorScheme.onSecondaryContainer,
|
|
|
|
|
'error' => colorScheme.onErrorContainer,
|
|
|
|
|
'pending' => colorScheme.onPrimaryContainer,
|
2026-03-16 16:11:28 +08:00
|
|
|
_ => fallback,
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-02-28 13:38:58 +08:00
|
|
|
|
2026-03-30 09:06:10 +08:00
|
|
|
String _resolveStatusLabel({
|
|
|
|
|
required String rawLabel,
|
|
|
|
|
required String status,
|
|
|
|
|
}) {
|
|
|
|
|
final normalizedStatus = status.trim().toLowerCase();
|
|
|
|
|
final normalizedLabel = rawLabel.trim().toLowerCase();
|
|
|
|
|
final bareLabel = normalizedLabel.startsWith(_statusLabelTokenPrefix)
|
|
|
|
|
? normalizedLabel.substring(_statusLabelTokenPrefix.length)
|
|
|
|
|
: normalizedLabel;
|
|
|
|
|
|
|
|
|
|
final isTokenLabel = normalizedLabel.startsWith(_statusLabelTokenPrefix);
|
|
|
|
|
final isLegacyStatusLabel = bareLabel == normalizedStatus;
|
|
|
|
|
|
|
|
|
|
if (isTokenLabel || isLegacyStatusLabel) {
|
|
|
|
|
return _tryLocalizedStatusLabel(bareLabel) ?? rawLabel;
|
|
|
|
|
}
|
|
|
|
|
return rawLabel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String? _tryLocalizedStatusLabel(String status) {
|
|
|
|
|
final l10n = L10n.current;
|
|
|
|
|
return switch (status) {
|
|
|
|
|
'success' => l10n.uiSchemaStatusSuccess,
|
|
|
|
|
'warning' => l10n.uiSchemaStatusWarning,
|
|
|
|
|
'error' => l10n.uiSchemaStatusError,
|
|
|
|
|
'pending' => l10n.uiSchemaStatusPending,
|
|
|
|
|
'info' => l10n.uiSchemaStatusInfo,
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 16:11:28 +08:00
|
|
|
static Map<String, dynamic>? _asMap(Object? value) {
|
|
|
|
|
if (value is Map<String, dynamic>) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (value is Map) {
|
2026-03-19 00:51:57 +08:00
|
|
|
return Map<String, dynamic>.from(value);
|
2026-03-16 16:11:28 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2026-02-28 13:38:58 +08:00
|
|
|
|
2026-03-16 16:11:28 +08:00
|
|
|
static int _asInt(Object? value, {int fallback = 0}) {
|
|
|
|
|
if (value is int) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if (value is double) {
|
|
|
|
|
return value.toInt();
|
2026-02-28 13:38:58 +08:00
|
|
|
}
|
2026-03-16 16:11:28 +08:00
|
|
|
if (value is String) {
|
|
|
|
|
return int.tryParse(value) ?? fallback;
|
|
|
|
|
}
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int? _asIntOrNull(Object? value) {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return _asInt(value);
|
2026-02-28 13:38:58 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-16 16:11:28 +08:00
|
|
|
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;
|
2026-02-28 13:38:58 +08:00
|
|
|
}
|
|
|
|
|
}
|