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,91 +13,131 @@ import '../../widgets/sheets.dart';
|
||||
import '../../widgets/states.dart';
|
||||
import '../shared/report_card_widget.dart';
|
||||
|
||||
class InstitutionDetailPage extends StatefulWidget {
|
||||
const InstitutionDetailPage({required this.institutionId, required this.dataSource, super.key});
|
||||
class InstitutionDetailPage extends HookConsumerWidget {
|
||||
const InstitutionDetailPage({
|
||||
required this.institutionId,
|
||||
required this.dataSource,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final String institutionId;
|
||||
final ReportDataSource dataSource;
|
||||
|
||||
@override
|
||||
State<InstitutionDetailPage> createState() => _InstitutionDetailPageState();
|
||||
}
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final retryCount = useState(0);
|
||||
final future = useMemoized(
|
||||
() => dataSource.institutionDetail(institutionId),
|
||||
[dataSource, institutionId, retryCount.value],
|
||||
);
|
||||
final snapshot = useFuture(future);
|
||||
|
||||
class _InstitutionDetailPageState extends State<InstitutionDetailPage> {
|
||||
late Future<Institution> future = widget.dataSource.institutionDetail(widget.institutionId);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('机构主页')),
|
||||
body: FutureBuilder<Institution>(
|
||||
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.institutionDetail(widget.institutionId)));
|
||||
final item = snapshot.data!;
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(WiseSpacing.x4),
|
||||
children: [
|
||||
AppCard(
|
||||
color: WiseColors.secondary200,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(item.nameCn, style: Theme.of(context).textTheme.headlineSmall),
|
||||
if (item.nameEn.isNotEmpty) Text(item.nameEn, style: Theme.of(context).textTheme.bodyMedium),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
Wrap(
|
||||
spacing: WiseSpacing.x2,
|
||||
runSpacing: WiseSpacing.x2,
|
||||
children: [
|
||||
AppBadge(text: item.sourceTier, icon: Icons.verified_outlined, kind: BadgeKind.tier),
|
||||
AppBadge(text: '${item.reportCount} 份研报', kind: BadgeKind.brand),
|
||||
for (final topic in item.coveredTopics) AppBadge(text: topic),
|
||||
],
|
||||
),
|
||||
],
|
||||
body: snapshot.connectionState != ConnectionState.done
|
||||
? const LoadingState()
|
||||
: snapshot.hasError
|
||||
? ErrorState(
|
||||
message: snapshot.error.toString(),
|
||||
onRetry: () => retryCount.value++,
|
||||
)
|
||||
: _InstitutionDetailContent(
|
||||
item: snapshot.data!,
|
||||
dataSource: dataSource,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
if (item.introCn.isNotEmpty)
|
||||
AppCard(child: Text(item.introCn, style: Theme.of(context).textTheme.bodyMedium)),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
if (item.credibilityNote.isNotEmpty)
|
||||
AppCard(
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Icon(Icons.verified_user_outlined, color: WiseColors.positive),
|
||||
const SizedBox(width: WiseSpacing.x2),
|
||||
Expanded(child: Text(item.credibilityNote, style: Theme.of(context).textTheme.bodyMedium)),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x5),
|
||||
Text('最新研报', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
if (item.recentReports.isEmpty)
|
||||
const EmptyState(title: '机构暂无研报', message: '稍后再试', icon: Icons.article_outlined)
|
||||
else
|
||||
for (final report in item.recentReports) ...[
|
||||
ReportCardWidget(
|
||||
report: report,
|
||||
onTap: () => openReportDetail(context, widget.dataSource, report),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
],
|
||||
AppButton(
|
||||
label: '了解相关服务',
|
||||
icon: Icons.open_in_new,
|
||||
kind: AppButtonKind.ghost,
|
||||
expand: true,
|
||||
onPressed: () => showOutboundSheet(context, title: item.nameCn),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InstitutionDetailContent extends StatelessWidget {
|
||||
const _InstitutionDetailContent({
|
||||
required this.item,
|
||||
required this.dataSource,
|
||||
});
|
||||
|
||||
final Institution item;
|
||||
final ReportDataSource dataSource;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(WiseSpacing.x4),
|
||||
children: [
|
||||
AppCard(
|
||||
color: WiseColors.secondary200,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(item.nameCn, style: Theme.of(context).textTheme.headlineSmall),
|
||||
if (item.nameEn.isNotEmpty)
|
||||
Text(item.nameEn, style: Theme.of(context).textTheme.bodyMedium),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
Wrap(
|
||||
spacing: WiseSpacing.x2,
|
||||
runSpacing: WiseSpacing.x2,
|
||||
children: [
|
||||
AppBadge(
|
||||
text: item.sourceTier,
|
||||
icon: Icons.verified_outlined,
|
||||
kind: BadgeKind.tier,
|
||||
),
|
||||
AppBadge(
|
||||
text: '${item.reportCount} 份研报',
|
||||
kind: BadgeKind.brand,
|
||||
),
|
||||
for (final topic in item.coveredTopics) AppBadge(text: topic),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
if (item.introCn.isNotEmpty)
|
||||
AppCard(
|
||||
child: Text(item.introCn, style: Theme.of(context).textTheme.bodyMedium),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
if (item.credibilityNote.isNotEmpty)
|
||||
AppCard(
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Icon(Icons.verified_user_outlined, color: WiseColors.positive),
|
||||
const SizedBox(width: WiseSpacing.x2),
|
||||
Expanded(
|
||||
child: Text(
|
||||
item.credibilityNote,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x5),
|
||||
Text('最新研报', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
if (item.recentReports.isEmpty)
|
||||
const EmptyState(
|
||||
title: '机构暂无研报',
|
||||
message: '稍后再试',
|
||||
icon: Icons.article_outlined,
|
||||
)
|
||||
else
|
||||
for (final report in item.recentReports) ...[
|
||||
ReportCardWidget(
|
||||
report: report,
|
||||
onTap: () => openReportDetail(context, dataSource, report),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
],
|
||||
AppButton(
|
||||
label: '了解相关服务',
|
||||
icon: Icons.open_in_new,
|
||||
kind: AppButtonKind.ghost,
|
||||
expand: true,
|
||||
onPressed: () => showOutboundSheet(context, title: item.nameCn),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
import '../../data/api/report_data_source.dart';
|
||||
import '../../data/content_providers.dart';
|
||||
import '../../data/models/models.dart';
|
||||
import '../../routing/app_routes.dart';
|
||||
import '../../theme/wise_tokens.dart';
|
||||
@@ -8,36 +10,43 @@ import '../../widgets/app_card.dart';
|
||||
import '../../widgets/badges.dart';
|
||||
import '../../widgets/states.dart';
|
||||
|
||||
class InstitutionsPage extends StatefulWidget {
|
||||
class InstitutionsPage extends HookConsumerWidget {
|
||||
const InstitutionsPage({required this.dataSource, super.key});
|
||||
|
||||
final ReportDataSource dataSource;
|
||||
|
||||
@override
|
||||
State<InstitutionsPage> createState() => _InstitutionsPageState();
|
||||
}
|
||||
|
||||
class _InstitutionsPageState extends State<InstitutionsPage> {
|
||||
late Future<List<Institution>> future = widget.dataSource.institutions();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<Institution>>(
|
||||
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.institutions()));
|
||||
final items = [...snapshot.data ?? const <Institution>[]]..sort((a, b) => b.reportCount.compareTo(a.reportCount));
|
||||
if (items.isEmpty) return const EmptyState(title: '暂无机构信息', message: '稍后再试', icon: Icons.account_balance_outlined);
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final snapshot = ref.watch(institutionsProvider);
|
||||
return snapshot.when(
|
||||
loading: () => const LoadingState(),
|
||||
error: (error, _) => ErrorState(
|
||||
message: error.toString(),
|
||||
onRetry: () => ref.invalidate(institutionsProvider),
|
||||
),
|
||||
data: (items) {
|
||||
final sorted = [...items]
|
||||
..sort((a, b) => b.reportCount.compareTo(a.reportCount));
|
||||
if (sorted.isEmpty) {
|
||||
return const EmptyState(
|
||||
title: '暂无机构信息',
|
||||
message: '稍后再试',
|
||||
icon: Icons.account_balance_outlined,
|
||||
);
|
||||
}
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(WiseSpacing.x4),
|
||||
children: [
|
||||
Text('研报来源机构', style: Theme.of(context).textTheme.titleLarge),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
for (final item in items) ...[
|
||||
for (final item in sorted) ...[
|
||||
InstitutionCard(
|
||||
institution: item,
|
||||
onTap: () => openInstitutionDetail(context, widget.dataSource, item.id),
|
||||
onTap: () => openInstitutionDetail(
|
||||
context,
|
||||
dataSource,
|
||||
item.id,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x3),
|
||||
],
|
||||
@@ -56,7 +65,9 @@ class InstitutionCard extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final initials = institution.nameCn.isEmpty ? '研' : institution.nameCn.characters.take(2).toString();
|
||||
final initials = institution.nameCn.isEmpty
|
||||
? '研'
|
||||
: institution.nameCn.characters.take(2).toString();
|
||||
return AppCard(
|
||||
onTap: onTap,
|
||||
child: Row(
|
||||
@@ -66,23 +77,36 @@ class InstitutionCard extends StatelessWidget {
|
||||
radius: 25,
|
||||
backgroundColor: WiseColors.secondary200,
|
||||
foregroundColor: WiseColors.primary,
|
||||
child: Text(initials, style: const TextStyle(fontWeight: FontWeight.w800)),
|
||||
child: Text(
|
||||
initials,
|
||||
style: const TextStyle(fontWeight: FontWeight.w800),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: WiseSpacing.x3),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(institution.nameCn, style: Theme.of(context).textTheme.titleMedium),
|
||||
Text(
|
||||
institution.nameCn,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
if (institution.nameEn.isNotEmpty)
|
||||
Text(institution.nameEn, maxLines: 1, overflow: TextOverflow.ellipsis, style: Theme.of(context).textTheme.bodySmall),
|
||||
Text(
|
||||
institution.nameEn,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: WiseSpacing.x2),
|
||||
Wrap(
|
||||
spacing: WiseSpacing.x2,
|
||||
runSpacing: WiseSpacing.x2,
|
||||
children: [
|
||||
if (institution.institutionType.isNotEmpty) AppBadge(text: institution.institutionType),
|
||||
for (final topic in institution.coveredTopics.take(3)) AppBadge(text: topic, kind: BadgeKind.brand),
|
||||
if (institution.institutionType.isNotEmpty)
|
||||
AppBadge(text: institution.institutionType),
|
||||
for (final topic in institution.coveredTopics.take(3))
|
||||
AppBadge(text: topic, kind: BadgeKind.brand),
|
||||
],
|
||||
),
|
||||
],
|
||||
@@ -91,7 +115,12 @@ class InstitutionCard extends StatelessWidget {
|
||||
const SizedBox(width: WiseSpacing.x2),
|
||||
Column(
|
||||
children: [
|
||||
Text('${institution.reportCount}', style: Theme.of(context).textTheme.titleMedium?.copyWith(color: WiseColors.primary)),
|
||||
Text(
|
||||
'${institution.reportCount}',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
color: WiseColors.primary,
|
||||
),
|
||||
),
|
||||
Text('份研报', style: Theme.of(context).textTheme.bodySmall),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user