rtc_prd/airhub_app/lib/pages/story_loading_page.dart
seaislee1209 8f5fb32b37 feat(story,music,server): 豆包故事生成 + 历史数据持久化 + 封面占位
- 接入火山引擎豆包 Chat API 生成儿童故事(SSE 流式进度)
- 新增 /api/stories 接口加载历史故事到书架
- 新增 /api/playlist 接口加载历史歌曲到唱片架
- 书架排序:预设故事在前,AI 生成在后
- AI 生成的故事显示"暂无封面"淡紫渐变占位
- 保存故事时传回真实标题+内容(不再用 mock)
- 修复 Windows GBK 编码导致的中文乱码问题
- 新增 MusicGenerationService 单例管理音乐生成
- 音乐页心情卡片 UI 重做 + 歌词可读性优化
- 添加豆包 API 参考文档和故事创作 prompt

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-09 23:11:58 +08:00

251 lines
7.2 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'story_detail_page.dart';
class StoryLoadingPage extends StatefulWidget {
/// Selected story elements from the generator modal
final List<String> characters;
final List<String> scenes;
final List<String> props;
const StoryLoadingPage({
super.key,
this.characters = const [],
this.scenes = const [],
this.props = const [],
});
@override
State<StoryLoadingPage> createState() => _StoryLoadingPageState();
}
class _StoryLoadingPageState extends State<StoryLoadingPage> {
static const String _kServerBase = 'http://localhost:3000';
double _progress = 0.0;
String _loadingText = '正在收集灵感碎片...';
bool _hasError = false;
@override
void initState() {
super.initState();
_generateStory();
}
Future<void> _generateStory() async {
try {
// ── Start SSE request ──
final request = http.Request(
'POST',
Uri.parse('$_kServerBase/api/create_story'),
);
request.headers['Content-Type'] = 'application/json';
request.body = jsonEncode({
'characters': widget.characters,
'scenes': widget.scenes,
'props': widget.props,
});
final client = http.Client();
final response = await client.send(request).timeout(
const Duration(seconds: 180),
);
if (response.statusCode != 200) {
_showError('服务器响应异常 (${response.statusCode})');
client.close();
return;
}
// ── Parse SSE stream ──
String buffer = '';
String? storyTitle;
String? storyContent;
await for (final chunk in response.stream.transform(utf8.decoder)) {
buffer += chunk;
while (buffer.contains('\n\n')) {
final idx = buffer.indexOf('\n\n');
final line = buffer.substring(0, idx).trim();
buffer = buffer.substring(idx + 2);
if (!line.startsWith('data: ')) continue;
final jsonStr = line.substring(6);
try {
final event = jsonDecode(jsonStr) as Map<String, dynamic>;
final stage = event['stage'] as String? ?? '';
final progress = (event['progress'] as num?)?.toDouble() ?? 0;
final message = event['message'] as String? ?? '';
if (!mounted) return;
switch (stage) {
case 'connecting':
_updateProgress(progress / 100, '正在收集灵感碎片...');
break;
case 'generating':
_updateProgress(progress / 100, '故事正在诞生...');
break;
case 'parsing':
_updateProgress(progress / 100, '正在编制最后的魔法...');
break;
case 'done':
storyTitle = event['title'] as String? ?? '卡皮巴拉的故事';
storyContent = event['content'] as String? ?? '';
_updateProgress(1.0, '大功告成!');
break;
case 'error':
_showError(message.isNotEmpty ? message : '故事生成失败,请重试');
client.close();
return;
}
} catch (e) {
debugPrint('SSE parse error: $e');
}
}
}
client.close();
// ── Navigate to story detail ──
if (!mounted) return;
if (storyTitle != null && storyContent != null && storyContent.isNotEmpty) {
// Brief pause to show "大功告成!"
await Future.delayed(const Duration(milliseconds: 600));
if (!mounted) return;
final result = await Navigator.of(context).push<dynamic>(
MaterialPageRoute(
builder: (context) => StoryDetailPage(
mode: StoryMode.generated,
story: {
'title': storyTitle,
'content': storyContent,
},
),
),
);
// Pass the story data back to DeviceControlPage
if (mounted) {
if (result == 'saved') {
Navigator.of(context).pop({
'action': 'saved',
'title': storyTitle,
'content': storyContent,
});
} else {
Navigator.of(context).pop(result);
}
}
} else {
_showError('AI 返回了空故事,请重试');
}
} catch (e) {
debugPrint('Story generation error: $e');
if (mounted) {
_showError('网络开小差了,再试一次~');
}
}
}
void _updateProgress(double progress, String text) {
if (!mounted) return;
setState(() {
_progress = progress.clamp(0.0, 1.0);
_loadingText = text;
});
}
void _showError(String message) {
if (!mounted) return;
setState(() {
_hasError = true;
_loadingText = message;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFFDF9F3),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Image
Image.asset(
'assets/www/kapi_writing.png',
width: 200,
height: 200,
errorBuilder: (c, e, s) => const Icon(
Icons.edit_note,
size: 100,
color: Color(0xFFD1D5DB),
),
),
const SizedBox(height: 32),
// Text
Text(
_loadingText,
style: const TextStyle(
fontSize: 18,
color: Color(0xFF4B2404),
fontWeight: FontWeight.w600,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
// Progress Bar
Container(
width: 280,
height: 12,
decoration: BoxDecoration(
color: const Color(0xFFC99672).withOpacity(0.2),
borderRadius: BorderRadius.circular(6),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(6),
child: FractionallySizedBox(
alignment: Alignment.centerLeft,
widthFactor: _progress.clamp(0.0, 1.0),
child: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xFFECCFA8), Color(0xFFC99672)],
),
),
),
),
),
),
// Retry button (shown on error)
if (_hasError) ...[
const SizedBox(height: 32),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text(
'返回重试',
style: TextStyle(
fontSize: 16,
color: Color(0xFFC99672),
fontWeight: FontWeight.w600,
),
),
),
],
],
),
),
);
}
}