теперь с кривой пагинацией

This commit is contained in:
gg12 darfren 2024-10-08 20:48:39 +04:00
parent 224097153a
commit f1d02a3b0f
6 changed files with 14 additions and 35 deletions

View File

@ -6,11 +6,15 @@ part 'character_dto.g.dart';
class CharactersDto { class CharactersDto {
@JsonKey(name: 'characters') @JsonKey(name: 'characters')
final List<CharacterDataDto>? data; final List<CharacterDataDto>? data;
final MetaDto? meta; final int? currentPage;
final int? pageSize;
final int? total;
const CharactersDto({ const CharactersDto({
this.data, this.data,
this.meta, this.currentPage,
this.pageSize,
this.total,
}); });
factory CharactersDto.fromJson(Map<String, dynamic> json) => factory CharactersDto.fromJson(Map<String, dynamic> json) =>
@ -35,25 +39,9 @@ class CharacterDataDto {
class CharacterDataAttributesDto { class CharacterDataAttributesDto {
final String? birthdate; final String? birthdate;
final String? sex; final String? sex;
final String? clan;
const CharacterDataAttributesDto({this.birthdate, this.sex, this.clan}); const CharacterDataAttributesDto({this.birthdate, this.sex});
factory CharacterDataAttributesDto.fromJson(Map<String, dynamic> json) => factory CharacterDataAttributesDto.fromJson(Map<String, dynamic> json) =>
_$CharacterDataAttributesDtoFromJson(json); _$CharacterDataAttributesDtoFromJson(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

@ -9,7 +9,7 @@ const _imagePlaceHolder =
extension CharactersDtoToModel on CharactersDto { extension CharactersDtoToModel on CharactersDto {
HomeData toDomain() => HomeData( HomeData toDomain() => HomeData(
data: data?.map((e) => e.toDomain()).toList(), data: data?.map((e) => e.toDomain()).toList(),
nextPage: meta?.pagination?.next, nextPage: (currentPage! * pageSize! < total! ? currentPage! + 1 : null),
); );
} }
@ -17,13 +17,11 @@ extension CharacterDataDtoToModel on CharacterDataDto {
CardData toDomain() => CardData( CardData toDomain() => CardData(
name ?? 'Unknown', name ?? 'Unknown',
_makeDescriptionText(), _makeDescriptionText(),
clan: 'clan: ${attributes != null ? (attributes?.clan ?? 'unknown') : 'unknown'}',
imageUrl: '${(images != null && images!.length > 0) ? (images?[0] ?? _imagePlaceHolder) : _imagePlaceHolder}' imageUrl: '${(images != null && images!.length > 0) ? (images?[0] ?? _imagePlaceHolder) : _imagePlaceHolder}'
); );
String _makeDescriptionText() { String _makeDescriptionText() {
return 'birthdate: ${attributes != null ? (attributes?.birthdate ?? 'unknown') : 'unknown'}\n' return 'birthdate: ${attributes != null ? (attributes?.birthdate ?? 'unknown') : 'unknown'}\n'
'sex: ${attributes != null ? (attributes?.sex ?? 'unknown') : 'unknown'}\n' 'sex: ${attributes != null ? (attributes?.sex ?? 'unknown') : 'unknown'}\n';
'clan: ${attributes != null ? (attributes?.clan ?? 'unknown') : 'unknown'}';
} }
} }

View File

@ -3,13 +3,11 @@ import 'package:flutter/material.dart';
class CardData { class CardData {
final String name; final String name;
final String descriptionText; final String descriptionText;
final String clan;
final String imageUrl; final String imageUrl;
CardData( CardData(
this.name, this.name,
this.descriptionText,{ this.descriptionText,{
required this.imageUrl, required this.imageUrl,
required this.clan,
}); });
} }

View File

@ -5,7 +5,6 @@ typedef OnLikeCall = void Function(String Title, bool isLiked);
class _Card extends StatefulWidget { class _Card extends StatefulWidget {
final String name; final String name;
final String descriptionText; final String descriptionText;
final String clan;
final String imageUrl; final String imageUrl;
final Color color; final Color color;
final OnLikeCall? onLike; final OnLikeCall? onLike;
@ -14,7 +13,6 @@ class _Card extends StatefulWidget {
const _Card( const _Card(
this.name, this.name,
this.descriptionText, { this.descriptionText, {
required this.clan,
required this.imageUrl, required this.imageUrl,
this.color = Colors.white70, this.color = Colors.white70,
this.onLike, this.onLike,
@ -29,7 +27,6 @@ class _Card extends StatefulWidget {
_Card( _Card(
data.name, data.name,
data.descriptionText, data.descriptionText,
clan: data.clan,
imageUrl: data.imageUrl, imageUrl: data.imageUrl,
color: Colors.white70, color: Colors.white70,
onLike: onLike, onLike: onLike,
@ -106,10 +103,6 @@ class _CardState extends State<_Card> {
widget.name, widget.name,
style: Theme.of(context).textTheme.headlineLarge, style: Theme.of(context).textTheme.headlineLarge,
), ),
Text(
widget.clan,
style: Theme.of(context).textTheme.bodyLarge,
),
], ],
), ),
), ),

View File

@ -72,7 +72,7 @@ class _BodyState extends State<_Body> {
} }
void _onNextPageListener() { void _onNextPageListener() {
if (scrollController.offset > scrollController.position.maxScrollExtent) { if (scrollController.offset >= scrollController.position.maxScrollExtent) {
// preventing multiple pagination request on multiple swipes // preventing multiple pagination request on multiple swipes
final bloc = context.read<HomeBloc>(); final bloc = context.read<HomeBloc>();
if (!bloc.state.isPaginationLoading) { if (!bloc.state.isPaginationLoading) {
@ -87,6 +87,7 @@ class _BodyState extends State<_Body> {
@override @override
void dispose() { void dispose() {
searchController.dispose(); searchController.dispose();
scrollController.dispose();
super.dispose(); super.dispose();
} }

View File

@ -20,9 +20,10 @@ class NarutoRepository extends ApiInterface {
Future<HomeData?> loadData({ Future<HomeData?> loadData({
OnErrorCallback? onError, OnErrorCallback? onError,
String? q, String? q,
int page = 1, int? page = 1,
int pageSize = 8, int pageSize = 8,
}) async { }) async {
const String url = '$_baseUrl/characters'; const String url = '$_baseUrl/characters';
try { try {
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>( final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(