Compare commits
No commits in common. "bebc4de8afb7a1e5308d8dccba4a5bccc98bee1e" and "d2237434ee6f834369fd7f9de30459162d6b431d" have entirely different histories.
bebc4de8af
...
d2237434ee
5
Makefile
5
Makefile
@ -4,6 +4,11 @@ gen:
|
|||||||
icon:
|
icon:
|
||||||
flutter pub run flutter_launcher_icons:main
|
flutter pub run flutter_launcher_icons:main
|
||||||
|
|
||||||
|
hello:
|
||||||
|
echo "Hi!"; \
|
||||||
|
echo "I`m makefile"; \
|
||||||
|
echo "^_^"
|
||||||
|
|
||||||
init_res:
|
init_res:
|
||||||
dart pub global activate flutter_asset_generator
|
dart pub global activate flutter_asset_generator
|
||||||
|
|
||||||
|
53
lib/data/dtos/characters_dto.dart
Normal file
53
lib/data/dtos/characters_dto.dart
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
|
part 'characters_dto.g.dart';
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class CharactersDto {
|
||||||
|
final List<CharactersDataDto>? data;
|
||||||
|
final MetaDto? meta;
|
||||||
|
const CharactersDto({this.data, this.meta});
|
||||||
|
factory CharactersDto.fromJson(Map<String, dynamic> json) => _$CharactersDtoFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class CharactersDataDto {
|
||||||
|
final String? id;
|
||||||
|
final String? type;
|
||||||
|
final CharasterAttributesDataDto? attributes;
|
||||||
|
const CharactersDataDto({this.id, this.type, this.attributes});
|
||||||
|
factory CharactersDataDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$CharactersDataDtoFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class CharasterAttributesDataDto {
|
||||||
|
final String? name;
|
||||||
|
final String? born;
|
||||||
|
final String? died;
|
||||||
|
final String? image;
|
||||||
|
|
||||||
|
const CharasterAttributesDataDto({this.name, this.born, this.died, this.image});
|
||||||
|
factory CharasterAttributesDataDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$CharasterAttributesDataDtoFromJson(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);
|
||||||
|
}
|
49
lib/data/dtos/characters_dto.g.dart
Normal file
49
lib/data/dtos/characters_dto.g.dart
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'characters_dto.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// JsonSerializableGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
CharactersDto _$CharactersDtoFromJson(Map<String, dynamic> json) =>
|
||||||
|
CharactersDto(
|
||||||
|
data: (json['data'] as List<dynamic>?)
|
||||||
|
?.map((e) => CharactersDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList(),
|
||||||
|
meta: json['meta'] == null
|
||||||
|
? null
|
||||||
|
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
||||||
|
);
|
||||||
|
|
||||||
|
CharactersDataDto _$CharactersDataDtoFromJson(Map<String, dynamic> json) =>
|
||||||
|
CharactersDataDto(
|
||||||
|
id: json['id'] as String?,
|
||||||
|
type: json['type'] as String?,
|
||||||
|
attributes: json['attributes'] == null
|
||||||
|
? null
|
||||||
|
: CharasterAttributesDataDto.fromJson(
|
||||||
|
json['attributes'] as Map<String, dynamic>),
|
||||||
|
);
|
||||||
|
|
||||||
|
CharasterAttributesDataDto _$CharasterAttributesDataDtoFromJson(
|
||||||
|
Map<String, dynamic> json) =>
|
||||||
|
CharasterAttributesDataDto(
|
||||||
|
name: json['name'] as String?,
|
||||||
|
born: json['born'] as String?,
|
||||||
|
died: json['died'] as String?,
|
||||||
|
image: json['image'] 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(),
|
||||||
|
);
|
@ -1,23 +0,0 @@
|
|||||||
import 'package:json_annotation/json_annotation.dart';
|
|
||||||
|
|
||||||
part 'titans_dto.g.dart';
|
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
|
||||||
class TitansDto {
|
|
||||||
final List<TitansResultsDto>? results;
|
|
||||||
const TitansDto({this.results});
|
|
||||||
factory TitansDto.fromJson(Map<String, dynamic> json) => _$TitansDtoFromJson(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
|
||||||
class TitansResultsDto {
|
|
||||||
final int? id;
|
|
||||||
final String? name;
|
|
||||||
final String? height;
|
|
||||||
final List<String>? abilities;
|
|
||||||
final String? allegiance;
|
|
||||||
final String? img;
|
|
||||||
const TitansResultsDto({this.id, this.name, this.height, this.abilities, this.allegiance, this.img});
|
|
||||||
factory TitansResultsDto.fromJson(Map<String, dynamic> json) =>
|
|
||||||
_$TitansResultsDtoFromJson(json);
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
part of 'titans_dto.dart';
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// JsonSerializableGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
TitansDto _$TitansDtoFromJson(Map<String, dynamic> json) => TitansDto(
|
|
||||||
results: (json['results'] as List<dynamic>?)
|
|
||||||
?.map((e) => TitansResultsDto.fromJson(e as Map<String, dynamic>))
|
|
||||||
.toList(),
|
|
||||||
);
|
|
||||||
|
|
||||||
TitansResultsDto _$TitansResultsDtoFromJson(Map<String, dynamic> json) =>
|
|
||||||
TitansResultsDto(
|
|
||||||
id: (json['id'] as num?)?.toInt(),
|
|
||||||
name: json['name'] as String?,
|
|
||||||
height: json['height'] as String?,
|
|
||||||
abilities: (json['abilities'] as List<dynamic>?)
|
|
||||||
?.map((e) => e as String)
|
|
||||||
.toList(),
|
|
||||||
allegiance: json['allegiance'] as String?,
|
|
||||||
img: json['img'] as String?,
|
|
||||||
);
|
|
30
lib/data/mappes/characters_mapper.dart
Normal file
30
lib/data/mappes/characters_mapper.dart
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import 'package:pmd_lab/data/dtos/characters_dto.dart';
|
||||||
|
import 'package:pmd_lab/domain/models/card.dart';
|
||||||
|
import 'package:pmd_lab/domain/models/home.dart';
|
||||||
|
|
||||||
|
const _imagePlaceholder = 'https://cdn-icons-png.flaticon.com/512/4054/4054617.png';
|
||||||
|
|
||||||
|
extension CharacterDataDtoToModel on CharactersDataDto {
|
||||||
|
CardData toDomain() => CardData(
|
||||||
|
attributes?.name ?? 'UNKNOWN',
|
||||||
|
imgUrl: attributes?.image ?? _imagePlaceholder,
|
||||||
|
description: _makeDescription(attributes?.born, attributes?.died),
|
||||||
|
id: id,
|
||||||
|
);
|
||||||
|
String _makeDescription(String? born, String? died) {
|
||||||
|
return born != null && died != null
|
||||||
|
? '$born - $died'
|
||||||
|
: born != null
|
||||||
|
? 'born: $born'
|
||||||
|
: died != null
|
||||||
|
? 'died: $died'
|
||||||
|
: '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension CharactersDtoToModel on CharactersDto {
|
||||||
|
HomeData toDomain() => HomeData(
|
||||||
|
data: data?.map((e) => e.toDomain()).toList(),
|
||||||
|
nextPage: meta?.pagination?.next,
|
||||||
|
);
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
import 'package:pmd_lab/data/dtos/titans_dto.dart';
|
|
||||||
import 'package:pmd_lab/domain/models/card.dart';
|
|
||||||
|
|
||||||
const _imagePlaceholder = 'https://cdn-icons-png.flaticon.com/512/4054/4054617.png';
|
|
||||||
|
|
||||||
extension CharacterDataDtoToModel on TitansResultsDto {
|
|
||||||
CardData toDomain() => CardData(
|
|
||||||
name ?? 'UNKNOWN',
|
|
||||||
img: img ?? _imagePlaceholder,
|
|
||||||
description: _makeDescription(height, abilities, allegiance),
|
|
||||||
id: id.toString(),
|
|
||||||
);
|
|
||||||
String _makeDescription(String? height, List<String>? abilities, String? allegiance) {
|
|
||||||
List<String> descriptionParts = [];
|
|
||||||
if (height != null && height.isNotEmpty) {
|
|
||||||
descriptionParts.add('Height: $height');
|
|
||||||
}
|
|
||||||
if (allegiance != null && allegiance.isNotEmpty) {
|
|
||||||
descriptionParts.add('Allegiance: $allegiance');
|
|
||||||
}
|
|
||||||
if (abilities != null && abilities.isNotEmpty) {
|
|
||||||
final abilitiesStr = abilities.join(', ');
|
|
||||||
descriptionParts.add('Abilities: $abilitiesStr.');
|
|
||||||
}
|
|
||||||
return descriptionParts.join('\n');
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +1,13 @@
|
|||||||
class CardData {
|
class CardData {
|
||||||
final String name;
|
final String text;
|
||||||
final String description;
|
final String description;
|
||||||
final String? img;
|
final String? imgUrl;
|
||||||
final String? id;
|
final String? id;
|
||||||
|
|
||||||
CardData(
|
CardData(
|
||||||
this.name, {
|
this.text, {
|
||||||
required this.description,
|
required this.description,
|
||||||
this.img,
|
this.imgUrl,
|
||||||
this.id,
|
this.id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
8
lib/domain/models/home.dart
Normal file
8
lib/domain/models/home.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import 'card.dart';
|
||||||
|
|
||||||
|
class HomeData {
|
||||||
|
final List<CardData>? data;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
HomeData({this.data, this.nextPage});
|
||||||
|
}
|
@ -9,8 +9,7 @@ import 'package:pmd_lab/presentation/home_page/home_page.dart';
|
|||||||
import 'package:pmd_lab/presentation/like_bloc/like_bloc.dart';
|
import 'package:pmd_lab/presentation/like_bloc/like_bloc.dart';
|
||||||
import 'package:pmd_lab/presentation/locale_bloc/locale_bloc.dart';
|
import 'package:pmd_lab/presentation/locale_bloc/locale_bloc.dart';
|
||||||
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
||||||
import 'package:pmd_lab/repositories/mock_repository.dart';
|
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -27,23 +26,24 @@ class MyApp extends StatelessWidget {
|
|||||||
child: BlocBuilder<LocaleBloc, LocaleState>(
|
child: BlocBuilder<LocaleBloc, LocaleState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
|
title: 'Potter App',
|
||||||
locale: state.currentLocale,
|
locale: state.currentLocale,
|
||||||
localizationsDelegates: AppLocale.localizationsDelegates,
|
localizationsDelegates: AppLocale.localizationsDelegates,
|
||||||
supportedLocales: AppLocale.supportedLocales,
|
supportedLocales: AppLocale.supportedLocales,
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
primarySwatch: Colors.orange,
|
primarySwatch: Colors.orange,
|
||||||
scaffoldBackgroundColor: Color(0x33BDBCBC),
|
scaffoldBackgroundColor: Colors.white,
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: RepositoryProvider<TitansRepository>(
|
home: RepositoryProvider<PotterRepository>(
|
||||||
lazy: true,
|
lazy: true,
|
||||||
create: (_) => TitansRepository(),
|
create: (_) => PotterRepository(),
|
||||||
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<TitansRepository>()),
|
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||||
child: const MyHomePage(),
|
child: const MyHomePage(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
|
||||||
import 'package:pmd_lab/domain/models/card.dart';
|
import 'package:pmd_lab/domain/models/card.dart';
|
||||||
|
|
||||||
class DetailsPage extends StatelessWidget {
|
class DetailsPage extends StatelessWidget {
|
||||||
@ -10,59 +9,28 @@ class DetailsPage extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
backgroundColor: Color(0xFF272727),
|
appBar: AppBar(),
|
||||||
appBar: AppBar(
|
|
||||||
backgroundColor: const Color(0xFF272727), // Тёмный фон AppBar
|
|
||||||
iconTheme: const IconThemeData(
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
title: const Text(
|
|
||||||
'Детальная страница',
|
|
||||||
style: TextStyle(
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
elevation: 0,
|
|
||||||
systemOverlayStyle: SystemUiOverlayStyle.light,
|
|
||||||
),
|
|
||||||
body: Column(
|
body: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Stack(children: [
|
Padding(
|
||||||
ClipRRect(
|
padding: const EdgeInsets.only(bottom: 16),
|
||||||
child: Image.network(
|
child: Image.network(
|
||||||
data.img ?? '',
|
data.imgUrl ?? '',
|
||||||
errorBuilder: (_, __, ___) => const Placeholder(),
|
|
||||||
height: 370,
|
|
||||||
alignment: Alignment.topCenter,
|
|
||||||
fit: BoxFit.cover,
|
|
||||||
width: double.infinity,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]),
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.only(bottom: 4, top: 16, right: 20, left: 20),
|
|
||||||
child: Text(
|
|
||||||
data.name,
|
|
||||||
style: const TextStyle(
|
|
||||||
color: Colors.white70,
|
|
||||||
fontSize: 30,
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(bottom: 4, top: 16, right: 20, left: 20),
|
padding: const EdgeInsets.only(bottom: 4),
|
||||||
child: Text(
|
child: Text(
|
||||||
|
data.text,
|
||||||
|
style: Theme.of(context).textTheme.headlineLarge,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Text(
|
||||||
data.description,
|
data.description,
|
||||||
style: const TextStyle(
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
color: Colors.white70,
|
|
||||||
fontSize: 20,
|
|
||||||
fontWeight: FontWeight.normal,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
])
|
],
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,37 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:pmd_lab/presentation/home_page/bloc/events.dart';
|
import 'package:pmd_lab/presentation/home_page/bloc/events.dart';
|
||||||
import 'package:pmd_lab/presentation/home_page/bloc/state.dart';
|
import 'package:pmd_lab/presentation/home_page/bloc/state.dart';
|
||||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||||
import 'package:pmd_lab/repositories/mock_repository.dart';
|
|
||||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
|
||||||
|
|
||||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
final TitansRepository repo;
|
final PotterRepository repo;
|
||||||
|
|
||||||
HomeBloc(this.repo) : super(const HomeState()) {
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
on<HomeLoadDataEvent>(_onLoadData);
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||||
|
if (event.nextPage == null) {
|
||||||
emit(state.copyWith(isLoading: true));
|
emit(state.copyWith(isLoading: true));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(isPaginationLoading: true));
|
||||||
|
}
|
||||||
|
|
||||||
String? error;
|
String? error;
|
||||||
|
|
||||||
final data = await repo.loadResults(
|
final data = await repo.loadData(
|
||||||
q: event.search,
|
q: event.search,
|
||||||
|
page: event.nextPage ?? 1,
|
||||||
onError: (e) => error = e,
|
onError: (e) => error = e,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (event.nextPage != null) {
|
||||||
|
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
isPaginationLoading: false,
|
||||||
data: data,
|
data: data,
|
||||||
error: error,
|
error: error,
|
||||||
));
|
));
|
||||||
|
@ -1,29 +1,33 @@
|
|||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:copy_with_extension/copy_with_extension.dart';
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
import 'package:pmd_lab/domain/models/card.dart';
|
import 'package:pmd_lab/domain/models/home.dart';
|
||||||
|
|
||||||
part 'state.g.dart';
|
part 'state.g.dart';
|
||||||
|
|
||||||
@CopyWith()
|
@CopyWith()
|
||||||
class HomeState extends Equatable {
|
class HomeState extends Equatable {
|
||||||
final List<CardData>? data;
|
final HomeData? data;
|
||||||
final bool isLoading;
|
final bool isLoading;
|
||||||
|
final bool isPaginationLoading;
|
||||||
final String? error;
|
final String? error;
|
||||||
|
|
||||||
const HomeState({
|
const HomeState({
|
||||||
this.data,
|
this.data,
|
||||||
this.isLoading = false,
|
this.isLoading = false,
|
||||||
|
this.isPaginationLoading = false,
|
||||||
this.error,
|
this.error,
|
||||||
});
|
});
|
||||||
|
|
||||||
HomeState copyWith({
|
HomeState copyWith({
|
||||||
List<CardData>? data,
|
HomeData? data,
|
||||||
bool? isLoading,
|
bool? isLoading,
|
||||||
|
bool? isPaginationLoading,
|
||||||
String? error,
|
String? error,
|
||||||
}) =>
|
}) =>
|
||||||
HomeState(
|
HomeState(
|
||||||
data: data ?? this.data,
|
data: data ?? this.data,
|
||||||
isLoading: isLoading ?? this.isLoading,
|
isLoading: isLoading ?? this.isLoading,
|
||||||
|
isPaginationLoading: isPaginationLoading ?? this.isPaginationLoading,
|
||||||
error: error ?? this.error,
|
error: error ?? this.error,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -31,6 +35,7 @@ class HomeState extends Equatable {
|
|||||||
List<Object?> get props => [
|
List<Object?> get props => [
|
||||||
data,
|
data,
|
||||||
isLoading,
|
isLoading,
|
||||||
|
isPaginationLoading,
|
||||||
error,
|
error,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,12 @@ part of 'state.dart';
|
|||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
abstract class _$HomeStateCWProxy {
|
abstract class _$HomeStateCWProxy {
|
||||||
HomeState data(List<CardData>? data);
|
HomeState data(HomeData? data);
|
||||||
|
|
||||||
HomeState isLoading(bool isLoading);
|
HomeState isLoading(bool isLoading);
|
||||||
|
|
||||||
|
HomeState isPaginationLoading(bool isPaginationLoading);
|
||||||
|
|
||||||
HomeState error(String? error);
|
HomeState error(String? error);
|
||||||
|
|
||||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
@ -20,8 +22,9 @@ abstract class _$HomeStateCWProxy {
|
|||||||
/// HomeState(...).copyWith(id: 12, name: "My name")
|
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||||
/// ````
|
/// ````
|
||||||
HomeState call({
|
HomeState call({
|
||||||
List<CardData>? data,
|
HomeData? data,
|
||||||
bool? isLoading,
|
bool? isLoading,
|
||||||
|
bool? isPaginationLoading,
|
||||||
String? error,
|
String? error,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -33,11 +36,15 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
|||||||
final HomeState _value;
|
final HomeState _value;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
HomeState data(List<CardData>? data) => this(data: data);
|
HomeState data(HomeData? data) => this(data: data);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState isPaginationLoading(bool isPaginationLoading) =>
|
||||||
|
this(isPaginationLoading: isPaginationLoading);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
HomeState error(String? error) => this(error: error);
|
HomeState error(String? error) => this(error: error);
|
||||||
|
|
||||||
@ -52,17 +59,24 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
|||||||
HomeState call({
|
HomeState call({
|
||||||
Object? data = const $CopyWithPlaceholder(),
|
Object? data = const $CopyWithPlaceholder(),
|
||||||
Object? isLoading = const $CopyWithPlaceholder(),
|
Object? isLoading = const $CopyWithPlaceholder(),
|
||||||
|
Object? isPaginationLoading = const $CopyWithPlaceholder(),
|
||||||
Object? error = const $CopyWithPlaceholder(),
|
Object? error = const $CopyWithPlaceholder(),
|
||||||
}) {
|
}) {
|
||||||
return HomeState(
|
return HomeState(
|
||||||
data: data == const $CopyWithPlaceholder()
|
data: data == const $CopyWithPlaceholder()
|
||||||
? _value.data
|
? _value.data
|
||||||
// ignore: cast_nullable_to_non_nullable
|
// ignore: cast_nullable_to_non_nullable
|
||||||
: data as List<CardData>?,
|
: data as HomeData?,
|
||||||
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
|
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
|
||||||
? _value.isLoading
|
? _value.isLoading
|
||||||
// ignore: cast_nullable_to_non_nullable
|
// ignore: cast_nullable_to_non_nullable
|
||||||
: isLoading as bool,
|
: isLoading as bool,
|
||||||
|
isPaginationLoading:
|
||||||
|
isPaginationLoading == const $CopyWithPlaceholder() ||
|
||||||
|
isPaginationLoading == null
|
||||||
|
? _value.isPaginationLoading
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: isPaginationLoading as bool,
|
||||||
error: error == const $CopyWithPlaceholder()
|
error: error == const $CopyWithPlaceholder()
|
||||||
? _value.error
|
? _value.error
|
||||||
// ignore: cast_nullable_to_non_nullable
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
@ -28,9 +28,9 @@ class _Card extends StatelessWidget {
|
|||||||
bool isLiked = false,
|
bool isLiked = false,
|
||||||
}) =>
|
}) =>
|
||||||
_Card(
|
_Card(
|
||||||
data.name,
|
data.text,
|
||||||
description: data.description,
|
description: data.description,
|
||||||
imgUrl: data.img,
|
imgUrl: data.imgUrl,
|
||||||
onLike: onLike,
|
onLike: onLike,
|
||||||
onTap: onTap,
|
onTap: onTap,
|
||||||
isLiked: isLiked,
|
isLiked: isLiked,
|
||||||
@ -44,12 +44,14 @@ class _Card extends StatelessWidget {
|
|||||||
child: Container(
|
child: Container(
|
||||||
margin: const EdgeInsets.only(bottom: 20),
|
margin: const EdgeInsets.only(bottom: 20),
|
||||||
decoration:
|
decoration:
|
||||||
BoxDecoration(color: Color(0xFF272727), borderRadius: BorderRadius.circular(20),
|
BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20), boxShadow: [
|
||||||
border: Border.all( // Задаем рамку
|
BoxShadow(
|
||||||
color: Colors.white12, // Цвет рамки
|
color: Colors.black.withOpacity(0.1),
|
||||||
width: 2, // Толщина рамки
|
spreadRadius: 1,
|
||||||
),
|
offset: const Offset(0, 0),
|
||||||
),
|
blurRadius: 15,
|
||||||
|
)
|
||||||
|
]),
|
||||||
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||||
Stack(children: [
|
Stack(children: [
|
||||||
ClipRRect(
|
ClipRRect(
|
||||||
@ -75,7 +77,7 @@ class _Card extends StatelessWidget {
|
|||||||
child: Text(
|
child: Text(
|
||||||
text,
|
text,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Colors.white70,
|
color: Color(0xff4c4c4c),
|
||||||
fontSize: 30,
|
fontSize: 30,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
),
|
),
|
||||||
@ -84,7 +86,7 @@ class _Card extends StatelessWidget {
|
|||||||
Text(
|
Text(
|
||||||
description,
|
description,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Colors.white70,
|
color: Color(0xff9c9c9c),
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
fontWeight: FontWeight.normal,
|
fontWeight: FontWeight.normal,
|
||||||
),
|
),
|
||||||
@ -103,7 +105,6 @@ class _Card extends StatelessWidget {
|
|||||||
)
|
)
|
||||||
: const Icon(
|
: const Icon(
|
||||||
Icons.favorite_border,
|
Icons.favorite_border,
|
||||||
color: Colors.white,
|
|
||||||
key: ValueKey<int>(1),
|
key: ValueKey<int>(1),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -14,7 +14,9 @@ import 'package:pmd_lab/presentation/like_bloc/like_event.dart';
|
|||||||
import 'package:pmd_lab/presentation/like_bloc/like_state.dart';
|
import 'package:pmd_lab/presentation/like_bloc/like_state.dart';
|
||||||
import 'package:pmd_lab/presentation/locale_bloc/locale_events.dart';
|
import 'package:pmd_lab/presentation/locale_bloc/locale_events.dart';
|
||||||
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
||||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||||
|
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||||
|
|
||||||
import '../locale_bloc/locale_bloc.dart';
|
import '../locale_bloc/locale_bloc.dart';
|
||||||
part 'card.dart';
|
part 'card.dart';
|
||||||
|
|
||||||
@ -39,20 +41,37 @@ class Body extends StatefulWidget {
|
|||||||
|
|
||||||
class _BodyState extends State<Body> {
|
class _BodyState extends State<Body> {
|
||||||
final searchController = TextEditingController();
|
final searchController = TextEditingController();
|
||||||
|
final scrollController = ScrollController();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
SvgObjects.init();
|
SvgObjects.init();
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
context.read<LikeBloc>().add(const LoadLikesEvent());
|
context.read<LikeBloc>().add(const LoadLikesEvent());
|
||||||
});
|
});
|
||||||
|
scrollController.addListener(_onNextPageListener);
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onNextPageListener() {
|
||||||
|
if (scrollController.offset > scrollController.position.maxScrollExtent) {
|
||||||
|
final bloc = context.read<HomeBloc>();
|
||||||
|
if (!bloc.state.isPaginationLoading) {
|
||||||
|
bloc.add(HomeLoadDataEvent(
|
||||||
|
search: searchController.text,
|
||||||
|
nextPage: bloc.state.data?.nextPage,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
searchController.dispose();
|
searchController.dispose();
|
||||||
|
scrollController.dispose();
|
||||||
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,13 +90,6 @@ class _BodyState extends State<Body> {
|
|||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
placeholder: context.locale.search,
|
placeholder: context.locale.search,
|
||||||
backgroundColor: Colors.white12,
|
|
||||||
placeholderStyle: TextStyle(
|
|
||||||
color: Colors.white, // Задайте желаемый цвет плейсхолдера// (Необязательно) Задайте размер шрифта(Необязательно) Задайте стиль шрифта
|
|
||||||
),
|
|
||||||
style: TextStyle(
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
onChanged: (search) {
|
onChanged: (search) {
|
||||||
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||||
},
|
},
|
||||||
@ -118,10 +130,11 @@ class _BodyState extends State<Body> {
|
|||||||
onRefresh: _onRefresh,
|
onRefresh: _onRefresh,
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
physics: const BouncingScrollPhysics(),
|
physics: const BouncingScrollPhysics(),
|
||||||
padding: const EdgeInsets.all(12),
|
controller: scrollController,
|
||||||
itemCount: state.data?.length ?? 0,
|
padding: EdgeInsets.zero,
|
||||||
|
itemCount: state.data?.data?.length ?? 0,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final data = state.data?[index];
|
final data = state.data?.data?[index];
|
||||||
return data != null
|
return data != null
|
||||||
? _Card.fromData(
|
? _Card.fromData(
|
||||||
data,
|
data,
|
||||||
@ -137,7 +150,12 @@ class _BodyState extends State<Body> {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
), // Expanded
|
), // Expanded
|
||||||
) // BlocBuilder
|
), // BlocBuilder
|
||||||
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.isPaginationLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const SizedBox.shrink(),
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -150,7 +168,7 @@ class _BodyState extends State<Body> {
|
|||||||
'$title ${isLiked ? context.locale.liked : context.locale.disliked}',
|
'$title ${isLiked ? context.locale.liked : context.locale.disliked}',
|
||||||
style: Theme.of(context).textTheme.bodyLarge,
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
),
|
),
|
||||||
backgroundColor: Colors.white,
|
backgroundColor: Colors.orangeAccent,
|
||||||
duration: const Duration(seconds: 1),
|
duration: const Duration(seconds: 1),
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
@ -170,7 +188,7 @@ class _BodyState extends State<Body> {
|
|||||||
|
|
||||||
void _onLike(String? id, String title, bool isLiked) {
|
void _onLike(String? id, String title, bool isLiked) {
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
context.read<LikeBloc>().add(ChangeLikeEvent(id.toString()));
|
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
||||||
_showSnackBar(context, title, !isLiked);
|
_showSnackBar(context, title, !isLiked);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import 'package:pmd_lab/domain/models/card.dart';
|
import 'package:pmd_lab/domain/models/home.dart';
|
||||||
|
|
||||||
typedef OnErrorCallback = void Function(String? error);
|
typedef OnErrorCallback = void Function(String? error);
|
||||||
|
|
||||||
abstract class ApiInterface {
|
abstract class ApiInterface {
|
||||||
Future<List<CardData>?> loadResults({OnErrorCallback? onError});
|
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,29 @@
|
|||||||
import 'package:pmd_lab/domain/models/card.dart';
|
import 'package:pmd_lab/domain/models/home.dart';
|
||||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||||
|
|
||||||
class MockRepository extends ApiInterface {
|
class MockRepository extends ApiInterface {
|
||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadResults({OnErrorCallback? onError, String? q}) async {
|
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||||
return [
|
return HomeData();
|
||||||
CardData(
|
// return [
|
||||||
'Freeze',
|
// CardData(
|
||||||
description: 'so cold..',
|
// 'Какая-то новость',
|
||||||
img:
|
// description: 'В данном блоке рекомендуем разместить краткую информацию',
|
||||||
'https://www.skedaddlewildlife.com/wp-content/uploads/2018/09/depositphotos_22425309-stock-photo-a-lonely-raccoon-in-winter.jpg',
|
// imgUrl:
|
||||||
),
|
// 'https://universal.revengel.ru/assets/cache_image/images/services/3_731x487_1cb.png',
|
||||||
CardData(
|
// ),
|
||||||
'Hi',
|
// CardData(
|
||||||
description: 'pretty face',
|
// 'Ещё какая-то новость',
|
||||||
img:
|
// description: 'В данном блоке рекомендуем разместить краткую информацию',
|
||||||
'https://www.thesprucepets.com/thmb/nKNaS4I586B_H7sEUw9QAXvWM_0=/2121x0/filters:no_upscale():strip_icc()/GettyImages-135630198-5ba7d225c9e77c0050cff91b.jpg',
|
// imgUrl:
|
||||||
),
|
// "https://universal.revengel.ru/assets/cache_image/images/services/2_731x487_1cb.png",
|
||||||
CardData(
|
// ),
|
||||||
'Orange',
|
// CardData(
|
||||||
description: 'I like autumn',
|
// 'Ещё одна новость',
|
||||||
img: 'https://furmanagers.com/wp-content/uploads/2019/11/dreamstime_l_22075357.jpg',
|
// description: 'В данном блоке рекомендуем разместить краткую информацию',
|
||||||
),
|
// imgUrl:
|
||||||
];
|
// "https://universal.revengel.ru/assets/cache_image/images/services/1_731x487_1cb.png",
|
||||||
|
// )
|
||||||
|
//];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
44
lib/repositories/potter_repository.dart
Normal file
44
lib/repositories/potter_repository.dart
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:pmd_lab/data/dtos/characters_dto.dart';
|
||||||
|
import 'package:pmd_lab/data/mappes/characters_mapper.dart';
|
||||||
|
import 'package:pmd_lab/domain/models/home.dart';
|
||||||
|
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||||
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
|
class PotterRepository extends ApiInterface {
|
||||||
|
static final Dio _dio = Dio()
|
||||||
|
..interceptors.add(PrettyDioLogger(
|
||||||
|
requestHeader: true,
|
||||||
|
requestBody: true,
|
||||||
|
));
|
||||||
|
|
||||||
|
static const String _baseUrl = 'https://api.potterdb.com';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<HomeData?> loadData({
|
||||||
|
OnErrorCallback? onError,
|
||||||
|
String? q,
|
||||||
|
int page = 1,
|
||||||
|
int pageSize = 25,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
const String url = '$_baseUrl/v1/characters';
|
||||||
|
|
||||||
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||||
|
url,
|
||||||
|
queryParameters: {
|
||||||
|
'filter[name_cont]': q,
|
||||||
|
'page[number]': page,
|
||||||
|
'page[size]': pageSize,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
|
final HomeData data = dto.toDomain();
|
||||||
|
return data;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
onError?.call(e.error?.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,38 +0,0 @@
|
|||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:pmd_lab/data/dtos/titans_dto.dart';
|
|
||||||
import 'package:pmd_lab/data/mappes/titans_mapper.dart';
|
|
||||||
import 'package:pmd_lab/domain/models/card.dart';
|
|
||||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
|
||||||
|
|
||||||
class TitansRepository extends ApiInterface {
|
|
||||||
static final Dio _dio = Dio()
|
|
||||||
..interceptors.add(PrettyDioLogger(
|
|
||||||
requestHeader: true,
|
|
||||||
requestBody: true,
|
|
||||||
));
|
|
||||||
|
|
||||||
static const String _baseUrl = 'https://api.attackontitanapi.com';
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<List<CardData>?> loadResults({
|
|
||||||
OnErrorCallback? onError,
|
|
||||||
String? q
|
|
||||||
}) async {
|
|
||||||
try {
|
|
||||||
const String url = '$_baseUrl/titans';
|
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
|
||||||
url,
|
|
||||||
queryParameters: q != null ? {'name': q} : null,
|
|
||||||
);
|
|
||||||
|
|
||||||
final TitansDto dto = TitansDto.fromJson(response.data as Map<String, dynamic>);
|
|
||||||
final List<CardData>? results = dto.results?.map((e) => e.toDomain()).toList();
|
|
||||||
return results;
|
|
||||||
} on DioException catch (e) {
|
|
||||||
onError?.call(e.error?.toString());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user