feat: rewrite all models in Flutter #36
No files matched your search
@@ -0,0 +1,11 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 模型通用接口,所有 Flutter 模型实现此接口。
|
||||
abstract class GcModel {
|
||||
String get id;
|
||||
String name();
|
||||
String description();
|
||||
Widget buildView(BuildContext context);
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'base.dart';
|
||||
import 'contour_model.dart';
|
||||
|
||||
/// 等高线模型 Flutter 实现
|
||||
class ContourModelFlutter implements GcModel {
|
||||
@override
|
||||
final String id = 'contour-map-flutter';
|
||||
|
||||
@override
|
||||
String name() => '等高线地形图 (Flutter)';
|
||||
|
||||
@override
|
||||
String description() => '纯 Flutter 实现的等高线地形模型,支持五大地形切换';
|
||||
|
||||
@override
|
||||
Widget buildView(BuildContext context) {
|
||||
return const _ContourModelView();
|
||||
}
|
||||
}
|
||||
|
||||
class _ContourModelView extends StatefulWidget {
|
||||
const _ContourModelView();
|
||||
|
||||
@override
|
||||
State<_ContourModelView> createState() => _ContourModelViewState();
|
||||
}
|
||||
|
||||
class _ContourModelViewState extends State<_ContourModelView>
|
||||
with TickerProviderStateMixin {
|
||||
late AnimationController _animController;
|
||||
TerrainKind _selectedTerrain = TerrainKind.mountain;
|
||||
bool _isPlaying = false;
|
||||
bool _colored = false;
|
||||
bool _isTopView = false;
|
||||
double _progress = 0;
|
||||
|
||||
final List<TerrainKind> _terrains = [
|
||||
TerrainKind.plain,
|
||||
TerrainKind.hill,
|
||||
TerrainKind.mountain,
|
||||
TerrainKind.plateau,
|
||||
TerrainKind.basin,
|
||||
];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_animController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(seconds: 8),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_animController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _togglePlay() {
|
||||
setState(() {
|
||||
_isPlaying = !_isPlaying;
|
||||
});
|
||||
if (_isPlaying) {
|
||||
_animController.repeat();
|
||||
} else {
|
||||
_animController.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void _reset() {
|
||||
setState(() {
|
||||
_isPlaying = false;
|
||||
_progress = 0;
|
||||
_colored = false;
|
||||
_isTopView = false;
|
||||
});
|
||||
_animController.stop();
|
||||
}
|
||||
|
||||
void _selectTerrain(TerrainKind terrain) {
|
||||
setState(() {
|
||||
_selectedTerrain = terrain;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
final bgColor = isDark ? const Color(0xFF0B1524) : const Color(0xFFE8F0FE);
|
||||
final panelColor = isDark ? const Color(0xFF101E31) : const Color(0xFFF5F7FA);
|
||||
final inkColor = isDark ? const Color(0xFFEAF2FF) : const Color(0xFF111A2E);
|
||||
final mutedColor = isDark ? const Color(0xFF93A8C6) : const Color(0xFF5C6A80);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
body: Column(
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'等高线地形 · 五大地形',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: inkColor,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
TextButton.icon(
|
||||
onPressed: _reset,
|
||||
icon: const Icon(Icons.refresh_rounded, size: 16),
|
||||
label: const Text('重置'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 五大地形切换
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'五大地形:',
|
||||
style: TextStyle(fontSize: 12, color: mutedColor),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
..._terrains.map((t) => _terrainChip(t, _selectedTerrain == t, () => _selectTerrain(t))),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 画布区域
|
||||
Expanded(
|
||||
child: _ContourCanvas(
|
||||
terrain: _selectedTerrain,
|
||||
isPlaying: _isPlaying,
|
||||
progress: _progress,
|
||||
colored: _colored,
|
||||
isTopView: _isTopView,
|
||||
animProgress: _animController.value,
|
||||
),
|
||||
),
|
||||
// 底部控制栏
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
_controlBtn(
|
||||
icon: _isPlaying ? Icons.pause_rounded : Icons.play_arrow_rounded,
|
||||
label: _isPlaying ? '暂停' : '播放',
|
||||
color: const Color(0xFF4D8CFF),
|
||||
onPressed: _togglePlay,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_controlBtn(
|
||||
icon: Icons.map_rounded,
|
||||
label: _isTopView ? '立体图' : '俯视图',
|
||||
color: panelColor,
|
||||
onPressed: () => setState(() => _isTopView = !_isTopView),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
_controlBtn(
|
||||
icon: Icons.palette_rounded,
|
||||
label: _colored ? '取消上色' : '上色',
|
||||
color: _colored ? const Color(0xFF4D8CFF) : panelColor,
|
||||
onPressed: () => setState(() => _colored = !_colored),
|
||||
),
|
||||
const Spacer(),
|
||||
Expanded(
|
||||
child: LinearProgressIndicator(
|
||||
value: _progress,
|
||||
minHeight: 4,
|
||||
backgroundColor: isDark ? const Color(0xFF1E3350) : const Color(0xFFE3E8F3),
|
||||
valueColor: const AlwaysStoppedAnimation<Color>(Color(0xFF4D8CFF)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _terrainChip(TerrainKind terrain, bool selected, VoidCallback onTap) {
|
||||
final names = {
|
||||
TerrainKind.plain: '平原',
|
||||
TerrainKind.hill: '丘陵',
|
||||
TerrainKind.mountain: '山地',
|
||||
TerrainKind.plateau: '高原',
|
||||
TerrainKind.basin: '盆地',
|
||||
};
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 6),
|
||||
child: FilterChip(
|
||||
label: Text(names[terrain] ?? ''),
|
||||
selected: selected,
|
||||
onSelected: (_) => onTap(),
|
||||
selectedColor: const Color(0xFF4D8CFF).withOpacity(0.2),
|
||||
checkmarkColor: const Color(0xFF4D8CFF),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _controlBtn({
|
||||
required IconData icon,
|
||||
required String label,
|
||||
required Color color,
|
||||
required VoidCallback onPressed,
|
||||
}) {
|
||||
return TextButton.icon(
|
||||
onPressed: onPressed,
|
||||
icon: Icon(icon, size: 16, color: color),
|
||||
label: Text(label, style: TextStyle(fontSize: 12, color: color)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 等高线画布
|
||||
class _ContourCanvas extends StatelessWidget {
|
||||
final TerrainKind terrain;
|
||||
final bool isPlaying;
|
||||
final double progress;
|
||||
final bool colored;
|
||||
final bool isTopView;
|
||||
final double animProgress;
|
||||
|
||||
const _ContourCanvas({
|
||||
required this.terrain,
|
||||
required this.isPlaying,
|
||||
required this.progress,
|
||||
required this.colored,
|
||||
required this.isTopView,
|
||||
required this.animProgress,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CustomPaint(
|
||||
size: Size.infinite,
|
||||
painter: _ContourPainter(
|
||||
terrain: terrain,
|
||||
isPlaying: isPlaying,
|
||||
progress: progress,
|
||||
colored: colored,
|
||||
isTopView: isTopView,
|
||||
animProgress: animProgress,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ContourPainter extends CustomPainter {
|
||||
final TerrainKind terrain;
|
||||
final bool isPlaying;
|
||||
final double progress;
|
||||
final bool colored;
|
||||
final bool isTopView;
|
||||
final double animProgress;
|
||||
|
||||
_ContourPainter({
|
||||
required this.terrain,
|
||||
required this.isPlaying,
|
||||
required this.progress,
|
||||
required this.colored,
|
||||
required this.isTopView,
|
||||
required this.animProgress,
|
||||
});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final bgColor = const Color(0xFF08111E);
|
||||
final lineColor = const Color(0xFF4D8CFF);
|
||||
final fillColor = const Color(0xFF1C5FA8);
|
||||
|
||||
canvas.drawColor(bgColor, BlendMode.src);
|
||||
|
||||
// 绘制等高线
|
||||
final center = Offset(size.width / 2, size.height / 2);
|
||||
final radius = math.min(size.width, size.height) * 0.35;
|
||||
|
||||
int contourCount;
|
||||
double maxHeight;
|
||||
|
||||
switch (terrain) {
|
||||
case TerrainKind.plain:
|
||||
contourCount = 3;
|
||||
maxHeight = 60;
|
||||
break;
|
||||
case TerrainKind.hill:
|
||||
contourCount = 8;
|
||||
maxHeight = 180;
|
||||
break;
|
||||
case TerrainKind.mountain:
|
||||
contourCount = 12;
|
||||
maxHeight = 1200;
|
||||
break;
|
||||
case TerrainKind.plateau:
|
||||
contourCount = 10;
|
||||
maxHeight = 1250;
|
||||
break;
|
||||
case TerrainKind.basin:
|
||||
contourCount = 10;
|
||||
maxHeight = 1060;
|
||||
break;
|
||||
}
|
||||
|
||||
final interval = maxHeight / contourCount;
|
||||
|
||||
for (int i = 0; i < contourCount; i++) {
|
||||
final ratio = (i + 1) / contourCount;
|
||||
final progressRatio = isPlaying ? animProgress : progress;
|
||||
|
||||
if (ratio > progressRatio) continue;
|
||||
|
||||
final alpha = colored ? (1 - ratio).clamp(0.2, 1.0) : 1.0;
|
||||
final paint = Paint()
|
||||
..color = colored
|
||||
? fillColor.withOpacity(alpha * 0.3)
|
||||
: lineColor.withOpacity(alpha)
|
||||
..style = colored ? PaintingStyle.fill : PaintingStyle.stroke
|
||||
..strokeWidth = 1.5;
|
||||
|
||||
Path path;
|
||||
if (terrain == TerrainKind.basin) {
|
||||
final r = radius * ratio;
|
||||
path = Path()..addOval(Rect.fromCircle(center: center, radius: r));
|
||||
} else if (terrain == TerrainKind.plateau) {
|
||||
final r = ratio < 0.5
|
||||
? radius * 0.5
|
||||
: radius * (0.5 + (ratio - 0.5) * 0.8);
|
||||
path = Path()..addOval(Rect.fromCircle(center: center, radius: r));
|
||||
} else {
|
||||
final spacing = terrain == TerrainKind.plain ? 0.15 : (terrain == TerrainKind.hill ? 0.1 : 0.08);
|
||||
final r = radius * (1 - ratio * (1 - spacing));
|
||||
path = Path()..addOval(Rect.fromCircle(center: center, radius: r));
|
||||
}
|
||||
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
// 绘制山峰标记
|
||||
if (!isTopView && terrain != TerrainKind.plain) {
|
||||
final peakPaint = Paint()
|
||||
..color = const Color(0xFFFFD166)
|
||||
..style = PaintingStyle.fill;
|
||||
canvas.drawCircle(center, radius * 0.08, peakPaint);
|
||||
|
||||
final textPainter = TextPainter(
|
||||
text: TextSpan(
|
||||
text: '${maxHeight.toInt()}m',
|
||||
style: const TextStyle(fontSize: 11, color: Color(0xFFEAF2FF)),
|
||||
),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(canvas, Offset(center.dx + 15, center.dy - 20));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _ContourPainter oldDelegate) {
|
||||
return oldDelegate.terrain != terrain ||
|
||||
oldDelegate.isPlaying != isPlaying ||
|
||||
oldDelegate.progress != progress ||
|
||||
oldDelegate.colored != colored ||
|
||||
oldDelegate.isTopView != isTopView ||
|
||||
oldDelegate.animProgress != animProgress;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/// 五大地形枚举(对应 C# TerrainKind)
|
||||
enum TerrainKind {
|
||||
plain, // 平原
|
||||
hill, // 丘陵
|
||||
mountain, // 山地
|
||||
plateau, // 高原
|
||||
basin, // 盆地
|
||||
}
|
||||
|
||||
/// 五大典型地貌部位(对应 C# LandformKind)
|
||||
enum LandformKind {
|
||||
peak, // 山峰
|
||||
ridge, // 山脊
|
||||
valley, // 山谷
|
||||
saddle, // 鞍部
|
||||
cliff, // 陡崖
|
||||
}
|
||||
|
||||
/// 等高线模型状态
|
||||
class ContourState {
|
||||
TerrainKind terrain = TerrainKind.mountain;
|
||||
LandformKind? selectedLandform;
|
||||
double progress = 0;
|
||||
bool isPlaying = false;
|
||||
bool colored = false;
|
||||
bool isTopView = false;
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'base.dart';
|
||||
import 'geography_model.dart';
|
||||
|
||||
/// 地球地理模型 Flutter 实现
|
||||
class GeographyModelFlutter implements GcModel {
|
||||
@override
|
||||
final String id = 'earth-geography-flutter';
|
||||
|
||||
@override
|
||||
String name() => '地球地理 (Flutter)';
|
||||
|
||||
@override
|
||||
String description() => '纯 Flutter 实现的地球地理模型,支持五带划分、七大洲四大洋';
|
||||
|
||||
@override
|
||||
Widget buildView(BuildContext context) {
|
||||
return const _GeographyModelView();
|
||||
}
|
||||
|
||||
/// 示例数据:五带
|
||||
List<GeoZone> get defaultZones => [
|
||||
GeoZone(
|
||||
id: 'torrid',
|
||||
nameZh: '热带',
|
||||
nameEn: 'Torrid Zone',
|
||||
latMin: -23.5,
|
||||
latMax: 23.5,
|
||||
descZh: '南北回归线之间,有太阳直射,终年高温。',
|
||||
descEn: 'Between the tropics; the Sun is overhead at some time each year.',
|
||||
),
|
||||
GeoZone(
|
||||
id: 'north-temperate',
|
||||
nameZh: '北温带',
|
||||
nameEn: 'North Temperate',
|
||||
latMin: 23.5,
|
||||
latMax: 66.5,
|
||||
descZh: '北回归线与北极圈之间,四季分明。',
|
||||
descEn: 'Between Tropic of Cancer and Arctic Circle; four distinct seasons.',
|
||||
),
|
||||
GeoZone(
|
||||
id: 'south-temperate',
|
||||
nameZh: '南温带',
|
||||
nameEn: 'South Temperate',
|
||||
latMin: -66.5,
|
||||
latMax: -23.5,
|
||||
descZh: '南回归线与南极圈之间,四季分明。',
|
||||
descEn: 'Between Tropic of Capricorn and Antarctic Circle; four seasons.',
|
||||
),
|
||||
GeoZone(
|
||||
id: 'north-frigid',
|
||||
nameZh: '北寒带',
|
||||
nameEn: 'North Frigid',
|
||||
latMin: 66.5,
|
||||
latMax: 90,
|
||||
descZh: '北极圈以北,终年寒冷,有极昼极夜。',
|
||||
descEn: 'Beyond the Arctic Circle; perpetually cold with polar day/night.',
|
||||
),
|
||||
GeoZone(
|
||||
id: 'south-frigid',
|
||||
nameZh: '南寒带',
|
||||
nameEn: 'South Frigid',
|
||||
latMin: -90,
|
||||
latMax: -66.5,
|
||||
descZh: '南极圈以南,终年寒冷,有极昼极夜。',
|
||||
descEn: 'Beyond the Antarctic Circle; perpetually cold with polar day/night.',
|
||||
),
|
||||
];
|
||||
|
||||
/// 示例数据:五大洲简化轮廓
|
||||
List<Continent> get defaultContinents => [
|
||||
Continent(
|
||||
nameZh: '亚洲',
|
||||
nameEn: 'Asia',
|
||||
points: [
|
||||
[60, 70], [100, 70], [140, 65], [180, 60],
|
||||
[180, 40], [140, 20], [120, 10], [100, 5],
|
||||
[80, 10], [60, 20], [50, 30], [40, 40],
|
||||
],
|
||||
),
|
||||
Continent(
|
||||
nameZh: '非洲',
|
||||
nameEn: 'Africa',
|
||||
points: [
|
||||
[35, 35], [50, 35], [50, 15], [40, 5],
|
||||
[30, -5], [20, -20], [15, -30], [20, -35],
|
||||
[30, -35], [35, -25], [40, -10], [35, 5],
|
||||
],
|
||||
),
|
||||
Continent(
|
||||
nameZh: '欧洲',
|
||||
nameEn: 'Europe',
|
||||
points: [
|
||||
[10, 70], [40, 70], [50, 60], [40, 50],
|
||||
[30, 45], [20, 45], [10, 50], [0, 55],
|
||||
],
|
||||
),
|
||||
Continent(
|
||||
nameZh: '北美洲',
|
||||
nameEn: 'North America',
|
||||
points: [
|
||||
[-170, 70], [-130, 70], [-80, 70], [-60, 50],
|
||||
[-60, 30], [-80, 20], [-100, 20], [-120, 30],
|
||||
[-160, 50], [-170, 60],
|
||||
],
|
||||
),
|
||||
Continent(
|
||||
nameZh: '南美洲',
|
||||
nameEn: 'South America',
|
||||
points: [
|
||||
[-80, 10], [-60, 10], [-35, -5], [-35, -20],
|
||||
[-50, -30], [-65, -50], [-75, -55], [-75, -40],
|
||||
[-70, -20], [-80, 0],
|
||||
],
|
||||
),
|
||||
Continent(
|
||||
nameZh: '大洋洲',
|
||||
nameEn: 'Oceania',
|
||||
points: [
|
||||
[115, -15], [150, -15], [160, -25], [150, -35],
|
||||
[130, -38], [115, -35], [113, -25],
|
||||
],
|
||||
),
|
||||
Continent(
|
||||
nameZh: '南极洲',
|
||||
nameEn: 'Antarctica',
|
||||
points: [
|
||||
[-180, -65], [-90, -65], [0, -65], [90, -65],
|
||||
[180, -65], [180, -90], [-180, -90],
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
class _GeographyModelView extends StatefulWidget {
|
||||
const _GeographyModelView();
|
||||
|
||||
@override
|
||||
State<_GeographyModelView> createState() => _GeographyModelViewState();
|
||||
}
|
||||
|
||||
class _GeographyModelViewState extends State<_GeographyModelView> {
|
||||
String _activeView = 'zones'; // zones, coast, plates
|
||||
bool _showZones = true;
|
||||
bool _showCoast = true;
|
||||
bool _showPlates = false;
|
||||
|
||||
final GeographyModelFlutter _model = GeographyModelFlutter();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
final bgColor = isDark ? const Color(0xFF0B1524) : const Color(0xFFE8F0FE);
|
||||
final panelColor = isDark ? const Color(0xFF101E31) : const Color(0xFFF5F7FA);
|
||||
final inkColor = isDark ? const Color(0xFFEAF2FF) : const Color(0xFF111A2E);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
body: Column(
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'地球地理 · 五带与七大洲',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: inkColor,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
_viewButton('五带', _activeView == 'zones', () => setState(() => _activeView = 'zones')),
|
||||
_viewButton('大洲', _activeView == 'coast', () => setState(() => _activeView = 'coast')),
|
||||
TextButton.icon(
|
||||
onPressed: () => setState(() {
|
||||
_showZones = !_showZones;
|
||||
_showCoast = !_showCoast;
|
||||
}),
|
||||
icon: const Icon(Icons.tune_rounded, size: 16),
|
||||
label: const Text('图层'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 地图区域
|
||||
Expanded(
|
||||
child: _GeographyMap(
|
||||
zones: _model.defaultZones,
|
||||
continents: _model.defaultContinents,
|
||||
showZones: _showZones,
|
||||
showCoast: _showCoast,
|
||||
activeView: _activeView,
|
||||
),
|
||||
),
|
||||
// 底部说明
|
||||
if (_activeView == 'zones' && _showZones)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
color: panelColor,
|
||||
child: Text(
|
||||
'热带:南北回归线之间 | 温带:回归线与极圈之间 | 寒带:极圈以内',
|
||||
style: TextStyle(fontSize: 12, color: isDark ? const Color(0xFF93A8C6) : const Color(0xFF5C6A80)),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _viewButton(String label, bool active, VoidCallback onTap) {
|
||||
return TextButton(
|
||||
onPressed: onTap,
|
||||
style: TextButton.styleFrom(
|
||||
backgroundColor: active ? const Color(0xFF4D8CFF) : null,
|
||||
foregroundColor: active ? Colors.white : null,
|
||||
),
|
||||
child: Text(label, style: const TextStyle(fontSize: 12)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _GeographyMap extends StatelessWidget {
|
||||
final List<GeoZone> zones;
|
||||
final List<Continent> continents;
|
||||
final bool showZones;
|
||||
final bool showCoast;
|
||||
final String activeView;
|
||||
|
||||
const _GeographyMap({
|
||||
required this.zones,
|
||||
required this.continents,
|
||||
required this.showZones,
|
||||
required this.showCoast,
|
||||
required this.activeView,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CustomPaint(
|
||||
size: Size.infinite,
|
||||
painter: _GeographyPainter(
|
||||
zones: zones,
|
||||
continents: continents,
|
||||
showZones: showZones,
|
||||
showCoast: showCoast,
|
||||
activeView: activeView,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _GeographyPainter extends CustomPainter {
|
||||
final List<GeoZone> zones;
|
||||
final List<Continent> continents;
|
||||
final bool showZones;
|
||||
final bool showCoast;
|
||||
final String activeView;
|
||||
|
||||
_GeographyPainter({
|
||||
required this.zones,
|
||||
required this.continents,
|
||||
required this.showZones,
|
||||
required this.showCoast,
|
||||
required this.activeView,
|
||||
});
|
||||
|
||||
/// 经纬度转画布坐标
|
||||
Offset _latLonToOffset(double lon, double lat, Size size) {
|
||||
final x = (lon + 180) / 360 * size.width;
|
||||
final y = (90 - lat) / 180 * size.height;
|
||||
return Offset(x, y);
|
||||
}
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final bgColor = const Color(0xFF0E2A4A);
|
||||
final oceanColor = const Color(0xFF0E2A4A);
|
||||
final zoneColors = {
|
||||
'torrid': const Color(0xFFFF6B35),
|
||||
'north-temperate': const Color(0xFFFFD93D),
|
||||
'south-temperate': const Color(0xFF6BCB77),
|
||||
'north-frigid': const Color(0xFF4D8CFF),
|
||||
'south-frigid': const Color(0xFF8B5CF6),
|
||||
};
|
||||
|
||||
// 海洋背景
|
||||
canvas.drawColor(oceanColor, BlendMode.src);
|
||||
|
||||
// 绘制五带
|
||||
if (showZones && activeView == 'zones') {
|
||||
final bandHeight = size.height / 9; // 180度纬度分9份
|
||||
final bandColors = [
|
||||
zoneColors['north-frigid']!,
|
||||
zoneColors['north-temperate']!,
|
||||
zoneColors['torrid']!,
|
||||
zoneColors['south-temperate']!,
|
||||
zoneColors['south-frigid']!,
|
||||
];
|
||||
final bandLatRanges = [
|
||||
(66.5, 90),
|
||||
(23.5, 66.5),
|
||||
(-23.5, 23.5),
|
||||
(-66.5, -23.5),
|
||||
(-90, -66.5),
|
||||
];
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
final (latMin, latMax) = bandLatRanges[i];
|
||||
final y1 = (90 - latMax) / 180 * size.height;
|
||||
final y2 = (90 - latMin) / 180 * size.height;
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(0, y1, size.width, y2 - y1),
|
||||
Paint()..color = bandColors[i].withOpacity(0.3),
|
||||
);
|
||||
}
|
||||
|
||||
// 绘制回归线和极圈
|
||||
for (final lat in [23.5, -23.5, 66.5, -66.5]) {
|
||||
final y = (90 - lat) / 180 * size.height;
|
||||
canvas.drawLine(
|
||||
Offset(0, y),
|
||||
Offset(size.width, y),
|
||||
Paint()
|
||||
..color = Colors.white.withOpacity(0.4)
|
||||
..strokeWidth = 1
|
||||
..style = PaintingStyle.stroke
|
||||
..style = PaintingStyle.stroke,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 绘制大陆轮廓
|
||||
if (showCoast && activeView == 'coast') {
|
||||
for (final continent in continents) {
|
||||
if (continent.points.length < 2) continue;
|
||||
|
||||
final path = Path();
|
||||
final start = _latLonToOffset(continent.points[0][0], continent.points[0][1], size);
|
||||
path.moveTo(start.dx, start.dy);
|
||||
|
||||
for (int i = 1; i < continent.points.length; i++) {
|
||||
final p = _latLonToOffset(continent.points[i][0], continent.points[i][1], size);
|
||||
path.lineTo(p.dx, p.dy);
|
||||
}
|
||||
path.close();
|
||||
|
||||
canvas.drawPath(
|
||||
path,
|
||||
Paint()
|
||||
..color = const Color(0xFF4E8C4C)
|
||||
..style = PaintingStyle.fill,
|
||||
);
|
||||
canvas.drawPath(
|
||||
path,
|
||||
Paint()
|
||||
..color = Colors.white.withOpacity(0.6)
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _GeographyPainter oldDelegate) {
|
||||
return oldDelegate.showZones != showZones ||
|
||||
oldDelegate.showCoast != showCoast ||
|
||||
oldDelegate.activeView != activeView;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 地球地理模型数据
|
||||
class GeoZone {
|
||||
final String id;
|
||||
final String nameZh;
|
||||
final String nameEn;
|
||||
final double latMin;
|
||||
final double latMax;
|
||||
final String descZh;
|
||||
final String descEn;
|
||||
|
||||
GeoZone({
|
||||
required this.id,
|
||||
required this.nameZh,
|
||||
required this.nameEn,
|
||||
required this.latMin,
|
||||
required this.latMax,
|
||||
required this.descZh,
|
||||
required this.descEn,
|
||||
});
|
||||
}
|
||||
|
||||
/// 大陆板块数据
|
||||
class Continent {
|
||||
final String nameZh;
|
||||
final String nameEn;
|
||||
final List<List<double>> points; // [lon, lat]
|
||||
|
||||
Continent({
|
||||
required this.nameZh,
|
||||
required this.nameEn,
|
||||
required this.points,
|
||||
});
|
||||
}
|
||||
|
||||
/// 板块数据
|
||||
class Plate {
|
||||
final String id;
|
||||
final String nameZh;
|
||||
final String nameEn;
|
||||
final List<List<double>> points;
|
||||
|
||||
Plate({
|
||||
required this.id,
|
||||
required this.nameZh,
|
||||
required this.nameEn,
|
||||
required this.points,
|
||||
});
|
||||
}
|
||||
|
||||
/// 洲界线
|
||||
class Border {
|
||||
final String betweenZh;
|
||||
final String betweenEn;
|
||||
final String nameZh;
|
||||
final String nameEn;
|
||||
final List<List<double>> points;
|
||||
|
||||
Border({
|
||||
required this.betweenZh,
|
||||
required this.betweenEn,
|
||||
required this.nameZh,
|
||||
required this.nameEn,
|
||||
required this.points,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/// 模型注册表:所有 Flutter 实现的模型都在这里注册。
|
||||
library;
|
||||
|
||||
export 'base.dart';
|
||||
export 'contour_model.dart';
|
||||
export 'contour_flutter.dart';
|
||||
export 'geography_model.dart';
|
||||
export 'geography_flutter.dart';
|
||||
export 'rotation_flutter.dart';
|
||||
export 'revolution_flutter.dart';
|
||||
export 'scale_model.dart';
|
||||
export 'scale_flutter.dart';
|
||||
|
||||
import 'base.dart';
|
||||
import 'contour_flutter.dart';
|
||||
import 'geography_flutter.dart';
|
||||
import 'rotation_flutter.dart';
|
||||
import 'revolution_flutter.dart';
|
||||
import 'scale_flutter.dart';
|
||||
|
||||
/// 所有可用的 Flutter 模型实例。
|
||||
final List<GcModel> allGcModels = [
|
||||
ContourModelFlutter(),
|
||||
GeographyModelFlutter(),
|
||||
RotationModelFlutter(),
|
||||
RevolutionModelFlutter(),
|
||||
ScaleCalculatorModelFlutter(),
|
||||
];
|
||||
|
||||
/// 根据 id 查找模型。
|
||||
GcModel? findModel(String id) {
|
||||
try {
|
||||
return allGcModels.firstWhere((m) => m.id == id);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'base.dart';
|
||||
import 'revolution_model.dart';
|
||||
|
||||
/// 地球公转模型 Flutter 实现
|
||||
class RevolutionModelFlutter implements GcModel {
|
||||
@override
|
||||
final String id = 'earth-revolution-flutter';
|
||||
|
||||
@override
|
||||
String name() => '地球公转 (Flutter)';
|
||||
|
||||
@override
|
||||
String description() => '纯 Flutter 实现的地球公转模型,观察四季成因';
|
||||
|
||||
@override
|
||||
Widget buildView(BuildContext context) {
|
||||
return const _RevolutionModelView();
|
||||
}
|
||||
|
||||
List<Season> get defaultSeasons => [
|
||||
Season(key: 'spring', date: '3/21', declinationDeg: 0, angleDeg: 0,
|
||||
teachingZh: '太阳直射赤道,全球昼夜平分。', teachingEn: 'Sun overhead at equator.'),
|
||||
Season(key: 'summer', date: '6/22', declinationDeg: 23.5, angleDeg: 90,
|
||||
teachingZh: '太阳直射北回归线,北半球昼最长。', teachingEn: 'Sun at Tropic of Cancer.'),
|
||||
Season(key: 'autumn', date: '9/23', declinationDeg: 0, angleDeg: 180,
|
||||
teachingZh: '太阳再次直射赤道,昼夜平分。', teachingEn: 'Sun back at equator.'),
|
||||
Season(key: 'winter', date: '12/22', declinationDeg: -23.5, angleDeg: 270,
|
||||
teachingZh: '太阳直射南回归线,北半球昼最短。', teachingEn: 'Sun at Tropic of Capricorn.'),
|
||||
];
|
||||
}
|
||||
|
||||
class _RevolutionModelView extends StatefulWidget {
|
||||
const _RevolutionModelView();
|
||||
|
||||
@override
|
||||
State<_RevolutionModelView> createState() => _RevolutionModelViewState();
|
||||
}
|
||||
|
||||
class _RevolutionModelViewState extends State<_RevolutionModelView>
|
||||
with TickerProviderStateMixin {
|
||||
late AnimationController _orbitController;
|
||||
bool _isPlaying = true;
|
||||
double _speed = 1.0;
|
||||
int _selectedSeason = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_orbitController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(seconds: 12),
|
||||
)..repeat();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_orbitController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _togglePlay() {
|
||||
setState(() => _isPlaying = !_isPlaying);
|
||||
if (_isPlaying) _orbitController.repeat();
|
||||
else _orbitController.stop();
|
||||
}
|
||||
|
||||
void _reset() {
|
||||
setState(() {
|
||||
_isPlaying = true;
|
||||
_speed = 1.0;
|
||||
_selectedSeason = 0;
|
||||
});
|
||||
_orbitController.repeat();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
final bgColor = isDark ? const Color(0xFF0B1524) : const Color(0xFFE8F0FE);
|
||||
final panelColor = isDark ? const Color(0xFF101E31) : const Color(0xFFF5F7FA);
|
||||
final inkColor = isDark ? const Color(0xFFEAF2FF) : const Color(0xFF111A2E);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
body: Column(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
Text('地球公转 · 四季成因', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: inkColor)),
|
||||
const Spacer(),
|
||||
...['春分', '夏至', '秋分', '冬至'].asMap().entries.map((e) =>
|
||||
Padding(padding: const EdgeInsets.only(left: 6),
|
||||
child: FilterChip(
|
||||
label: Text(e.value, style: const TextStyle(fontSize: 11)),
|
||||
selected: _selectedSeason == e.key,
|
||||
onSelected: (_) => setState(() => _selectedSeason = e.key),
|
||||
selectedColor: const Color(0xFF4D8CFF).withOpacity(0.2),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _RevolutionOrbit(
|
||||
progress: _orbitController.value,
|
||||
selectedSeason: _selectedSeason,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(onPressed: _togglePlay, icon: Icon(_isPlaying ? Icons.pause_rounded : Icons.play_arrow_rounded), color: const Color(0xFF4D8CFF)),
|
||||
const SizedBox(width: 16),
|
||||
Text('速度:', style: TextStyle(fontSize: 12, color: isDark ? const Color(0xFF93A8C6) : const Color(0xFF5C6A80))),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: Slider(value: _speed, min: 0.1, max: 3.0, divisions: 29, onChanged: (v) => setState(() => _speed = v))),
|
||||
Text('${_speed.toStringAsFixed(1)}x'),
|
||||
const Spacer(),
|
||||
TextButton.icon(onPressed: _reset, icon: const Icon(Icons.refresh_rounded, size: 16), label: const Text('重置')),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RevolutionOrbit extends StatelessWidget {
|
||||
final double progress;
|
||||
final int selectedSeason;
|
||||
|
||||
const _RevolutionOrbit({required this.progress, required this.selectedSeason});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CustomPaint(
|
||||
size: Size.infinite,
|
||||
painter: _RevolutionPainter(progress: progress, selectedSeason: selectedSeason),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RevolutionPainter extends CustomPainter {
|
||||
final double progress;
|
||||
final int selectedSeason;
|
||||
|
||||
_RevolutionPainter({required this.progress, required this.selectedSeason});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final bgColor = const Color(0xFF08111E);
|
||||
final sunColor = const Color(0xFFFFC24B);
|
||||
final orbitColor = const Color(0xFF4D8CFF);
|
||||
final earthColor = const Color(0xFF1C5FA8);
|
||||
|
||||
canvas.drawColor(bgColor, BlendMode.src);
|
||||
|
||||
final center = Offset(size.width / 2, size.height / 2);
|
||||
final orbitRadius = math.min(size.width, size.height) * 0.35;
|
||||
|
||||
// 绘制轨道
|
||||
canvas.drawCircle(center, orbitRadius, Paint()
|
||||
..color = orbitColor.withOpacity(0.3)
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 1.5
|
||||
|
||||
..style = PaintingStyle.stroke);
|
||||
|
||||
// 绘制太阳
|
||||
canvas.drawCircle(center, 30, Paint()
|
||||
..color = sunColor
|
||||
..style = PaintingStyle.fill);
|
||||
|
||||
// 太阳发光效果
|
||||
for (int i = 4; i > 0; i--) {
|
||||
canvas.drawCircle(center, 30 + i * 12, Paint()
|
||||
..color = sunColor.withOpacity(0.1 * (5 - i))
|
||||
..style = PaintingStyle.fill);
|
||||
}
|
||||
|
||||
// 绘制地球位置(根据进度)
|
||||
final angle = progress * 2 * math.pi;
|
||||
final earthX = center.dx + math.cos(angle) * orbitRadius;
|
||||
final earthY = center.dy + math.sin(angle) * orbitRadius;
|
||||
|
||||
// 地球
|
||||
canvas.drawCircle(Offset(earthX, earthY), 15, Paint()
|
||||
..color = earthColor
|
||||
..style = PaintingStyle.fill);
|
||||
|
||||
// 地轴(倾斜23.5度)
|
||||
final tiltAngle = 23.5 * math.pi / 180;
|
||||
canvas.drawLine(
|
||||
Offset(earthX - math.sin(tiltAngle) * 25, earthY - math.cos(tiltAngle) * 25),
|
||||
Offset(earthX + math.sin(tiltAngle) * 25, earthY + math.cos(tiltAngle) * 25),
|
||||
Paint()
|
||||
..color = const Color(0xFFFF6B35)
|
||||
..strokeWidth = 2
|
||||
..style = PaintingStyle.stroke);
|
||||
|
||||
// 标注四个关键位置
|
||||
final seasonLabels = ['春分', '夏至', '秋分', '冬至'];
|
||||
final seasonColors = [const Color(0xFF6BCB77), const Color(0xFFFFD93D), const Color(0xFF4D8CFF), const Color(0xFF8B5CF6)];
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
final seasonAngle = i * math.pi / 2;
|
||||
final sx = center.dx + math.cos(seasonAngle) * orbitRadius;
|
||||
final sy = center.dy + math.sin(seasonAngle) * orbitRadius;
|
||||
|
||||
// 小圆点标记
|
||||
canvas.drawCircle(Offset(sx, sy), 5, Paint()
|
||||
..color = seasonColors[i]
|
||||
..style = PaintingStyle.fill);
|
||||
|
||||
// 标签
|
||||
final labelX = sx + math.cos(seasonAngle) * 25;
|
||||
final labelY = sy + math.sin(seasonAngle) * 25;
|
||||
final textPainter = TextPainter(
|
||||
text: TextSpan(text: seasonLabels[i], style: const TextStyle(fontSize: 10, color: Color(0xFFEAF2FF))),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(canvas, Offset(labelX - 15, labelY - 8));
|
||||
}
|
||||
|
||||
// 当前选中季节高亮
|
||||
if (selectedSeason >= 0 && selectedSeason < 4) {
|
||||
final selAngle = selectedSeason * math.pi / 2;
|
||||
final selX = center.dx + math.cos(selAngle) * orbitRadius;
|
||||
final selY = center.dy + math.sin(selAngle) * orbitRadius;
|
||||
|
||||
canvas.drawCircle(Offset(selX, selY), 8, Paint()
|
||||
..color = seasonColors[selectedSeason].withOpacity(0.5)
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _RevolutionPainter oldDelegate) {
|
||||
return oldDelegate.progress != progress || oldDelegate.selectedSeason != selectedSeason;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 节气数据
|
||||
class Season {
|
||||
final String key;
|
||||
final String date;
|
||||
final double declinationDeg;
|
||||
final double angleDeg;
|
||||
final String teachingZh;
|
||||
final String teachingEn;
|
||||
|
||||
Season({
|
||||
required this.key,
|
||||
required this.date,
|
||||
required this.declinationDeg,
|
||||
required this.angleDeg,
|
||||
required this.teachingZh,
|
||||
required this.teachingEn,
|
||||
});
|
||||
}
|
||||
|
||||
/// 教学要点
|
||||
class Lesson {
|
||||
final String titleZh;
|
||||
final String titleEn;
|
||||
final String bodyZh;
|
||||
final String bodyEn;
|
||||
final String view;
|
||||
final double? angleDeg;
|
||||
|
||||
Lesson({
|
||||
required this.titleZh,
|
||||
required this.titleEn,
|
||||
required this.bodyZh,
|
||||
required this.bodyEn,
|
||||
required this.view,
|
||||
this.angleDeg,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'base.dart';
|
||||
|
||||
/// 地球自转模型 Flutter 实现
|
||||
class RotationModelFlutter implements GcModel {
|
||||
@override
|
||||
final String id = 'earth-rotation-flutter';
|
||||
|
||||
@override
|
||||
String name() => '地球自转 (Flutter)';
|
||||
|
||||
@override
|
||||
String description() => '纯 Flutter 实现的地球自转模型,观察昼夜交替、晨昏线';
|
||||
|
||||
@override
|
||||
Widget buildView(BuildContext context) {
|
||||
return const _RotationModelView();
|
||||
}
|
||||
}
|
||||
|
||||
class _RotationModelView extends StatefulWidget {
|
||||
const _RotationModelView();
|
||||
|
||||
@override
|
||||
State<_RotationModelView> createState() => _RotationModelViewState();
|
||||
}
|
||||
|
||||
class _RotationModelViewState extends State<_RotationModelView>
|
||||
with TickerProviderStateMixin {
|
||||
late AnimationController _spinController;
|
||||
late AnimationController _tiltController;
|
||||
bool _isPlaying = true;
|
||||
double _speed = 1.0;
|
||||
bool _showLabels = true;
|
||||
bool _showTilt = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_spinController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(seconds: 10),
|
||||
)..repeat();
|
||||
_tiltController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 500),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_spinController.dispose();
|
||||
_tiltController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _togglePlay() {
|
||||
setState(() {
|
||||
_isPlaying = !_isPlaying;
|
||||
});
|
||||
if (_isPlaying) {
|
||||
_spinController.repeat();
|
||||
} else {
|
||||
_spinController.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void _reset() {
|
||||
setState(() {
|
||||
_isPlaying = true;
|
||||
_speed = 1.0;
|
||||
_showLabels = true;
|
||||
_showTilt = false;
|
||||
});
|
||||
_spinController.repeat();
|
||||
_tiltController.reset();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
final bgColor = isDark ? const Color(0xFF0B1524) : const Color(0xFFE8F0FE);
|
||||
final panelColor = isDark ? const Color(0xFF101E31) : const Color(0xFFF5F7FA);
|
||||
final inkColor = isDark ? const Color(0xFFEAF2FF) : const Color(0xFF111A2E);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
body: Column(
|
||||
children: [
|
||||
// 标题栏
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'地球自转 · 昼夜交替',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: inkColor,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
_toggleButton('标注', _showLabels, () => setState(() => _showLabels = !_showLabels)),
|
||||
_toggleButton('倾角', _showTilt, () => setState(() => _showTilt = !_showTilt)),
|
||||
const SizedBox(width: 8),
|
||||
TextButton.icon(
|
||||
onPressed: _reset,
|
||||
icon: const Icon(Icons.refresh_rounded, size: 16),
|
||||
label: const Text('重置'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// 地球动画
|
||||
Expanded(
|
||||
child: _RotationEarth(
|
||||
animProgress: _spinController.value,
|
||||
showLabels: _showLabels,
|
||||
showTilt: _showTilt,
|
||||
isPlaying: _isPlaying,
|
||||
),
|
||||
),
|
||||
// 底部控制栏
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: _togglePlay,
|
||||
icon: Icon(_isPlaying ? Icons.pause_rounded : Icons.play_arrow_rounded),
|
||||
color: const Color(0xFF4D8CFF),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Text('速度:', style: TextStyle(fontSize: 12, color: isDark ? const Color(0xFF93A8C6) : const Color(0xFF5C6A80))),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Slider(
|
||||
value: _speed,
|
||||
min: 0.1,
|
||||
max: 3.0,
|
||||
divisions: 29,
|
||||
onChanged: (v) => setState(() => _speed = v),
|
||||
),
|
||||
),
|
||||
Text('${_speed.toStringAsFixed(1)}x', style: const TextStyle(fontSize: 12)),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _toggleButton(String label, bool active, VoidCallback onTap) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(left: 8),
|
||||
child: FilterChip(
|
||||
label: Text(label, style: const TextStyle(fontSize: 12)),
|
||||
selected: active,
|
||||
onSelected: (_) => onTap(),
|
||||
selectedColor: const Color(0xFF4D8CFF).withOpacity(0.2),
|
||||
checkmarkColor: const Color(0xFF4D8CFF),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RotationEarth extends StatelessWidget {
|
||||
final double animProgress;
|
||||
final bool showLabels;
|
||||
final bool showTilt;
|
||||
final bool isPlaying;
|
||||
|
||||
const _RotationEarth({
|
||||
required this.animProgress,
|
||||
required this.showLabels,
|
||||
required this.showTilt,
|
||||
required this.isPlaying,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CustomPaint(
|
||||
size: Size.infinite,
|
||||
painter: _RotationPainter(
|
||||
animProgress: animProgress,
|
||||
showLabels: showLabels,
|
||||
showTilt: showTilt,
|
||||
isPlaying: isPlaying,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RotationPainter extends CustomPainter {
|
||||
final double animProgress;
|
||||
final bool showLabels;
|
||||
final bool showTilt;
|
||||
final bool isPlaying;
|
||||
|
||||
_RotationPainter({
|
||||
required this.animProgress,
|
||||
required this.showLabels,
|
||||
required this.showTilt,
|
||||
required this.isPlaying,
|
||||
});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final bgColor = const Color(0xFF08111E);
|
||||
final earthColor = const Color(0xFF1C5FA8);
|
||||
final landColor = const Color(0xFF4E8C4C);
|
||||
final iceColor = const Color(0xFFE9EFF6);
|
||||
final axisColor = const Color(0xFFFF6B35);
|
||||
final labelColor = const Color(0xFFEAF2FF);
|
||||
|
||||
canvas.drawColor(bgColor, BlendMode.src);
|
||||
|
||||
final center = Offset(size.width / 2, size.height / 2);
|
||||
final radius = math.min(size.width, size.height) * 0.3;
|
||||
|
||||
// 绘制地球球体
|
||||
final earthPaint = Paint()
|
||||
..color = earthColor
|
||||
..style = PaintingStyle.fill;
|
||||
canvas.drawCircle(center, radius, earthPaint);
|
||||
|
||||
// 绘制大陆(简化版,用椭圆模拟)
|
||||
final continents = [
|
||||
(center.dx - radius * 0.3, center.dy - radius * 0.2, radius * 0.25, radius * 0.35), // 亚洲
|
||||
(center.dx + radius * 0.2, center.dy - radius * 0.1, radius * 0.2, radius * 0.25), // 欧洲
|
||||
(center.dx - radius * 0.1, center.dy + radius * 0.2, radius * 0.15, radius * 0.3), // 非洲
|
||||
(center.dx + radius * 0.35, center.dy + radius * 0.1, radius * 0.2, radius * 0.25), // 北美
|
||||
(center.dx + radius * 0.3, center.dy + radius * 0.4, radius * 0.15, radius * 0.2), // 南美
|
||||
];
|
||||
|
||||
for (final cont in continents) {
|
||||
canvas.drawOval(
|
||||
Rect.fromLTWH(cont.$1 - cont.$3 / 2, cont.$2 - cont.$4 / 2, cont.$3, cont.$4),
|
||||
Paint()..color = landColor,
|
||||
);
|
||||
}
|
||||
|
||||
// 绘制极冰
|
||||
canvas.drawOval(
|
||||
Rect.fromLTWH(center.dx - radius * 0.15, center.dy - radius * 0.95, radius * 0.3, radius * 0.15),
|
||||
Paint()..color = iceColor,
|
||||
);
|
||||
canvas.drawOval(
|
||||
Rect.fromLTWH(center.dx - radius * 0.15, center.dy + radius * 0.8, radius * 0.3, radius * 0.15),
|
||||
Paint()..color = iceColor,
|
||||
);
|
||||
|
||||
// 绘制地轴
|
||||
if (showTilt) {
|
||||
final tiltAngle = 23.5 * math.pi / 180;
|
||||
final axisLength = radius * 1.4;
|
||||
final topY = center.dy - axisLength / 2;
|
||||
final bottomY = center.dy + axisLength / 2;
|
||||
final offsetX = math.sin(tiltAngle) * axisLength / 2;
|
||||
|
||||
canvas.drawLine(
|
||||
Offset(center.dx - offsetX, topY),
|
||||
Offset(center.dx + offsetX, bottomY),
|
||||
Paint()
|
||||
..color = axisColor
|
||||
..strokeWidth = 2
|
||||
..style = PaintingStyle.stroke,
|
||||
);
|
||||
|
||||
// 北极标注
|
||||
if (showLabels) {
|
||||
final textPainter = TextPainter(
|
||||
text: const TextSpan(text: '北极', style: TextStyle(fontSize: 11, color: Color(0xFFFF6B35))),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(canvas, Offset(center.dx + offsetX + 10, topY - 5));
|
||||
}
|
||||
}
|
||||
|
||||
// 绘制晨昏线(昼夜分界)
|
||||
final sunAngle = animProgress * 2 * math.pi;
|
||||
final terminatorX = center.dx + math.cos(sunAngle) * radius * 0.3;
|
||||
|
||||
// 昼半球高光
|
||||
final dayPaint = Paint()
|
||||
..color = Colors.yellow.withOpacity(0.15)
|
||||
..style = PaintingStyle.fill;
|
||||
canvas.drawCircle(
|
||||
Offset(terminatorX + math.cos(sunAngle) * radius * 0.3, center.dy),
|
||||
radius * 0.9,
|
||||
dayPaint,
|
||||
);
|
||||
|
||||
// 标注直射点
|
||||
if (showLabels) {
|
||||
final subsolarX = center.dx + math.cos(sunAngle) * radius;
|
||||
final subsolarY = center.dy + math.sin(sunAngle * 0.3) * radius * 0.5;
|
||||
|
||||
// 太阳光线
|
||||
canvas.drawLine(
|
||||
Offset(subsolarX + math.cos(sunAngle) * 20, subsolarY + math.sin(sunAngle) * 20),
|
||||
Offset(subsolarX + math.cos(sunAngle) * 50, subsolarY + math.sin(sunAngle) * 50),
|
||||
Paint()
|
||||
..color = const Color(0xFFFFC24B)
|
||||
..strokeWidth = 2
|
||||
..style = PaintingStyle.stroke,
|
||||
);
|
||||
|
||||
final textPainter = TextPainter(
|
||||
text: const TextSpan(text: '直射点', style: TextStyle(fontSize: 10, color: Color(0xFFFFC24B))),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(canvas, Offset(subsolarX + 15, subsolarY - 10));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _RotationPainter oldDelegate) {
|
||||
return oldDelegate.animProgress != animProgress ||
|
||||
oldDelegate.showLabels != showLabels ||
|
||||
oldDelegate.showTilt != showTilt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'base.dart';
|
||||
import 'scale_model.dart';
|
||||
|
||||
class ScaleCalculatorModelFlutter implements GcModel {
|
||||
@override
|
||||
final String id = 'scale-calculator-flutter';
|
||||
@override
|
||||
String name() => '比例尺计算器 (Flutter)';
|
||||
@override
|
||||
String description() => '纯 Flutter 实现的比例尺计算器';
|
||||
@override
|
||||
Widget buildView(BuildContext context) => const _ScaleCalculatorView();
|
||||
|
||||
List<ScalePreset> get presets => [
|
||||
ScalePreset(n: 10000, nameKey: 'preset.campus', noteKey: 'preset.campusNote'),
|
||||
ScalePreset(n: 50000, nameKey: 'preset.town', noteKey: 'preset.townNote'),
|
||||
ScalePreset(n: 250000, nameKey: 'preset.city', noteKey: 'preset.cityNote'),
|
||||
ScalePreset(n: 1000000, nameKey: 'preset.province', noteKey: 'preset.provinceNote'),
|
||||
ScalePreset(n: 10000000, nameKey: 'preset.china', noteKey: 'preset.chinaNote'),
|
||||
ScalePreset(n: 100000000, nameKey: 'preset.world', noteKey: 'preset.worldNote'),
|
||||
];
|
||||
|
||||
List<ScaleSpectrum> get spectrum => [
|
||||
ScaleSpectrum(n: 10000, labelKey: 'spec.campus', scopeKey: 'spec.campusScope'),
|
||||
ScaleSpectrum(n: 250000, labelKey: 'spec.city', scopeKey: 'spec.cityScope'),
|
||||
ScaleSpectrum(n: 10000000, labelKey: 'spec.china', scopeKey: 'spec.chinaScope'),
|
||||
ScaleSpectrum(n: 100000000, labelKey: 'spec.world', scopeKey: 'spec.worldScope'),
|
||||
];
|
||||
|
||||
List<Landmark> get landmarks => [
|
||||
Landmark(nameKey: 'lm.classroom', meters: 10),
|
||||
Landmark(nameKey: 'lm.track', meters: 400),
|
||||
Landmark(nameKey: 'lm.marathon', meters: 42195),
|
||||
Landmark(nameKey: 'lm.beijingShanghai', meters: 1080000),
|
||||
Landmark(nameKey: 'lm.equator', meters: 40000000),
|
||||
];
|
||||
}
|
||||
|
||||
class _ScaleCalculatorView extends StatefulWidget {
|
||||
const _ScaleCalculatorView();
|
||||
@override
|
||||
State<_ScaleCalculatorView> createState() => _ScaleCalculatorViewState();
|
||||
}
|
||||
|
||||
class _ScaleCalculatorViewState extends State<_ScaleCalculatorView> {
|
||||
String _mode = 'real';
|
||||
double _mapDist = 10;
|
||||
double _realDist = 100000;
|
||||
double _scaleN = 10000.0;
|
||||
String _unit = 'cm';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
final bgColor = isDark ? const Color(0xFF0B1524) : const Color(0xFFE8F0FE);
|
||||
final panelColor = isDark ? const Color(0xFF101E31) : const Color(0xFFF5F7FA);
|
||||
final inkColor = isDark ? const Color(0xFFEAF2FF) : const Color(0xFF111A2E);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: bgColor,
|
||||
body: Column(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 12),
|
||||
color: panelColor,
|
||||
child: Row(
|
||||
children: [
|
||||
Text('比例尺计算器', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: inkColor)),
|
||||
const Spacer(),
|
||||
_modeBtn('求实地距离', _mode == 'real'),
|
||||
_modeBtn('求图上距离', _mode == 'map'),
|
||||
_modeBtn('求比例尺', _mode == 'scale'),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _ScaleCanvas(
|
||||
mode: _mode,
|
||||
mapDist: _mapDist,
|
||||
realDist: _realDist,
|
||||
scaleN: _scaleN,
|
||||
unit: _unit,
|
||||
),
|
||||
),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
color: panelColor,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('已知量:', style: TextStyle(fontSize: 12, color: isDark ? const Color(0xFF93A8C6) : const Color(0xFF5C6A80))),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
decoration: const InputDecoration(labelText: '图上距离 (cm)'),
|
||||
keyboardType: TextInputType.number,
|
||||
onChanged: (v) => setState(() => _mapDist = double.tryParse(v) ?? 0),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: TextField(
|
||||
decoration: const InputDecoration(labelText: '比例尺 N'),
|
||||
keyboardType: TextInputType.number,
|
||||
onChanged: (v) => setState(() => _scaleN = double.tryParse(v) ?? 10000.0),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Text('结果:', style: TextStyle(fontSize: 12, color: isDark ? const Color(0xFF93A8C6) : const Color(0xFF5C6A80))),
|
||||
const SizedBox(width: 8),
|
||||
Text('实地距离 = ${(_mapDist * _scaleN / 100).toStringAsFixed(2)} m', style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Color(0xFF4D8CFF))),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _modeBtn(String label, bool active) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(left: 6),
|
||||
child: FilterChip(
|
||||
label: Text(label, style: const TextStyle(fontSize: 12)),
|
||||
selected: active,
|
||||
onSelected: (_) => setState(() => _mode = active ? 'real' : 'real'),
|
||||
selectedColor: const Color(0xFF4D8CFF).withOpacity(0.2),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ScaleCanvas extends StatelessWidget {
|
||||
final String mode;
|
||||
final double mapDist;
|
||||
final double realDist;
|
||||
final double scaleN;
|
||||
final String unit;
|
||||
|
||||
const _ScaleCanvas({required this.mode, required this.mapDist, required this.realDist, required this.scaleN, required this.unit});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CustomPaint(
|
||||
size: Size.infinite,
|
||||
painter: _ScalePainter(mode: mode, mapDist: mapDist, realDist: realDist, scaleN: scaleN, unit: unit),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ScalePainter extends CustomPainter {
|
||||
final String mode;
|
||||
final double mapDist;
|
||||
final double realDist;
|
||||
final double scaleN;
|
||||
final String unit;
|
||||
|
||||
_ScalePainter({required this.mode, required this.mapDist, required this.realDist, required this.scaleN, required this.unit});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final isDark = true;
|
||||
final bgColor = const Color(0xFF08111E);
|
||||
final barColor = const Color(0xFF4D8CFF);
|
||||
final inkColor = const Color(0xFFEAF2FF);
|
||||
|
||||
canvas.drawColor(bgColor, BlendMode.src);
|
||||
|
||||
final centerY = size.height / 2;
|
||||
final double barHeight = 40.0;
|
||||
final barY = centerY - barHeight / 2;
|
||||
final maxBarWidth = size.width * 0.8;
|
||||
final startX = (size.width - maxBarWidth) / 2;
|
||||
|
||||
// 绘制标尺
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(Rect.fromLTWH(startX, barY, maxBarWidth, barHeight), Radius.circular(4.0)),
|
||||
Paint()..color = barColor.withOpacity(0.2),
|
||||
);
|
||||
|
||||
// 刻度
|
||||
final double tickCount = 20.0;
|
||||
for (int i = 0; i <= tickCount; i++) {
|
||||
final x = startX + (maxBarWidth * i / tickCount);
|
||||
final double tickHeight = i % 5 == 0 ? 15.0 : 8.0;
|
||||
canvas.drawLine(
|
||||
Offset(x, barY + (barHeight - tickHeight) / 2),
|
||||
Offset(x, barY + (barHeight + tickHeight) / 2),
|
||||
Paint()
|
||||
..color = inkColor
|
||||
..strokeWidth = 1,
|
||||
);
|
||||
}
|
||||
|
||||
// 数值标注
|
||||
final textPainter = TextPainter(
|
||||
text: TextSpan(text: '1 : $scaleN', style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w600, color: Color(0xFF4D8CFF))),
|
||||
textDirection: TextDirection.ltr,
|
||||
);
|
||||
textPainter.layout();
|
||||
textPainter.paint(canvas, Offset(size.width / 2 - 40, barY + barHeight + 16));
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant _ScalePainter oldDelegate) {
|
||||
return oldDelegate.mode != mode || oldDelegate.scaleN != scaleN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 比例尺预设
|
||||
class ScalePreset {
|
||||
final int n;
|
||||
final String nameKey;
|
||||
final String noteKey;
|
||||
|
||||
ScalePreset({required this.n, required this.nameKey, required this.noteKey});
|
||||
}
|
||||
|
||||
/// 比例尺光谱
|
||||
class ScaleSpectrum {
|
||||
final int n;
|
||||
final String labelKey;
|
||||
final String scopeKey;
|
||||
|
||||
ScaleSpectrum({required this.n, required this.labelKey, required this.scopeKey});
|
||||
}
|
||||
|
||||
/// 真实地标
|
||||
class Landmark {
|
||||
final String nameKey;
|
||||
final double meters;
|
||||
|
||||
Landmark({required this.nameKey, required this.meters});
|
||||
}
|
||||
Reference in new issue
Block a user