重构记分数据模型,替换弓型和靶纸类型为 ID 及名称,更新相关页面以支持新配置。
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class BowOption {
|
||||
const BowOption({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.icon,
|
||||
});
|
||||
|
||||
final int id;
|
||||
final String name;
|
||||
final String icon;
|
||||
|
||||
factory BowOption.fromJson(Map<String, dynamic> json) {
|
||||
return BowOption(
|
||||
id: json['id'] as int,
|
||||
name: json['name'] as String,
|
||||
icon: json['icon'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class TargetOption {
|
||||
const TargetOption({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.icon,
|
||||
required this.iconPng,
|
||||
});
|
||||
|
||||
final int id;
|
||||
final String name;
|
||||
final String icon;
|
||||
final String iconPng;
|
||||
|
||||
factory TargetOption.fromJson(Map<String, dynamic> json) {
|
||||
return TargetOption(
|
||||
id: json['id'] as int,
|
||||
name: json['name'] as String,
|
||||
icon: json['icon'] as String? ?? '',
|
||||
iconPng: json['iconPng'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PointBookConfig {
|
||||
const PointBookConfig({
|
||||
required this.bowOption,
|
||||
required this.targetOption,
|
||||
});
|
||||
|
||||
static const assetPath = 'assets/config/point_book_config.json';
|
||||
|
||||
final List<BowOption> bowOption;
|
||||
final List<TargetOption> targetOption;
|
||||
|
||||
factory PointBookConfig.fromJson(Map<String, dynamic> json) {
|
||||
final bows = (json['bowOption'] as List<dynamic>? ?? const [])
|
||||
.cast<Map<String, dynamic>>()
|
||||
.map(BowOption.fromJson)
|
||||
.toList();
|
||||
final targets = (json['targetOption'] as List<dynamic>? ?? const [])
|
||||
.cast<Map<String, dynamic>>()
|
||||
.map(TargetOption.fromJson)
|
||||
.toList();
|
||||
return PointBookConfig(bowOption: bows, targetOption: targets);
|
||||
}
|
||||
|
||||
static Future<PointBookConfig> fromAsset({
|
||||
String path = assetPath,
|
||||
}) async {
|
||||
final raw = await rootBundle.loadString(path);
|
||||
return PointBookConfig.fromJson(
|
||||
jsonDecode(raw) as Map<String, dynamic>,
|
||||
);
|
||||
}
|
||||
|
||||
BowOption? bowById(int id) {
|
||||
for (final o in bowOption) {
|
||||
if (o.id == id) return o;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
TargetOption? targetById(int id) {
|
||||
for (final o in targetOption) {
|
||||
if (o.id == id) return o;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user