подгружаются картинки, лайки поставить нельзя...
This commit is contained in:
parent
d2b4355e37
commit
28e44f4703
@ -4,16 +4,18 @@ part 'pokemon_dto.g.dart';
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PokemonDto {
|
||||
final int id;
|
||||
final String name;
|
||||
final List<PokemonTypeDto> types;
|
||||
final PokemonSpritesDto sprites;
|
||||
final String? name;
|
||||
final int? baseExperience;
|
||||
final List<PokemonTypeDto>? types;
|
||||
final List<PokemonAbilityDto>? abilities;
|
||||
final PokemonSpritesDto? sprites;
|
||||
|
||||
const PokemonDto({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.types,
|
||||
required this.sprites,
|
||||
this.name,
|
||||
this.baseExperience,
|
||||
this.types,
|
||||
this.abilities,
|
||||
this.sprites,
|
||||
});
|
||||
|
||||
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
||||
@ -21,29 +23,42 @@ class PokemonDto {
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PokemonTypeDto {
|
||||
final int slot;
|
||||
final PokemonTypeDetailDto type;
|
||||
final int? slot;
|
||||
final TypeDto? type;
|
||||
|
||||
const PokemonTypeDto({
|
||||
required this.slot,
|
||||
required this.type,
|
||||
});
|
||||
const PokemonTypeDto({this.slot, this.type});
|
||||
|
||||
factory PokemonTypeDto.fromJson(Map<String, dynamic> json) => _$PokemonTypeDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PokemonTypeDetailDto {
|
||||
final String name;
|
||||
final String url;
|
||||
class TypeDto {
|
||||
final String? name;
|
||||
final String? url;
|
||||
|
||||
const PokemonTypeDetailDto({
|
||||
required this.name,
|
||||
required this.url,
|
||||
});
|
||||
const TypeDto({this.name, this.url});
|
||||
|
||||
factory PokemonTypeDetailDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$PokemonTypeDetailDtoFromJson(json);
|
||||
factory TypeDto.fromJson(Map<String, dynamic> json) => _$TypeDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PokemonAbilityDto {
|
||||
final int? slot;
|
||||
final AbilityDto? ability;
|
||||
|
||||
const PokemonAbilityDto({this.slot, this.ability});
|
||||
|
||||
factory PokemonAbilityDto.fromJson(Map<String, dynamic> json) => _$PokemonAbilityDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class AbilityDto {
|
||||
final String? name;
|
||||
final String? url;
|
||||
|
||||
const AbilityDto({this.name, this.url});
|
||||
|
||||
factory AbilityDto.fromJson(Map<String, dynamic> json) => _$AbilityDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
@ -54,6 +69,5 @@ class PokemonSpritesDto {
|
||||
required this.front_default,
|
||||
});
|
||||
|
||||
factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$PokemonSpritesDtoFromJson(json);
|
||||
}
|
||||
factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) => _$PokemonSpritesDtoFromJson(json);
|
||||
}
|
@ -7,26 +7,43 @@ part of 'pokemon_dto.dart';
|
||||
// **************************************************************************
|
||||
|
||||
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>))
|
||||
name: json['name'] as String?,
|
||||
baseExperience: (json['baseExperience'] as num?)?.toInt(),
|
||||
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>),
|
||||
abilities: (json['abilities'] as List<dynamic>?)
|
||||
?.map((e) => PokemonAbilityDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
sprites: json['sprites'] == null
|
||||
? null
|
||||
: 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>),
|
||||
slot: (json['slot'] as num?)?.toInt(),
|
||||
type: json['type'] == null
|
||||
? null
|
||||
: TypeDto.fromJson(json['type'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
PokemonTypeDetailDto _$PokemonTypeDetailDtoFromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
PokemonTypeDetailDto(
|
||||
name: json['name'] as String,
|
||||
url: json['url'] as String,
|
||||
TypeDto _$TypeDtoFromJson(Map<String, dynamic> json) => TypeDto(
|
||||
name: json['name'] as String?,
|
||||
url: json['url'] as String?,
|
||||
);
|
||||
|
||||
PokemonAbilityDto _$PokemonAbilityDtoFromJson(Map<String, dynamic> json) =>
|
||||
PokemonAbilityDto(
|
||||
slot: (json['slot'] as num?)?.toInt(),
|
||||
ability: json['ability'] == null
|
||||
? null
|
||||
: AbilityDto.fromJson(json['ability'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
AbilityDto _$AbilityDtoFromJson(Map<String, dynamic> json) => AbilityDto(
|
||||
name: json['name'] as String?,
|
||||
url: json['url'] as String?,
|
||||
);
|
||||
|
||||
PokemonSpritesDto _$PokemonSpritesDtoFromJson(Map<String, dynamic> json) =>
|
||||
|
@ -1,17 +1,25 @@
|
||||
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';
|
||||
|
||||
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),
|
||||
);
|
||||
HomeData toDomain() => HomeData(
|
||||
data: [toCardData()],
|
||||
nextPage: null,
|
||||
);
|
||||
|
||||
String _makeDescriptionText(List<PokemonTypeDto> types) {
|
||||
return 'Types: ${types.map((type) => type.type.name).join(', ')}';
|
||||
CardData toCardData() => CardData(
|
||||
name ?? 'UNKNOWN',
|
||||
descriptionText: _makeDescriptionText(types, abilities),
|
||||
imageUrl: sprites?.front_default ?? _imagePlaceholder,
|
||||
);
|
||||
|
||||
String _makeDescriptionText(List<PokemonTypeDto>? types, List<PokemonAbilityDto>? abilities) {
|
||||
final typeNames = types?.map((e) => e.type?.name).whereType<String>().join(', ') ?? '';
|
||||
final abilityNames = abilities?.map((e) => e.ability?.name).whereType<String>().join(', ') ?? '';
|
||||
return 'Types: $typeNames\nAbilities: $abilityNames';
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1,56 @@
|
||||
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/home.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
|
||||
class PokeRepository 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<HomeData?> loadData({
|
||||
OnErrorCallback? onError,
|
||||
String? q,
|
||||
int page = 1,
|
||||
int pageSize = 25,
|
||||
}) async {
|
||||
try {
|
||||
final String url = '$_baseUrl/pokemon';
|
||||
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||
url,
|
||||
queryParameters: {
|
||||
'offset': (page - 1) * pageSize,
|
||||
'limit': pageSize,
|
||||
},
|
||||
);
|
||||
|
||||
final Map<String, dynamic> data = response.data as Map<String, dynamic>;
|
||||
final List<dynamic> results = data['results'] as List<dynamic>;
|
||||
final List<PokemonDto> pokemons = await Future.wait(
|
||||
results.map((result) async {
|
||||
final String pokemonUrl = result['url'] as String;
|
||||
final Response<dynamic> pokemonResponse = await _dio.get<Map<dynamic, dynamic>>(pokemonUrl);
|
||||
return PokemonDto.fromJson(pokemonResponse.data as Map<String, dynamic>);
|
||||
}),
|
||||
);
|
||||
|
||||
final HomeData homeData = HomeData(
|
||||
data: pokemons.map((pokemon) => pokemon.toCardData()).toList(),
|
||||
nextPage: page + 1,
|
||||
);
|
||||
|
||||
return homeData;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e.error?.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import 'package:mobilki_lab1/presentation/like_bloc/like_bloc.dart';
|
||||
import 'package:mobilki_lab1/presentation/locale_bloc/locale_bloc.dart';
|
||||
import 'package:mobilki_lab1/presentation/locale_bloc/locale_state.dart';
|
||||
import 'components/locale/l10n/app_locale.dart';
|
||||
import 'data/repositories/pokemon_repository.dart';
|
||||
import 'data/repositories/potter_repository.dart';
|
||||
|
||||
void main() {
|
||||
@ -34,15 +35,15 @@ class MyApp extends StatelessWidget {
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: RepositoryProvider<PotterRepository>(
|
||||
home: RepositoryProvider<PokeRepository>(
|
||||
lazy: true,
|
||||
create: (_) => PotterRepository(),
|
||||
create: (_) => PokeRepository(),
|
||||
child: BlocProvider<LikeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => LikeBloc(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||
create: (context) => HomeBloc(context.read<PokeRepository>()),
|
||||
child: const MyHomePage(title: 'Чубыкина Полина Павловна'),
|
||||
),
|
||||
),
|
||||
|
@ -1,10 +1,12 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:mobilki_lab1/data/repositories/pokemon_repository.dart';
|
||||
import 'package:mobilki_lab1/data/repositories/potter_repository.dart';
|
||||
import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
|
||||
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
||||
|
||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||
final PotterRepository repo;
|
||||
//final PotterRepository repo;
|
||||
final PokeRepository repo;
|
||||
|
||||
HomeBloc(this.repo) : super(const HomeState()) {
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
|
Loading…
x
Reference in New Issue
Block a user