Лаба 7, починила лайки

This commit is contained in:
a.puchkina 2024-10-17 20:21:38 +04:00
parent d707eff870
commit 77ed129792
4 changed files with 8 additions and 12 deletions

View File

@ -1,6 +1,6 @@
part of 'home_page.dart';
typedef OnLikeCallBack = void Function(int? id, String title, bool isLiked)?;
typedef OnLikeCallBack = void Function(String? id, String title, bool isLiked)?;
class _Card extends StatelessWidget {
final String text;
@ -9,7 +9,7 @@ class _Card extends StatelessWidget {
final String? imageUrl;
final OnLikeCallBack onLike;
final VoidCallback? onTap;
final int? id;
final String? id;
final bool isLiked;
const _Card(this.text, {
@ -35,7 +35,7 @@ class _Card extends StatelessWidget {
onLike: onLike,
onTap: onTap,
isLiked: isLiked,
id: data.id,
id: data.id.toString()
);

View File

@ -147,7 +147,7 @@ class _BodyState extends State<Body> {
? _Card.fromData(
data,
onLike: _onLike,
isLiked: likeState.likedIds?.contains(data.id) == true,
isLiked: likeState.likedIds?.contains(data.id.toString()) == true,
onTap: () => _navToDetails(context, data),
)
: const SizedBox.shrink();
@ -186,7 +186,7 @@ class _BodyState extends State<Body> {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'$title ${isLiked ? context.locale.liked : context.locale.disliked}',
'$title ${isLiked ? context.locale.disliked : context.locale.liked}',
style: Theme.of(context).textTheme.bodyLarge,
),
backgroundColor: Color(0xFFFFFFFF),
@ -195,11 +195,10 @@ class _BodyState extends State<Body> {
});
}
void _onLike(int? id, String title, bool isLiked) {
void _onLike(String? id, String title, bool isLiked) {
if (id != null){
context.read<LikeBloc>().add(ChangeLikeEvent(id));
_showSnackBar(context, title, isLiked);
}
}
}

View File

@ -22,10 +22,7 @@ class LikeBloc extends Bloc<LikeEvent, LikeState> {
}
FutureOr<void> _onChangeLike(ChangeLikeEvent event, Emitter<LikeState> emit) async{
final updatedListInt = List<int>.from(state.likedIds ?? []);
print(updatedListInt);
final updatedList = updatedListInt.map((int number) => number.toString()).toList();
print(updatedList);
final updatedList = List<String>.from(state.likedIds ?? []);
if (updatedList.contains(event.id.toString())) {
updatedList.remove(event.id.toString());

View File

@ -7,7 +7,7 @@ class LoadLikesEvent extends LikeEvent {
}
class ChangeLikeEvent extends LikeEvent {
final int id;
final String id;
const ChangeLikeEvent(this.id);
}