Merge pull request 'course_work' (#3) from course_work into master
Reviewed-on: #3
This commit is contained in:
commit
8bd5756ab3
5
Makefile
5
Makefile
@ -4,11 +4,6 @@ gen:
|
||||
icon:
|
||||
flutter pub run flutter_launcher_icons:main
|
||||
|
||||
hello:
|
||||
echo "Hi!"; \
|
||||
echo "I`m makefile"; \
|
||||
echo "^_^"
|
||||
|
||||
init_res:
|
||||
dart pub global activate flutter_asset_generator
|
||||
|
||||
|
@ -1,53 +0,0 @@
|
||||
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);
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
// 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(),
|
||||
);
|
23
lib/data/dtos/titans_dto.dart
Normal file
23
lib/data/dtos/titans_dto.dart
Normal file
@ -0,0 +1,23 @@
|
||||
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);
|
||||
}
|
25
lib/data/dtos/titans_dto.g.dart
Normal file
25
lib/data/dtos/titans_dto.g.dart
Normal file
@ -0,0 +1,25 @@
|
||||
// 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?,
|
||||
);
|
@ -1,30 +0,0 @@
|
||||
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,
|
||||
);
|
||||
}
|
34
lib/data/mappes/titans_mapper.dart
Normal file
34
lib/data/mappes/titans_mapper.dart
Normal file
@ -0,0 +1,34 @@
|
||||
import 'package:pmd_lab/data/dtos/titans_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 TitansDtoToModel on TitansDto {
|
||||
HomeData toDomain() => HomeData(
|
||||
data: results?.map((e) => e.toDomain()).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
extension TitanResultsDtoToModel 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 {
|
||||
final String text;
|
||||
final String name;
|
||||
final String description;
|
||||
final String? imgUrl;
|
||||
final String? id;
|
||||
final String? img;
|
||||
final String id;
|
||||
|
||||
CardData(
|
||||
this.text, {
|
||||
this.name, {
|
||||
required this.description,
|
||||
this.imgUrl,
|
||||
this.id,
|
||||
this.img,
|
||||
required this.id,
|
||||
});
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
import 'card.dart';
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
|
||||
class HomeData {
|
||||
final List<CardData>? data;
|
||||
final int? nextPage;
|
||||
|
||||
HomeData({this.data, this.nextPage});
|
||||
}
|
||||
HomeData({this.data});
|
||||
}
|
@ -9,7 +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/locale_bloc/locale_bloc.dart';
|
||||
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
@ -26,24 +26,24 @@ class MyApp extends StatelessWidget {
|
||||
child: BlocBuilder<LocaleBloc, LocaleState>(
|
||||
builder: (context, state) {
|
||||
return MaterialApp(
|
||||
title: 'Potter App',
|
||||
locale: state.currentLocale,
|
||||
localizationsDelegates: AppLocale.localizationsDelegates,
|
||||
supportedLocales: AppLocale.supportedLocales,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.orange,
|
||||
scaffoldBackgroundColor: Colors.white,
|
||||
scaffoldBackgroundColor: Color(0x33BDBCBC),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: RepositoryProvider<PotterRepository>(
|
||||
home: RepositoryProvider<TitansRepository>(
|
||||
lazy: true,
|
||||
create: (_) => PotterRepository(),
|
||||
create: (_) => TitansRepository(),
|
||||
child: BlocProvider<LikeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => LikeBloc(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||
create: (context) => HomeBloc(context.read<TitansRepository>()),
|
||||
child: const MyHomePage(),
|
||||
),
|
||||
),
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
|
||||
class DetailsPage extends StatelessWidget {
|
||||
@ -9,28 +10,59 @@ class DetailsPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
backgroundColor: Color(0xFF272727),
|
||||
appBar: AppBar(
|
||||
backgroundColor: const Color(0xFF272727), // Тёмный фон AppBar
|
||||
iconTheme: const IconThemeData(
|
||||
color: Colors.white,
|
||||
),
|
||||
title: Text(
|
||||
data.name,
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
elevation: 0,
|
||||
systemOverlayStyle: SystemUiOverlayStyle.light,
|
||||
),
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 16),
|
||||
child: Image.network(
|
||||
data.imgUrl ?? '',
|
||||
Stack(children: [
|
||||
ClipRRect(
|
||||
child: Image.network(
|
||||
data.img ?? '',
|
||||
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: const EdgeInsets.only(bottom: 4),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4, top: 16, right: 20, left: 20),
|
||||
child: Text(
|
||||
data.text,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
data.description,
|
||||
style: const TextStyle(
|
||||
color: Colors.white70,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
data.description,
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
)
|
||||
],
|
||||
));
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,27 @@
|
||||
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/state.dart';
|
||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
||||
|
||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||
final PotterRepository repo;
|
||||
final TitansRepository repo;
|
||||
|
||||
HomeBloc(this.repo) : super(const HomeState()) {
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
}
|
||||
|
||||
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||
if (event.nextPage == null) {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
} else {
|
||||
emit(state.copyWith(isPaginationLoading: true));
|
||||
}
|
||||
|
||||
String? error;
|
||||
|
||||
final data = await repo.loadData(
|
||||
final data = await repo.loadResults(
|
||||
q: event.search,
|
||||
page: event.nextPage ?? 1,
|
||||
onError: (e) => error = e,
|
||||
);
|
||||
|
||||
if (event.nextPage != null) {
|
||||
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
isPaginationLoading: false,
|
||||
data: data,
|
||||
error: error,
|
||||
));
|
||||
|
@ -4,6 +4,5 @@ class HomeEvent {
|
||||
|
||||
class HomeLoadDataEvent extends HomeEvent {
|
||||
final String? search;
|
||||
final int? nextPage;
|
||||
const HomeLoadDataEvent({this.search, this.nextPage});
|
||||
const HomeLoadDataEvent({this.search});
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'package:equatable/equatable.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';
|
||||
@ -8,26 +9,22 @@ part 'state.g.dart';
|
||||
class HomeState extends Equatable {
|
||||
final HomeData? data;
|
||||
final bool isLoading;
|
||||
final bool isPaginationLoading;
|
||||
final String? error;
|
||||
|
||||
const HomeState({
|
||||
this.data,
|
||||
this.isLoading = false,
|
||||
this.isPaginationLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
HomeState copyWith({
|
||||
HomeData? data,
|
||||
bool? isLoading,
|
||||
bool? isPaginationLoading,
|
||||
String? error,
|
||||
}) =>
|
||||
HomeState(
|
||||
data: data ?? this.data,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
isPaginationLoading: isPaginationLoading ?? this.isPaginationLoading,
|
||||
error: error ?? this.error,
|
||||
);
|
||||
|
||||
@ -35,7 +32,6 @@ class HomeState extends Equatable {
|
||||
List<Object?> get props => [
|
||||
data,
|
||||
isLoading,
|
||||
isPaginationLoading,
|
||||
error,
|
||||
];
|
||||
}
|
||||
|
@ -11,8 +11,6 @@ abstract class _$HomeStateCWProxy {
|
||||
|
||||
HomeState isLoading(bool isLoading);
|
||||
|
||||
HomeState isPaginationLoading(bool isPaginationLoading);
|
||||
|
||||
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.
|
||||
@ -24,7 +22,6 @@ abstract class _$HomeStateCWProxy {
|
||||
HomeState call({
|
||||
HomeData? data,
|
||||
bool? isLoading,
|
||||
bool? isPaginationLoading,
|
||||
String? error,
|
||||
});
|
||||
}
|
||||
@ -41,10 +38,6 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||
@override
|
||||
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
||||
|
||||
@override
|
||||
HomeState isPaginationLoading(bool isPaginationLoading) =>
|
||||
this(isPaginationLoading: isPaginationLoading);
|
||||
|
||||
@override
|
||||
HomeState error(String? error) => this(error: error);
|
||||
|
||||
@ -59,7 +52,6 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||
HomeState call({
|
||||
Object? data = const $CopyWithPlaceholder(),
|
||||
Object? isLoading = const $CopyWithPlaceholder(),
|
||||
Object? isPaginationLoading = const $CopyWithPlaceholder(),
|
||||
Object? error = const $CopyWithPlaceholder(),
|
||||
}) {
|
||||
return HomeState(
|
||||
@ -71,12 +63,6 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||
? _value.isLoading
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: isLoading as bool,
|
||||
isPaginationLoading:
|
||||
isPaginationLoading == const $CopyWithPlaceholder() ||
|
||||
isPaginationLoading == null
|
||||
? _value.isPaginationLoading
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: isPaginationLoading as bool,
|
||||
error: error == const $CopyWithPlaceholder()
|
||||
? _value.error
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
|
@ -28,9 +28,9 @@ class _Card extends StatelessWidget {
|
||||
bool isLiked = false,
|
||||
}) =>
|
||||
_Card(
|
||||
data.text,
|
||||
data.name,
|
||||
description: data.description,
|
||||
imgUrl: data.imgUrl,
|
||||
imgUrl: data.img,
|
||||
onLike: onLike,
|
||||
onTap: onTap,
|
||||
isLiked: isLiked,
|
||||
@ -44,14 +44,12 @@ class _Card extends StatelessWidget {
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 20),
|
||||
decoration:
|
||||
BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20), boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.1),
|
||||
spreadRadius: 1,
|
||||
offset: const Offset(0, 0),
|
||||
blurRadius: 15,
|
||||
)
|
||||
]),
|
||||
BoxDecoration(color: Color(0xFF272727), borderRadius: BorderRadius.circular(20),
|
||||
border: Border.all( // Задаем рамку
|
||||
color: Colors.white12, // Цвет рамки
|
||||
width: 2, // Толщина рамки
|
||||
),
|
||||
),
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||
Stack(children: [
|
||||
ClipRRect(
|
||||
@ -77,7 +75,7 @@ class _Card extends StatelessWidget {
|
||||
child: Text(
|
||||
text,
|
||||
style: const TextStyle(
|
||||
color: Color(0xff4c4c4c),
|
||||
color: Colors.white70,
|
||||
fontSize: 30,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
@ -86,7 +84,7 @@ class _Card extends StatelessWidget {
|
||||
Text(
|
||||
description,
|
||||
style: const TextStyle(
|
||||
color: Color(0xff9c9c9c),
|
||||
color: Colors.white70,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.normal,
|
||||
),
|
||||
@ -105,6 +103,7 @@ class _Card extends StatelessWidget {
|
||||
)
|
||||
: const Icon(
|
||||
Icons.favorite_border,
|
||||
color: Colors.white,
|
||||
key: ValueKey<int>(1),
|
||||
),
|
||||
),
|
||||
|
@ -14,9 +14,7 @@ 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/locale_bloc/locale_events.dart';
|
||||
import 'package:pmd_lab/presentation/locale_bloc/locale_state.dart';
|
||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||
|
||||
import 'package:pmd_lab/repositories/titans_repository.dart';
|
||||
import '../locale_bloc/locale_bloc.dart';
|
||||
part 'card.dart';
|
||||
|
||||
@ -41,37 +39,20 @@ class Body extends StatefulWidget {
|
||||
|
||||
class _BodyState extends State<Body> {
|
||||
final searchController = TextEditingController();
|
||||
final scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
SvgObjects.init();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||
context.read<LikeBloc>().add(const LoadLikesEvent());
|
||||
});
|
||||
scrollController.addListener(_onNextPageListener);
|
||||
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
|
||||
void dispose() {
|
||||
searchController.dispose();
|
||||
scrollController.dispose();
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@ -90,6 +71,13 @@ class _BodyState extends State<Body> {
|
||||
child: CupertinoSearchTextField(
|
||||
controller: searchController,
|
||||
placeholder: context.locale.search,
|
||||
backgroundColor: Colors.white12,
|
||||
placeholderStyle: TextStyle(
|
||||
color: Colors.white, // Задайте желаемый цвет плейсхолдера// (Необязательно) Задайте размер шрифта(Необязательно) Задайте стиль шрифта
|
||||
),
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
),
|
||||
onChanged: (search) {
|
||||
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||
},
|
||||
@ -130,8 +118,7 @@ class _BodyState extends State<Body> {
|
||||
onRefresh: _onRefresh,
|
||||
child: ListView.builder(
|
||||
physics: const BouncingScrollPhysics(),
|
||||
controller: scrollController,
|
||||
padding: EdgeInsets.zero,
|
||||
padding: const EdgeInsets.all(12),
|
||||
itemCount: state.data?.data?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final data = state.data?.data?[index];
|
||||
@ -150,12 +137,7 @@ class _BodyState extends State<Body> {
|
||||
);
|
||||
},
|
||||
), // Expanded
|
||||
), // BlocBuilder
|
||||
BlocBuilder<HomeBloc, HomeState>(
|
||||
builder: (context, state) => state.isPaginationLoading
|
||||
? const CircularProgressIndicator()
|
||||
: const SizedBox.shrink(),
|
||||
)
|
||||
) // BlocBuilder
|
||||
],
|
||||
),
|
||||
);
|
||||
@ -168,7 +150,7 @@ class _BodyState extends State<Body> {
|
||||
'$title ${isLiked ? context.locale.liked : context.locale.disliked}',
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
backgroundColor: Colors.orangeAccent,
|
||||
backgroundColor: Colors.white,
|
||||
duration: const Duration(seconds: 1),
|
||||
));
|
||||
});
|
||||
@ -188,7 +170,7 @@ class _BodyState extends State<Body> {
|
||||
|
||||
void _onLike(String? id, String title, bool isLiked) {
|
||||
if (id != null) {
|
||||
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
||||
context.read<LikeBloc>().add(ChangeLikeEvent(id.toString()));
|
||||
_showSnackBar(context, title, !isLiked);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
import 'package:pmd_lab/domain/models/card.dart';
|
||||
import 'package:pmd_lab/domain/models/home.dart';
|
||||
|
||||
typedef OnErrorCallback = void Function(String? error);
|
||||
|
||||
abstract class ApiInterface {
|
||||
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||
Future<HomeData?> loadResults({OnErrorCallback? onError});
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
import 'package:pmd_lab/domain/models/home.dart';
|
||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||
|
||||
class MockRepository extends ApiInterface {
|
||||
@override
|
||||
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||
return HomeData();
|
||||
// return [
|
||||
// CardData(
|
||||
// 'Какая-то новость',
|
||||
// description: 'В данном блоке рекомендуем разместить краткую информацию',
|
||||
// imgUrl:
|
||||
// 'https://universal.revengel.ru/assets/cache_image/images/services/3_731x487_1cb.png',
|
||||
// ),
|
||||
// CardData(
|
||||
// 'Ещё какая-то новость',
|
||||
// description: 'В данном блоке рекомендуем разместить краткую информацию',
|
||||
// imgUrl:
|
||||
// "https://universal.revengel.ru/assets/cache_image/images/services/2_731x487_1cb.png",
|
||||
// ),
|
||||
// CardData(
|
||||
// 'Ещё одна новость',
|
||||
// description: 'В данном блоке рекомендуем разместить краткую информацию',
|
||||
// imgUrl:
|
||||
// "https://universal.revengel.ru/assets/cache_image/images/services/1_731x487_1cb.png",
|
||||
// )
|
||||
//];
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
39
lib/repositories/titans_repository.dart
Normal file
39
lib/repositories/titans_repository.dart
Normal file
@ -0,0 +1,39 @@
|
||||
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/domain/models/home.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<HomeData?> 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 HomeData data = dto.toDomain();
|
||||
return data;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e.error?.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user