почти слэй но пагинация не работает

This commit is contained in:
Полина Чубыкина 2024-11-20 00:12:00 +04:00
parent 89b3891506
commit 9c16de318d
7 changed files with 121 additions and 90 deletions

View File

@ -4,16 +4,12 @@ part 'pokemon_dto.g.dart';
@JsonSerializable(createToJson: false)
class PokemonDto {
final List<PokemonDataDto>? results;
final int? count;
final String? next;
final String? previous;
final List<PokemonDataDto>? data;
final MetaDto? meta;
const PokemonDto({
this.results,
this.count,
this.next,
this.previous,
this.data,
this.meta,
});
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
@ -21,46 +17,61 @@ class PokemonDto {
@JsonSerializable(createToJson: false)
class PokemonDataDto {
final String? id;
final String? name;
final String? url;
final List<String>? types;
final List<String>? evolvesTo;
final List<PokemonAbilityDto>? abilities;
final PokemonImagesDto? images;
const PokemonDataDto({
this.id,
this.name,
this.url,
this.types,
this.evolvesTo,
this.abilities,
this.images,
});
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonDetailsDto {
final int? height;
final int? weight;
final List<AbilityDto>? abilities;
const PokemonDetailsDto({
this.height,
this.weight,
this.abilities,
});
factory PokemonDetailsDto.fromJson(Map<String, dynamic> json) => _$PokemonDetailsDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class AbilityDto {
final AbilityDetailDto? ability;
const AbilityDto({this.ability});
factory AbilityDto.fromJson(Map<String, dynamic> json) => _$AbilityDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class AbilityDetailDto {
class PokemonAbilityDto {
final String? name;
final String? text;
final String? type;
const AbilityDetailDto({this.name});
const PokemonAbilityDto({this.name, this.text, this.type});
factory AbilityDetailDto.fromJson(Map<String, dynamic> json) => _$AbilityDetailDtoFromJson(json);
factory PokemonAbilityDto.fromJson(Map<String, dynamic> json) => _$PokemonAbilityDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonImagesDto {
final String? large;
const PokemonImagesDto({this.large});
factory PokemonImagesDto.fromJson(Map<String, dynamic> json) => _$PokemonImagesDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class MetaDto {
final PaginationDto? pagination;
const MetaDto({this.pagination});
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PaginationDto {
final int? current;
final int? next;
final int? last;
const PaginationDto({this.current, this.next, this.last});
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
}

View File

@ -7,36 +7,52 @@ part of 'pokemon_dto.dart';
// **************************************************************************
PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
results: (json['results'] as List<dynamic>?)
data: (json['data'] as List<dynamic>?)
?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
count: (json['count'] as num?)?.toInt(),
next: json['next'] as String?,
previous: json['previous'] as String?,
meta: json['meta'] == null
? null
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
);
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
PokemonDataDto(
id: json['id'] as String?,
name: json['name'] as String?,
url: json['url'] as String?,
);
PokemonDetailsDto _$PokemonDetailsDtoFromJson(Map<String, dynamic> json) =>
PokemonDetailsDto(
height: (json['height'] as num?)?.toInt(),
weight: (json['weight'] as num?)?.toInt(),
abilities: (json['abilities'] as List<dynamic>?)
?.map((e) => AbilityDto.fromJson(e as Map<String, dynamic>))
types:
(json['types'] as List<dynamic>?)?.map((e) => e as String).toList(),
evolvesTo: (json['evolvesTo'] as List<dynamic>?)
?.map((e) => e as String)
.toList(),
);
AbilityDto _$AbilityDtoFromJson(Map<String, dynamic> json) => AbilityDto(
ability: json['ability'] == null
abilities: (json['abilities'] as List<dynamic>?)
?.map((e) => PokemonAbilityDto.fromJson(e as Map<String, dynamic>))
.toList(),
images: json['images'] == null
? null
: AbilityDetailDto.fromJson(json['ability'] as Map<String, dynamic>),
: PokemonImagesDto.fromJson(json['images'] as Map<String, dynamic>),
);
AbilityDetailDto _$AbilityDetailDtoFromJson(Map<String, dynamic> json) =>
AbilityDetailDto(
PokemonAbilityDto _$PokemonAbilityDtoFromJson(Map<String, dynamic> json) =>
PokemonAbilityDto(
name: json['name'] as String?,
text: json['text'] as String?,
type: json['type'] as String?,
);
PokemonImagesDto _$PokemonImagesDtoFromJson(Map<String, dynamic> json) =>
PokemonImagesDto(
large: json['large'] as String?,
);
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
pagination: json['pagination'] == null
? null
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
);
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
PaginationDto(
current: (json['current'] as num?)?.toInt(),
next: (json['next'] as num?)?.toInt(),
last: (json['last'] as num?)?.toInt(),
);

View File

@ -1,34 +1,39 @@
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
import 'package:mobilki_lab1/domain/models/card.dart';
import 'package:mobilki_lab1/domain/models/home.dart';
import 'package:flutter/material.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension PokemonDtoToModel on PokemonDto {
HomeData toDomain() => HomeData(
data: results?.map((e) => e.toDomain()).toList(),
nextPage: next != null ? int.tryParse(next!.split('=')[1].split('&')[0]) : null,
data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next,
);
}
extension PokemonDataDtoToModel on PokemonDataDto {
CardData toDomain() {
final id = url?.split('/')[6] ?? 'UNKNOWN';
final imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png';
return CardData(
CardData toDomain() => CardData(
name ?? 'UNKNOWN',
imageUrl: imageUrl,
descriptionText: 'ID: $id',
imageUrl: images?.large ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(types, evolvesTo, abilities),
id: id,
);
}
}
extension PokemonDetailsDtoToModel on PokemonDetailsDto {
String toDescriptionText() {
final abilitiesText = abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN';
return 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} kg, Abilities: $abilitiesText';
String _makeDescriptionText(
List<String>? types,
List<String>? evolvesTo,
List<PokemonAbilityDto>? abilities,
) {
final typeText = types != null && types.isNotEmpty ? 'Types: ${types.join(", ")}' : '';
final evolvesToText = evolvesTo != null && evolvesTo.isNotEmpty ? 'Evolves To: ${evolvesTo.join(", ")}' : '';
final abilitiesText = abilities != null && abilities.isNotEmpty ? 'Abilities: ${abilities.map((a) => a.name).join(", ")}' : '';
return [
typeText,
evolvesToText,
abilitiesText,
].where((text) => text.isNotEmpty).join('\n');
}
}

View File

@ -5,33 +5,32 @@ import 'package:mobilki_lab1/data/repositories/api_interface.dart';
import 'package:mobilki_lab1/domain/models/home.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class PokeRepository extends ApiInterface {
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';
static const String _baseUrl = 'https://api.pokemontcg.io/v2';
@override
Future<HomeData?> loadData({
OnErrorCallback? onError,
String? q, // Добавляем параметр для поискового запроса
String? q,
int page = 1,
int pageSize = 25,
int pageSize = 10,
}) async {
try {
final String url = '$_baseUrl/pokemon';
final String url = '$_baseUrl/cards';
final Map<String, dynamic> queryParameters = {
'offset': (page - 1) * pageSize,
'limit': pageSize,
'page': page,
'pageSize': pageSize,
};
// Если есть поисковый запрос, добавляем его в параметры запроса
if (q != null && q.isNotEmpty) {
queryParameters['name'] = q;
if (q != null) {
queryParameters['q'] = 'name:$q*';
}
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(

View File

@ -35,15 +35,15 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: RepositoryProvider<PotterRepository>(
home: RepositoryProvider<PokemonRepository>(
lazy: true,
create: (_) => PotterRepository(),
create: (_) => PokemonRepository(),
child: BlocProvider<LikeBloc>(
lazy: false,
create: (context) => LikeBloc(),
child: BlocProvider<HomeBloc>(
lazy: false,
create: (context) => HomeBloc(context.read<PotterRepository>()),
create: (context) => HomeBloc(context.read<PokemonRepository>()),
child: const MyHomePage(title: 'Чубыкина Полина Павловна'),
),
),

View File

@ -6,8 +6,8 @@ import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
import '../../../data/repositories/pokemon_repository.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final PotterRepository repo;
// final PokeRepository repo;
// final PotterRepository repo;
final PokemonRepository repo;
HomeBloc(this.repo) : super(const HomeState()) {
on<HomeLoadDataEvent>(_onLoadData);

View File

@ -63,7 +63,7 @@ class BodyState extends State<Body> {
}
void _onNextPageListener() {
if (scrollController.offset > scrollController.position.maxScrollExtent) {
if (scrollController.offset >= scrollController.position.maxScrollExtent) {
final bloc = context.read<HomeBloc>();
if (!bloc.state.isPaginationLoading) {
bloc.add(HomeLoadDataEvent(