Files
Navius/frontend/lib/features/home/domain/models/link_section.dart
CNWei e2cc3ac30f feat: 进行功能分区方便后期维护
- 添加导航条目和分类数据模型
- 实现导航卡片网格展示
- 支持点击卡片打开链接
- 添加主题样式(渐变色、卡片阴影)
- 区分系统默认和自定义条目
- 添加分类统计信息"
2026-09-13 22:49:02 +08:00

76 lines
1.9 KiB
Dart

import 'link_item.dart';
/// 一个分组,对应 `index.html` 里带标题的一组卡片。
class LinkSection {
const LinkSection({
required this.id,
required this.title,
required this.items,
});
/// 从 JSON 构造。
///
/// `items` 做了类型防御:字段缺失或类型不对时退化成空列表,
/// 避免后端返回异常数据时整个页面直接崩掉。
factory LinkSection.fromJson(Map<String, dynamic> json) {
final Object? rawItems = json['items'];
final List<LinkItem> items = rawItems is List
? rawItems
.whereType<Map<String, dynamic>>()
.map(LinkItem.fromJson)
.toList(growable: false)
: const <LinkItem>[];
return LinkSection(
id: json['id'] as String,
title: json['title'] as String,
items: items,
);
}
/// 唯一标识,同时用作锚点。
final String id;
/// 分组标题。
final String title;
/// 分组下的站点。
final List<LinkItem> items;
/// 站点数量。
int get itemCount => items.length;
/// 序列化为 JSON。
Map<String, dynamic> toJson() => <String, dynamic>{
'id': id,
'title': title,
'items': items.map((LinkItem item) => item.toJson()).toList(growable: false),
};
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other is! LinkSection ||
other.id != id ||
other.title != title ||
other.items.length != items.length) {
return false;
}
for (int index = 0; index < items.length; index++) {
if (items[index] != other.items[index]) {
return false;
}
}
return true;
}
@override
int get hashCode =>
Object.hash(id, title, Object.hashAll(items));
@override
String toString() => 'LinkSection(id: $id, title: $title, items: $itemCount)';
}