fix: resolve flutter analyze errors - remove leaked code, fix method calls, cleanup imports

This commit is contained in:
2026-03-23 16:55:54 +07:00
parent 4f202936fa
commit 71f1feaf98
33 changed files with 2851 additions and 224 deletions
@@ -1,6 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
class CommentsScreen extends StatelessWidget {
import '../../../core/models/comment_model.dart';
import '../../auth/providers/auth_provider.dart';
import '../providers/comments_provider.dart';
class CommentsScreen extends ConsumerStatefulWidget {
const CommentsScreen({
super.key,
required this.novelId,
@@ -10,29 +16,179 @@ class CommentsScreen extends StatelessWidget {
final String novelId;
final String? chapterId;
@override
ConsumerState<CommentsScreen> createState() => _CommentsScreenState();
}
class _CommentsScreenState extends ConsumerState<CommentsScreen> {
final _textCtrl = TextEditingController();
bool _submitting = false;
String get _key =>
widget.chapterId != null ? '${widget.novelId}:${widget.chapterId}' : widget.novelId;
@override
void dispose() {
_textCtrl.dispose();
super.dispose();
}
Future<void> _submit() async {
final text = _textCtrl.text.trim();
if (text.isEmpty) return;
setState(() => _submitting = true);
try {
await ref.read(commentsProvider(_key).notifier).post(text);
_textCtrl.clear();
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Lỗi: $e')));
}
} finally {
if (mounted) setState(() => _submitting = false);
}
}
@override
Widget build(BuildContext context) {
final isAuth = ref.watch(isAuthenticatedProvider);
final commentsAsync = ref.watch(commentsProvider(_key));
return Scaffold(
appBar: AppBar(title: const Text('Binh luan')),
body: ListView(
padding: const EdgeInsets.all(20),
appBar: AppBar(
title: Text(widget.chapterId != null ? 'Bình luận chương' : 'Bình luận'),
),
body: Column(
children: [
Text('Novel ID: ${novelId.isEmpty ? '(missing)' : novelId}'),
Text('Chapter ID: ${chapterId ?? '(all novel comments)'}'),
const SizedBox(height: 12),
const Text('Khung danh sach binh luan + form gui comment + phan trang.'),
const SizedBox(height: 20),
TextField(
maxLines: 4,
decoration: const InputDecoration(
hintText: 'Viet binh luan cua ban...',
border: OutlineInputBorder(),
Expanded(
child: commentsAsync.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (e, _) => Center(child: Text('Lỗi: $e')),
data: (comments) {
if (comments.isEmpty) {
return const Center(child: Text('Chưa có bình luận nào'));
}
return ListView.separated(
padding: const EdgeInsets.symmetric(vertical: 8),
itemCount: comments.length,
separatorBuilder: (_, __) => const Divider(height: 1),
itemBuilder: (context, index) =>
_CommentTile(comment: comments[index]),
);
},
),
),
const SizedBox(height: 12),
FilledButton(
onPressed: () {},
child: const Text('Gui binh luan'),
if (isAuth)
_CommentInput(
controller: _textCtrl,
submitting: _submitting,
onSubmit: _submit,
),
],
),
);
}
}
class _CommentTile extends StatelessWidget {
final CommentModel comment;
const _CommentTile({required this.comment});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
CircleAvatar(
radius: 16,
child: Text(
comment.username[0].toUpperCase(),
),
),
const SizedBox(width: 8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
comment.username,
style: const TextStyle(fontWeight: FontWeight.w600),
),
Text(
_formatDate(comment.createdAt),
style: Theme.of(context).textTheme.bodySmall,
),
],
),
),
],
),
const SizedBox(height: 8),
Text(comment.content),
],
),
);
}
String _formatDate(DateTime? dt) {
if (dt == null) return '';
return DateFormat('dd/MM/yyyy HH:mm').format(dt.toLocal());
}
}
class _CommentInput extends StatelessWidget {
final TextEditingController controller;
final bool submitting;
final VoidCallback onSubmit;
const _CommentInput({
required this.controller,
required this.submitting,
required this.onSubmit,
});
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.only(
left: 12,
right: 12,
top: 8,
bottom: MediaQuery.of(context).padding.bottom + 8,
),
color: Theme.of(context).colorScheme.surface,
child: Row(
children: [
Expanded(
child: TextField(
controller: controller,
maxLines: 3,
minLines: 1,
decoration: InputDecoration(
hintText: 'Viết bình luận...',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
isDense: true,
),
textInputAction: TextInputAction.newline,
),
),
const SizedBox(width: 8),
IconButton.filled(
onPressed: submitting ? null : onSubmit,
icon: submitting
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.send),
),
],
),
@@ -0,0 +1,72 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/models/comment_model.dart';
import '../../../core/network/providers.dart';
class CommentsNotifier extends StateNotifier<AsyncValue<List<CommentModel>>> {
final Ref _ref;
final String novelId;
final String? chapterId;
int _page = 1;
bool _hasMore = true;
CommentsNotifier(this._ref, {required this.novelId, this.chapterId})
: super(const AsyncValue.loading()) {
fetch();
}
bool get hasMore => _hasMore;
Future<void> fetch({bool reset = false}) async {
if (reset) {
_page = 1;
_hasMore = true;
state = const AsyncValue.loading();
}
try {
final client = _ref.read(apiClientProvider);
final queryParams = <String, dynamic>{
'page': _page.toString(),
'limit': '20',
if (chapterId != null) 'chapterId': chapterId,
};
final res = await client.dio.get('/api/truyen/$novelId/comments', queryParameters: queryParams);
final data = res.data as Map<String, dynamic>;
final newItems = (data['comments'] as List)
.map((e) => CommentModel.fromJson(e as Map<String, dynamic>))
.toList();
final totalPages = data['totalPages'] as int? ?? 1;
_hasMore = _page < totalPages;
final existing = reset ? <CommentModel>[] : (state.valueOrNull ?? []);
state = AsyncValue.data([...existing, ...newItems]);
} catch (e, st) {
state = AsyncValue.error(e, st);
}
}
Future<void> loadMore() async {
if (!_hasMore) return;
_page++;
await fetch();
}
Future<void> post(String content) async {
final client = _ref.read(apiClientProvider);
final res = await client.dio.post('/api/truyen/$novelId/comments', data: {
'content': content,
if (chapterId != null) 'chapterId': chapterId,
});
final newComment = CommentModel.fromJson(res.data as Map<String, dynamic>);
state = AsyncValue.data([newComment, ...(state.valueOrNull ?? [])]);
}
}
// Provider family params: "novelId" or "novelId:chapterId"
final commentsProvider = StateNotifierProvider.family<CommentsNotifier,
AsyncValue<List<CommentModel>>, String>((ref, key) {
final parts = key.split(':');
return CommentsNotifier(
ref,
novelId: parts[0],
chapterId: parts.length > 1 ? parts[1] : null,
);
});