fix:优化使用常用技术框架
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
import '../../data/api/report_data_source.dart';
|
||||
import '../../data/models/models.dart';
|
||||
@@ -11,7 +13,7 @@ import '../../widgets/sheets.dart';
|
||||
import '../../widgets/states.dart';
|
||||
import 'modules/renderer_registry.dart';
|
||||
|
||||
class ReportDetailPage extends StatefulWidget {
|
||||
class ReportDetailPage extends HookConsumerWidget {
|
||||
const ReportDetailPage({
|
||||
required this.reportId,
|
||||
required this.dataSource,
|
||||
@@ -38,108 +40,138 @@ class ReportDetailPage extends StatefulWidget {
|
||||
final VoidCallback? onSpeed;
|
||||
|
||||
@override
|
||||
State<ReportDetailPage> createState() => _ReportDetailPageState();
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final retryCount = useState(0);
|
||||
final detailFuture = useMemoized(
|
||||
() => dataSource.reportDetail(reportId),
|
||||
[dataSource, reportId, retryCount.value],
|
||||
);
|
||||
final snapshot = useFuture(detailFuture);
|
||||
const registry = ModuleRendererRegistry();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('研报详情')),
|
||||
body: snapshot.connectionState != ConnectionState.done
|
||||
? const LoadingState()
|
||||
: snapshot.hasError
|
||||
? ErrorState(
|
||||
message: snapshot.error.toString(),
|
||||
onRetry: () => retryCount.value++,
|
||||
)
|
||||
: _ReportDetailContent(
|
||||
detail: snapshot.data!,
|
||||
dataSource: dataSource,
|
||||
player: player,
|
||||
onStartAudio: onStartAudio,
|
||||
onToggleAudio: onToggleAudio,
|
||||
onSeekAudio: onSeekAudio,
|
||||
onSpeed: onSpeed,
|
||||
registry: registry,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ReportDetailPageState extends State<ReportDetailPage> {
|
||||
static const registry = ModuleRendererRegistry();
|
||||
late Future<ReportDetail> future = widget.dataSource.reportDetail(
|
||||
widget.reportId,
|
||||
);
|
||||
class _ReportDetailContent extends StatelessWidget {
|
||||
const _ReportDetailContent({
|
||||
required this.detail,
|
||||
required this.dataSource,
|
||||
required this.player,
|
||||
required this.registry,
|
||||
this.onStartAudio,
|
||||
this.onToggleAudio,
|
||||
this.onSeekAudio,
|
||||
this.onSpeed,
|
||||
});
|
||||
|
||||
final ReportDetail detail;
|
||||
final ReportDataSource dataSource;
|
||||
final PlayerStateModel player;
|
||||
final ModuleRendererRegistry registry;
|
||||
final void Function(
|
||||
String audioId,
|
||||
String reportId,
|
||||
String title,
|
||||
int durationSec,
|
||||
)?
|
||||
onStartAudio;
|
||||
final VoidCallback? onToggleAudio;
|
||||
final void Function(int delta)? onSeekAudio;
|
||||
final VoidCallback? onSpeed;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('研报详情')),
|
||||
body: FutureBuilder<ReportDetail>(
|
||||
future: future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const LoadingState();
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return ErrorState(
|
||||
message: snapshot.error.toString(),
|
||||
onRetry: () => setState(
|
||||
() => future = widget.dataSource.reportDetail(widget.reportId),
|
||||
),
|
||||
);
|
||||
}
|
||||
final detail = snapshot.data!;
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(WiseSpacing.x4),
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(WiseSpacing.x4),
|
||||
children: [
|
||||
AppCard(
|
||||
color: WiseColors.secondary200,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
AppCard(
|
||||
color: WiseColors.secondary200,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Wrap(
|
||||
spacing: WiseSpacing.x2,
|
||||
runSpacing: WiseSpacing.x2,
|
||||
children: [
|
||||
AppBadge(
|
||||
text: detail.interpretationLabel,
|
||||
kind: BadgeKind.brand,
|
||||
),
|
||||
if (detail.hasAudio)
|
||||
const AppBadge(
|
||||
text: '音频',
|
||||
icon: Icons.graphic_eq,
|
||||
kind: BadgeKind.audio,
|
||||
),
|
||||
AppBadge(
|
||||
text: asString(detail.source['source_tier']),
|
||||
icon: Icons.verified_outlined,
|
||||
kind: BadgeKind.tier,
|
||||
),
|
||||
],
|
||||
Wrap(
|
||||
spacing: WiseSpacing.x2,
|
||||
runSpacing: WiseSpacing.x2,
|
||||
children: [
|
||||
AppBadge(
|
||||
text: detail.interpretationLabel,
|
||||
kind: BadgeKind.brand,
|
||||
),
|
||||
if (detail.hasAudio)
|
||||
const AppBadge(
|
||||
text: '音频',
|
||||
icon: Icons.graphic_eq,
|
||||
kind: BadgeKind.audio,
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
Text(
|
||||
detail.titleCn,
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
if (detail.oneLiner.isNotEmpty) ...[
|
||||
const SizedBox(height: WiseSpacing.x2),
|
||||
Text(
|
||||
detail.oneLiner,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
],
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
Text(
|
||||
'${detail.institution.nameCn} · ${formatDate(detail.releasedAt)}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
AppBadge(
|
||||
text: asString(detail.source['source_tier']),
|
||||
icon: Icons.verified_outlined,
|
||||
kind: BadgeKind.tier,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
_ActionBar(detail: detail),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
_Toc(modules: detail.modules),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
for (final module in detail.modules) ...[
|
||||
registry.card(
|
||||
context: context,
|
||||
module: module,
|
||||
report: detail,
|
||||
dataSource: widget.dataSource,
|
||||
player: widget.player,
|
||||
onStartAudio: widget.onStartAudio,
|
||||
onToggleAudio: widget.onToggleAudio,
|
||||
onSeekAudio: widget.onSeekAudio,
|
||||
onSpeed: widget.onSpeed,
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
Text(
|
||||
detail.titleCn,
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
if (detail.oneLiner.isNotEmpty) ...[
|
||||
const SizedBox(height: WiseSpacing.x2),
|
||||
Text(
|
||||
detail.oneLiner,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
],
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
Text(
|
||||
'${detail.institution.nameCn} · ${formatDate(detail.releasedAt)}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
_ActionBar(detail: detail),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
_Toc(modules: detail.modules),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
for (final module in detail.modules) ...[
|
||||
registry.card(
|
||||
context: context,
|
||||
module: module,
|
||||
report: detail,
|
||||
dataSource: dataSource,
|
||||
player: player,
|
||||
onStartAudio: onStartAudio,
|
||||
onToggleAudio: onToggleAudio,
|
||||
onSeekAudio: onSeekAudio,
|
||||
onSpeed: onSpeed,
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x4),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -158,7 +190,8 @@ class _ActionBar extends StatelessWidget {
|
||||
label: '收藏',
|
||||
icon: Icons.favorite_border,
|
||||
kind: AppButtonKind.ghost,
|
||||
onPressed: () => showLoginSheet(context, reason: '登录后保存到你的收藏'),
|
||||
onPressed: () =>
|
||||
showLoginSheet(context, reason: '登录后保存到你的收藏'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: WiseSpacing.x2),
|
||||
|
||||
Reference in New Issue
Block a user