подгружаются картинки, лайки поставить нельзя...
This commit is contained in:
parent
d2b4355e37
commit
28e44f4703
@ -4,16 +4,18 @@ part 'pokemon_dto.g.dart';
|
|||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class PokemonDto {
|
class PokemonDto {
|
||||||
final int id;
|
final String? name;
|
||||||
final String name;
|
final int? baseExperience;
|
||||||
final List<PokemonTypeDto> types;
|
final List<PokemonTypeDto>? types;
|
||||||
final PokemonSpritesDto sprites;
|
final List<PokemonAbilityDto>? abilities;
|
||||||
|
final PokemonSpritesDto? sprites;
|
||||||
|
|
||||||
const PokemonDto({
|
const PokemonDto({
|
||||||
required this.id,
|
this.name,
|
||||||
required this.name,
|
this.baseExperience,
|
||||||
required this.types,
|
this.types,
|
||||||
required this.sprites,
|
this.abilities,
|
||||||
|
this.sprites,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
||||||
@ -21,29 +23,42 @@ class PokemonDto {
|
|||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class PokemonTypeDto {
|
class PokemonTypeDto {
|
||||||
final int slot;
|
final int? slot;
|
||||||
final PokemonTypeDetailDto type;
|
final TypeDto? type;
|
||||||
|
|
||||||
const PokemonTypeDto({
|
const PokemonTypeDto({this.slot, this.type});
|
||||||
required this.slot,
|
|
||||||
required this.type,
|
|
||||||
});
|
|
||||||
|
|
||||||
factory PokemonTypeDto.fromJson(Map<String, dynamic> json) => _$PokemonTypeDtoFromJson(json);
|
factory PokemonTypeDto.fromJson(Map<String, dynamic> json) => _$PokemonTypeDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class PokemonTypeDetailDto {
|
class TypeDto {
|
||||||
final String name;
|
final String? name;
|
||||||
final String url;
|
final String? url;
|
||||||
|
|
||||||
const PokemonTypeDetailDto({
|
const TypeDto({this.name, this.url});
|
||||||
required this.name,
|
|
||||||
required this.url,
|
|
||||||
});
|
|
||||||
|
|
||||||
factory PokemonTypeDetailDto.fromJson(Map<String, dynamic> json) =>
|
factory TypeDto.fromJson(Map<String, dynamic> json) => _$TypeDtoFromJson(json);
|
||||||
_$PokemonTypeDetailDtoFromJson(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)
|
@JsonSerializable(createToJson: false)
|
||||||
@ -54,6 +69,5 @@ class PokemonSpritesDto {
|
|||||||
required this.front_default,
|
required this.front_default,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) =>
|
factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) => _$PokemonSpritesDtoFromJson(json);
|
||||||
_$PokemonSpritesDtoFromJson(json);
|
|
||||||
}
|
}
|
@ -7,26 +7,43 @@ part of 'pokemon_dto.dart';
|
|||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
|
PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
|
||||||
id: (json['id'] as num).toInt(),
|
name: json['name'] as String?,
|
||||||
name: json['name'] as String,
|
baseExperience: (json['baseExperience'] as num?)?.toInt(),
|
||||||
types: (json['types'] as List<dynamic>)
|
types: (json['types'] as List<dynamic>?)
|
||||||
.map((e) => PokemonTypeDto.fromJson(e as Map<String, dynamic>))
|
?.map((e) => PokemonTypeDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
sprites:
|
abilities: (json['abilities'] as List<dynamic>?)
|
||||||
PokemonSpritesDto.fromJson(json['sprites'] as Map<String, 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 _$PokemonTypeDtoFromJson(Map<String, dynamic> json) =>
|
||||||
PokemonTypeDto(
|
PokemonTypeDto(
|
||||||
slot: (json['slot'] as num).toInt(),
|
slot: (json['slot'] as num?)?.toInt(),
|
||||||
type: PokemonTypeDetailDto.fromJson(json['type'] as Map<String, dynamic>),
|
type: json['type'] == null
|
||||||
|
? null
|
||||||
|
: TypeDto.fromJson(json['type'] as Map<String, dynamic>),
|
||||||
);
|
);
|
||||||
|
|
||||||
PokemonTypeDetailDto _$PokemonTypeDetailDtoFromJson(
|
TypeDto _$TypeDtoFromJson(Map<String, dynamic> json) => TypeDto(
|
||||||
Map<String, dynamic> json) =>
|
name: json['name'] as String?,
|
||||||
PokemonTypeDetailDto(
|
url: json['url'] as String?,
|
||||||
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) =>
|
PokemonSpritesDto _$PokemonSpritesDtoFromJson(Map<String, dynamic> json) =>
|
||||||
|
@ -1,17 +1,25 @@
|
|||||||
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
|
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
|
||||||
import 'package:mobilki_lab1/domain/models/card.dart';
|
import 'package:mobilki_lab1/domain/models/card.dart';
|
||||||
|
import 'package:mobilki_lab1/domain/models/home.dart';
|
||||||
|
|
||||||
const _imagePlaceholder =
|
const _imagePlaceholder =
|
||||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||||
|
|
||||||
extension PokemonDtoToModel on PokemonDto {
|
extension PokemonDtoToModel on PokemonDto {
|
||||||
CardData toDomain() => CardData(
|
HomeData toDomain() => HomeData(
|
||||||
name,
|
data: [toCardData()],
|
||||||
imageUrl: sprites.front_default ?? _imagePlaceholder,
|
nextPage: null,
|
||||||
descriptionText: _makeDescriptionText(types),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
String _makeDescriptionText(List<PokemonTypeDto> types) {
|
CardData toCardData() => CardData(
|
||||||
return 'Types: ${types.map((type) => type.type.name).join(', ')}';
|
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_bloc.dart';
|
||||||
import 'package:mobilki_lab1/presentation/locale_bloc/locale_state.dart';
|
import 'package:mobilki_lab1/presentation/locale_bloc/locale_state.dart';
|
||||||
import 'components/locale/l10n/app_locale.dart';
|
import 'components/locale/l10n/app_locale.dart';
|
||||||
|
import 'data/repositories/pokemon_repository.dart';
|
||||||
import 'data/repositories/potter_repository.dart';
|
import 'data/repositories/potter_repository.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -34,15 +35,15 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: RepositoryProvider<PotterRepository>(
|
home: RepositoryProvider<PokeRepository>(
|
||||||
lazy: true,
|
lazy: true,
|
||||||
create: (_) => PotterRepository(),
|
create: (_) => PokeRepository(),
|
||||||
child: BlocProvider<LikeBloc>(
|
child: BlocProvider<LikeBloc>(
|
||||||
lazy: false,
|
lazy: false,
|
||||||
create: (context) => LikeBloc(),
|
create: (context) => LikeBloc(),
|
||||||
child: BlocProvider<HomeBloc>(
|
child: BlocProvider<HomeBloc>(
|
||||||
lazy: false,
|
lazy: false,
|
||||||
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
create: (context) => HomeBloc(context.read<PokeRepository>()),
|
||||||
child: const MyHomePage(title: 'Чубыкина Полина Павловна'),
|
child: const MyHomePage(title: 'Чубыкина Полина Павловна'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
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/data/repositories/potter_repository.dart';
|
||||||
import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
|
import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
|
||||||
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
||||||
|
|
||||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
final PotterRepository repo;
|
//final PotterRepository repo;
|
||||||
|
final PokeRepository repo;
|
||||||
|
|
||||||
HomeBloc(this.repo) : super(const HomeState()) {
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
on<HomeLoadDataEvent>(_onLoadData);
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user