прикол с покемонами

This commit is contained in:
Полина Чубыкина 2024-10-14 21:25:43 +04:00
parent c9736aff2d
commit 8e74f46465
5 changed files with 156 additions and 2 deletions

View File

@ -0,0 +1,57 @@
import 'package:json_annotation/json_annotation.dart';
part 'pokemon_dto.g.dart';
@JsonSerializable(createToJson: false)
class PokemonDto {
final int id;
final String name;
final List<PokemonTypeDto> types;
final PokemonSpritesDto sprites;
const PokemonDto({
required this.id,
required this.name,
required this.types,
required this.sprites,
});
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonTypeDto {
final int slot;
final PokemonTypeDetailDto type;
const PokemonTypeDto({
required this.slot,
required this.type,
});
factory PokemonTypeDto.fromJson(Map<String, dynamic> json) => _$PokemonTypeDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonTypeDetailDto {
final String name;
final String url;
const PokemonTypeDetailDto({
required this.name,
required this.url,
});
factory PokemonTypeDetailDto.fromJson(Map<String, dynamic> json) => _$PokemonTypeDetailDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonSpritesDto {
final String front_default;
const PokemonSpritesDto({
required this.front_default,
});
factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) => _$PokemonSpritesDtoFromJson(json);
}

View File

@ -0,0 +1,35 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'pokemon_dto.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
id: (json['id'] as num).toInt(),
name: json['name'] as String,
types: (json['types'] as List<dynamic>)
.map((e) => PokemonTypeDto.fromJson(e as Map<String, dynamic>))
.toList(),
sprites:
PokemonSpritesDto.fromJson(json['sprites'] as Map<String, dynamic>),
);
PokemonTypeDto _$PokemonTypeDtoFromJson(Map<String, dynamic> json) =>
PokemonTypeDto(
slot: (json['slot'] as num).toInt(),
type: PokemonTypeDetailDto.fromJson(json['type'] as Map<String, dynamic>),
);
PokemonTypeDetailDto _$PokemonTypeDetailDtoFromJson(
Map<String, dynamic> json) =>
PokemonTypeDetailDto(
name: json['name'] as String,
url: json['url'] as String,
);
PokemonSpritesDto _$PokemonSpritesDtoFromJson(Map<String, dynamic> json) =>
PokemonSpritesDto(
front_default: json['front_default'] as String,
);

View File

@ -0,0 +1,17 @@
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
import 'package:mobilki_lab1/domain/models/card.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension PokemonDtoToModel on PokemonDto {
CardData toDomain() => CardData(
name,
imageUrl: sprites.front_default ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(types),
);
String _makeDescriptionText(List<PokemonTypeDto> types) {
return 'Types: ${types.map((type) => type.type.name).join(', ')}';
}
}

View File

@ -0,0 +1,43 @@
import 'package:dio/dio.dart';
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
import 'package:mobilki_lab1/data/mappers/pokemon_mapper.dart';
import 'package:mobilki_lab1/data/repositories/api_interface.dart';
import 'package:mobilki_lab1/domain/models/card.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class PokemonRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://pokeapi.co/api/v2';
@override
Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
try {
final String url = '$_baseUrl/pokemon';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: q != null ? {'name': q} : null,
);
final List<dynamic> results = response.data['results'] as List<dynamic>;
final List<CardData> data = [];
for (final result in results) {
final String pokemonUrl = result['url'] as String;
final Response<dynamic> pokemonResponse = await _dio.get<Map<dynamic, dynamic>>(pokemonUrl);
final PokemonDto pokemonDto = PokemonDto.fromJson(pokemonResponse.data as Map<String, dynamic>);
data.add(pokemonDto.toDomain());
}
return data;
} on DioException catch (e) {
onError?.call(e.response?.statusMessage);
return null;
}
}
}

View File

@ -1,6 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobilki_lab1/data/repositories/potter_repository.dart';
import 'package:mobilki_lab1/data/repositories/pokemon_repository.dart';
import 'package:mobilki_lab1/domain/models/card.dart';
import 'package:mobilki_lab1/presentation/details_page/details_page.dart';
@ -33,7 +34,8 @@ class _BodyState extends State<Body> {
final searchController = TextEditingController();
late Future<List<CardData>?> data;
final repo = PotterRepository();
final repo = PokemonRepository();
//final repo = PotterRepository();
@override
void initState() {
@ -97,7 +99,7 @@ class _BodyState extends State<Body> {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'$title ${isLiked ? 'liked!' : 'disliked :('}',
'${isLiked ? 'Вам понравился $title!' : 'Вам больше не нравится $title :('}',
style: Theme.of(context).textTheme.bodyLarge,
),
backgroundColor: Colors.orangeAccent,