fix: resolve flutter analyze errors - remove leaked code, fix method calls, cleanup imports
This commit is contained in:
@@ -1,43 +1,257 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../../app/router/route_names.dart';
|
||||
import '../../../core/models/novel_model.dart';
|
||||
import '../../bookshelf/providers/bookshelf_provider.dart';
|
||||
import '../providers/novels_provider.dart';
|
||||
|
||||
class NovelDetailScreen extends StatelessWidget {
|
||||
class NovelDetailScreen extends ConsumerWidget {
|
||||
const NovelDetailScreen({super.key, required this.novelId});
|
||||
|
||||
final String novelId;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final novelAsync = ref.watch(novelDetailProvider(novelId));
|
||||
final chaptersAsync = ref.watch(chapterListProvider(novelId));
|
||||
final isBookmarked = ref.watch(isBookmarkedProvider(novelId));
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Chi tiet truyện')),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
children: [
|
||||
Text('Novel ID: ${novelId.isEmpty ? '(missing)' : novelId}'),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'Khung chi tiet truyện: metadata, series, chapter list, rating, bookmark, recommendation, comments.',
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Wrap(
|
||||
spacing: 10,
|
||||
runSpacing: 10,
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: () => context.push('${RouteNames.reader}?chapterId=1'),
|
||||
child: const Text('Doc chuong 1'),
|
||||
body: novelAsync.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (e, _) => Center(child: Text('Lỗi: $e')),
|
||||
data: (novel) => CustomScrollView(
|
||||
slivers: [
|
||||
_NovelAppBar(novel: novel, isBookmarked: isBookmarked, onBookmark: () {
|
||||
ref.read(bookshelfProvider.notifier).toggle(novelId);
|
||||
}),
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Genre chips
|
||||
if (novel.genres.isNotEmpty)
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 4,
|
||||
children: novel.genres
|
||||
.map((g) => Chip(
|
||||
label: Text(g.name),
|
||||
padding: EdgeInsets.zero,
|
||||
labelPadding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
// Description
|
||||
if (novel.description != null)
|
||||
Text(novel.description!, style: Theme.of(context).textTheme.bodyMedium),
|
||||
const SizedBox(height: 16),
|
||||
// Stats row
|
||||
_StatsRow(novel: novel),
|
||||
const SizedBox(height: 16),
|
||||
// Read button
|
||||
chaptersAsync.when(
|
||||
loading: () => const SizedBox.shrink(),
|
||||
error: (_, __) => const SizedBox.shrink(),
|
||||
data: (chapters) {
|
||||
if (chapters.isEmpty) return const SizedBox.shrink();
|
||||
final first = chapters.first;
|
||||
return FilledButton.icon(
|
||||
onPressed: () => context.push(
|
||||
RouteNames.readerChapter(first.id),
|
||||
),
|
||||
icon: const Icon(Icons.menu_book),
|
||||
label: Text(
|
||||
'Đọc Chương ${first.number}: ${first.title}',
|
||||
),
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size.fromHeight(48),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
// Chapter list header
|
||||
Row(
|
||||
children: [
|
||||
Text('Danh sách chương', style: Theme.of(context).textTheme.titleMedium),
|
||||
const Spacer(),
|
||||
chaptersAsync.whenOrNull(
|
||||
data: (chapters) => Text(
|
||||
'${chapters.length} chương',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
) ?? const SizedBox.shrink(),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
OutlinedButton(
|
||||
onPressed: () =>
|
||||
context.push('${RouteNames.comments}?novelId=$novelId'),
|
||||
child: const Text('Xem binh luan'),
|
||||
),
|
||||
// Chapter list
|
||||
chaptersAsync.when(
|
||||
loading: () => const SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
error: (_, __) => const SliverToBoxAdapter(child: SizedBox.shrink()),
|
||||
data: (chapters) => SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
final ch = chapters[index];
|
||||
return ListTile(
|
||||
dense: true,
|
||||
title: Text(
|
||||
'Chương ${ch.number}: ${ch.title}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onTap: () => context.push(RouteNames.readerChapter(ch.id)),
|
||||
);
|
||||
},
|
||||
childCount: chapters.length,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SliverToBoxAdapter(child: SizedBox(height: 24)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _NovelAppBar extends StatelessWidget {
|
||||
final NovelModel novel;
|
||||
final bool isBookmarked;
|
||||
final VoidCallback onBookmark;
|
||||
|
||||
const _NovelAppBar({
|
||||
required this.novel,
|
||||
required this.isBookmarked,
|
||||
required this.onBookmark,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SliverAppBar(
|
||||
expandedHeight: 280,
|
||||
pinned: true,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(isBookmarked ? Icons.bookmark : Icons.bookmark_outline),
|
||||
onPressed: onBookmark,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.comment_outlined),
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => Scaffold(
|
||||
appBar: AppBar(title: const Text('Bình luận')),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
background: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
if (novel.coverUrl != null)
|
||||
CachedNetworkImage(
|
||||
imageUrl: novel.coverUrl!,
|
||||
fit: BoxFit.cover,
|
||||
)
|
||||
else
|
||||
Container(color: Theme.of(context).colorScheme.primaryContainer),
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Colors.transparent, Colors.black.withAlpha(200)],
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
bottom: 16,
|
||||
left: 16,
|
||||
right: 16,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
novel.title,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
if (novel.authorName.isNotEmpty)
|
||||
Text(
|
||||
novel.authorName,
|
||||
style: const TextStyle(color: Colors.white70, fontSize: 14),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _StatsRow extends StatelessWidget {
|
||||
final NovelModel novel;
|
||||
const _StatsRow({required this.novel});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Wrap(
|
||||
spacing: 16,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
if (novel.rating > 0)
|
||||
_Stat(icon: Icons.star, value: novel.rating.toStringAsFixed(1)),
|
||||
if (novel.views > 0)
|
||||
_Stat(icon: Icons.visibility, value: _formatNum(novel.views)),
|
||||
if (novel.latestChapter != null)
|
||||
_Stat(icon: Icons.library_books, value: 'Ch. ${novel.latestChapter!.number}'),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _formatNum(int n) {
|
||||
if (n >= 1000000) return '${(n / 1000000).toStringAsFixed(1)}M';
|
||||
if (n >= 1000) return '${(n / 1000).toStringAsFixed(1)}K';
|
||||
return n.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class _Stat extends StatelessWidget {
|
||||
final IconData icon;
|
||||
final String value;
|
||||
const _Stat({required this.icon, required this.value});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(icon, size: 16, color: Theme.of(context).colorScheme.secondary),
|
||||
const SizedBox(width: 4),
|
||||
Text(value, style: Theme.of(context).textTheme.bodySmall),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../core/models/novel_model.dart';
|
||||
import '../../../core/models/chapter_model.dart';
|
||||
import '../../../core/network/providers.dart';
|
||||
|
||||
// ─── Browse / Search ──────────────────────────────────────────────────────────
|
||||
|
||||
class BrowseParams {
|
||||
final String? query;
|
||||
final String? genre;
|
||||
final String? status;
|
||||
final String sort;
|
||||
final int page;
|
||||
|
||||
const BrowseParams({
|
||||
this.query,
|
||||
this.genre,
|
||||
this.status,
|
||||
this.sort = 'latest',
|
||||
this.page = 1,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toQueryParams() => {
|
||||
if (query != null && query!.isNotEmpty) 'q': query,
|
||||
if (genre != null) 'genre': genre,
|
||||
if (status != null) 'status': status,
|
||||
'sort': sort,
|
||||
'page': page.toString(),
|
||||
'limit': '20',
|
||||
};
|
||||
|
||||
BrowseParams copyWith({
|
||||
String? query,
|
||||
String? genre,
|
||||
String? status,
|
||||
String? sort,
|
||||
int? page,
|
||||
bool clearQuery = false,
|
||||
bool clearGenre = false,
|
||||
bool clearStatus = false,
|
||||
}) =>
|
||||
BrowseParams(
|
||||
query: clearQuery ? null : query ?? this.query,
|
||||
genre: clearGenre ? null : genre ?? this.genre,
|
||||
status: clearStatus ? null : status ?? this.status,
|
||||
sort: sort ?? this.sort,
|
||||
page: page ?? this.page,
|
||||
);
|
||||
}
|
||||
|
||||
class BrowseResult {
|
||||
final List<NovelModel> items;
|
||||
final int totalCount;
|
||||
final int totalPages;
|
||||
final int currentPage;
|
||||
|
||||
const BrowseResult({
|
||||
required this.items,
|
||||
required this.totalCount,
|
||||
required this.totalPages,
|
||||
required this.currentPage,
|
||||
});
|
||||
}
|
||||
|
||||
class NovelsNotifier extends StateNotifier<AsyncValue<BrowseResult>> {
|
||||
final Ref _ref;
|
||||
BrowseParams _params = const BrowseParams();
|
||||
|
||||
NovelsNotifier(this._ref) : super(const AsyncValue.loading()) {
|
||||
fetch();
|
||||
}
|
||||
|
||||
BrowseParams get params => _params;
|
||||
|
||||
Future<void> fetch({BrowseParams? params}) async {
|
||||
if (params != null) _params = params;
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final client = _ref.read(apiClientProvider);
|
||||
final res = await client.dio.get('/api/novels/browse', queryParameters: _params.toQueryParams());
|
||||
final data = res.data as Map<String, dynamic>;
|
||||
state = AsyncValue.data(BrowseResult(
|
||||
items: (data['items'] as List).map((e) => NovelModel.fromJson(e as Map<String, dynamic>)).toList(),
|
||||
totalCount: data['totalCount'] as int,
|
||||
totalPages: data['totalPages'] as int,
|
||||
currentPage: data['currentPage'] as int,
|
||||
));
|
||||
} catch (e, st) {
|
||||
state = AsyncValue.error(e, st);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateParams(BrowseParams params) => fetch(params: params);
|
||||
}
|
||||
|
||||
final novelsProvider = StateNotifierProvider<NovelsNotifier, AsyncValue<BrowseResult>>((ref) {
|
||||
return NovelsNotifier(ref);
|
||||
});
|
||||
|
||||
// ─── Novel Detail ─────────────────────────────────────────────────────────────
|
||||
|
||||
final novelDetailProvider =
|
||||
FutureProvider.family<NovelModel, String>((ref, idOrSlug) async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final res = await client.dio.get('/api/novels/$idOrSlug');
|
||||
return NovelModel.fromJson(res.data as Map<String, dynamic>);
|
||||
});
|
||||
|
||||
// ─── Chapter List ─────────────────────────────────────────────────────────────
|
||||
|
||||
final chapterListProvider =
|
||||
FutureProvider.family<List<ChapterListItem>, String>((ref, novelId) async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final res = await client.dio.get('/api/truyen/$novelId/chapters');
|
||||
final data = res.data as Map<String, dynamic>;
|
||||
final chapters = data['chapters'] as List? ?? [];
|
||||
return chapters
|
||||
.map((e) => ChapterListItem.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
});
|
||||
Reference in New Issue
Block a user