некоторые правки

This commit is contained in:
Галина Федоренко 2024-12-21 19:21:05 +04:00
parent e031ca847e
commit 937649c4fb
8 changed files with 33 additions and 105 deletions

View File

@ -2,6 +2,16 @@ import 'package:json_annotation/json_annotation.dart';
part 'thrones_character_dto.g.dart'; part 'thrones_character_dto.g.dart';
@JsonSerializable(createToJson: false)
class ThronesCharacterListDto {
final List<ThronesCharacterDto> characters;
ThronesCharacterListDto({required this.characters});
factory ThronesCharacterListDto.fromJson(Map<String, dynamic> json) =>
_$ThronesCharacterListDtoFromJson(json);
}
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class ThronesCharacterDto { class ThronesCharacterDto {
final int id; final int id;

View File

@ -6,6 +6,14 @@ part of 'thrones_character_dto.dart';
// JsonSerializableGenerator // JsonSerializableGenerator
// ************************************************************************** // **************************************************************************
ThronesCharacterListDto _$ThronesCharacterListDtoFromJson(
Map<String, dynamic> json) =>
ThronesCharacterListDto(
characters: (json['characters'] as List<dynamic>)
.map((e) => ThronesCharacterDto.fromJson(e as Map<String, dynamic>))
.toList(),
);
ThronesCharacterDto _$ThronesCharacterDtoFromJson(Map<String, dynamic> json) => ThronesCharacterDto _$ThronesCharacterDtoFromJson(Map<String, dynamic> json) =>
ThronesCharacterDto( ThronesCharacterDto(
id: (json['id'] as num).toInt(), id: (json['id'] as num).toInt(),

View File

@ -7,14 +7,14 @@ const _imagePlaceholder =
extension ThronesCharacterDtoToModel on ThronesCharacterDto { extension ThronesCharacterDtoToModel on ThronesCharacterDto {
CardData toDomain() => CardData( CardData toDomain() => CardData(
descriptionText: _makeDescriptionText(title, family), text: _makeDescriptionText(title, family),
firstName: firstName, firstName: firstName,
lastName: lastName, lastName: lastName,
title: title, title: title,
family: family, family: family,
fullName: fullName, fullName: fullName,
imageUrl: imageUrl, imageUrl: imageUrl,
text: '', id: id.toString()
); );
String _makeDescriptionText(String? title, String? family) { String _makeDescriptionText(String? title, String? family) {

View File

@ -1,75 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_app/data/repositories/api_interface.dart';
import 'package:flutter_app/domain/models/card.dart';
// class MockRepository extends ApiInterface {
// @override
// Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
// return [
// CardData(
// fullName: 'Daenerys Targaryen',
// descriptionText: 'Mother of Dragons',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/daenerys.jpg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Samwell Tarly',
// descriptionText: 'Maester',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/sam.jpg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Jon Snow',
// descriptionText: 'King of the North',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/jon-snow.jpg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Arya Stark',
// descriptionText: 'No One',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/arya-stark.jpg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Sansa Stark',
// descriptionText: 'Lady of Winterfell',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/sansa-stark.jpeg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Brandon Stark',
// descriptionText: 'Lord of Winterfell',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/bran-stark.jpg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Ned Stark',
// descriptionText: 'Lord of Winterfell',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/ned-stark.jpg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// CardData(
// fullName: 'Robert Baratheon',
// descriptionText: 'Lord of the Seven Kingdoms',
// icon: Icons.favorite,
// imageUrl:
// 'https://thronesapi.com/assets/images/robert-baratheon.jpeg',
// text: '', firstName: '', lastName: '', title: '', family: '',
// ),
// ];
// }
// }

View File

@ -1,21 +1,15 @@
import 'package:flutter/material.dart';
class CardData { class CardData {
final String text; final String text;
final String descriptionText;
final IconData icon;
final String? imageUrl; final String? imageUrl;
final String? firstName; final String? firstName;
final String? lastName; final String? lastName;
final String? fullName; final String? fullName;
final String? title; final String? title;
final String? family; final String? family;
final int? id; final String? id;
CardData({ CardData({
required this.text, required this.text,
required this.descriptionText,
this.icon = Icons.people,
this.imageUrl, this.imageUrl,
required this.firstName, required this.firstName,
required this.lastName, required this.lastName,

View File

@ -42,7 +42,7 @@ class DetailsPage extends StatelessWidget {
), ),
), ),
Text( Text(
data.descriptionText, data.text,
style: Theme.of(context).textTheme.bodyLarge?.copyWith( style: Theme.of(context).textTheme.bodyLarge?.copyWith(
fontSize: 20.0, fontSize: 20.0,
fontFamily: 'Times New Roman', fontFamily: 'Times New Roman',

View File

@ -4,9 +4,7 @@ typedef OnLikeCallback = void Function(String? id, String title, bool isLiked)?;
class _Card extends StatelessWidget { class _Card extends StatelessWidget {
final String text; final String text;
final String descriptionText;
final String? fullName; final String? fullName;
final IconData icon;
final String? imageUrl; final String? imageUrl;
final OnLikeCallback onLike; final OnLikeCallback onLike;
final VoidCallback? onTap; final VoidCallback? onTap;
@ -15,8 +13,6 @@ class _Card extends StatelessWidget {
const _Card( const _Card(
this.text, { this.text, {
this.icon = Icons.catching_pokemon,
required this.descriptionText,
required this.fullName, required this.fullName,
this.imageUrl, this.imageUrl,
this.onLike, this.onLike,
@ -33,9 +29,7 @@ class _Card extends StatelessWidget {
}) => }) =>
_Card( _Card(
data.text, data.text,
descriptionText: data.descriptionText,
fullName: data.fullName, fullName: data.fullName,
icon: data.icon,
imageUrl: data.imageUrl, imageUrl: data.imageUrl,
onLike: onLike, onLike: onLike,
onTap: onTap, onTap: onTap,
@ -77,7 +71,7 @@ class _Card extends StatelessWidget {
), ),
), ),
Text( Text(
descriptionText, text,
style: Theme.of(context).textTheme.bodyMedium, style: Theme.of(context).textTheme.bodyMedium,
), ),
], ],
@ -86,14 +80,11 @@ class _Card extends StatelessWidget {
Align( Align(
alignment: Alignment.bottomRight, alignment: Alignment.bottomRight,
child: Padding( child: Padding(
padding: const EdgeInsets.only( padding:
left: 8.0, const EdgeInsets.only(left: 8, right: 16, bottom: 16),
right: 16.0, child: GestureDetector(
bottom: 16.0, onTap: () => onLike?.call(id, fullName!, isLiked),
), child: AnimatedSwitcher(
child: GestureDetector(
onTap: () => onLike?.call(id, text, isLiked),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300), duration: const Duration(milliseconds: 300),
child: isLiked child: isLiked
? const Icon( ? const Icon(

View File

@ -14,20 +14,20 @@ class LikeBloc extends Bloc<LikeEvent, LikeState> {
on<LoadLikesEvent>(_onLoadLikes); on<LoadLikesEvent>(_onLoadLikes);
} }
FutureOr<void> _onLoadLikes(LoadLikesEvent event, Emitter<LikeState> emit) async { Future<void> _onLoadLikes(LoadLikesEvent event, Emitter<LikeState> emit) async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
final data = prefs.getStringList(_likedPrefsKey); final data = prefs.getStringList(_likedPrefsKey);
emit(state.copyWith(likedIds: data)); emit(state.copyWith(likedIds: data));
} }
FutureOr<void> _onChangeLike(ChangeLikeEvent event, Emitter<LikeState> emit) async{ Future<void> _onChangeLike(ChangeLikeEvent event, Emitter<LikeState> emit) async {
final updatedList = List<String>.from(state.likedIds ?? []); final updatedList = List<String>.from(state.likedIds ?? []);
if (updatedList.contains(event.id.toString())) { if (updatedList.contains(event.id)) {
updatedList.remove(event.id.toString()); updatedList.remove(event.id);
} else { } else {
updatedList.add(event.id.toString()); updatedList.add(event.id);
} }
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();