лаб 6 готова, пагинации нет, но нужна ли она?....
This commit is contained in:
parent
61450b5ce5
commit
1e6e02e9a0
20
lib/components/utils/debounce.dart
Normal file
20
lib/components/utils/debounce.dart
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
class Debounce {
|
||||||
|
factory Debounce() => _instance;
|
||||||
|
|
||||||
|
Debounce._();
|
||||||
|
|
||||||
|
static final Debounce _instance = Debounce._();
|
||||||
|
|
||||||
|
static Timer? _timer;
|
||||||
|
|
||||||
|
static void run (
|
||||||
|
VoidCallback action, {
|
||||||
|
Duration delay = const Duration(milliseconds: 500),
|
||||||
|
}) {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = Timer(delay, action);
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,12 @@ part 'characters_dto.g.dart';
|
|||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class CharactersDto {
|
class CharactersDto {
|
||||||
final List<CharacterDataDto>? data;
|
final List<CharacterDataDto>? data;
|
||||||
|
final MetaDto? meta;
|
||||||
|
|
||||||
const CharactersDto({this.data});
|
const CharactersDto({
|
||||||
|
this.data,
|
||||||
|
this.meta,
|
||||||
|
});
|
||||||
|
|
||||||
factory CharactersDto.fromJson(Map<String, dynamic> json) => _$CharactersDtoFromJson(json);
|
factory CharactersDto.fromJson(Map<String, dynamic> json) => _$CharactersDtoFromJson(json);
|
||||||
}
|
}
|
||||||
@ -15,7 +19,7 @@ class CharactersDto {
|
|||||||
class CharacterDataDto {
|
class CharacterDataDto {
|
||||||
final String? id;
|
final String? id;
|
||||||
final String? type;
|
final String? type;
|
||||||
final CharacterAttributesDataDto? attributes; //составной элемент
|
final CharacterAttributesDataDto? attributes;
|
||||||
|
|
||||||
const CharacterDataDto({this.id, this.type, this.attributes});
|
const CharacterDataDto({this.id, this.type, this.attributes});
|
||||||
|
|
||||||
@ -31,5 +35,26 @@ class CharacterAttributesDataDto {
|
|||||||
|
|
||||||
const CharacterAttributesDataDto({this.name, this.born, this.died, this.image});
|
const CharacterAttributesDataDto({this.name, this.born, this.died, this.image});
|
||||||
|
|
||||||
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) => _$CharacterAttributesDataDtoFromJson(json);
|
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$CharacterAttributesDataDtoFromJson(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);
|
||||||
}
|
}
|
@ -11,6 +11,9 @@ CharactersDto _$CharactersDtoFromJson(Map<String, dynamic> json) =>
|
|||||||
data: (json['data'] as List<dynamic>?)
|
data: (json['data'] as List<dynamic>?)
|
||||||
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
|
?.map((e) => CharacterDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
|
meta: json['meta'] == null
|
||||||
|
? null
|
||||||
|
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
||||||
);
|
);
|
||||||
|
|
||||||
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) =>
|
CharacterDataDto _$CharacterDataDtoFromJson(Map<String, dynamic> json) =>
|
||||||
@ -31,3 +34,16 @@ CharacterAttributesDataDto _$CharacterAttributesDataDtoFromJson(
|
|||||||
died: json['died'] as String?,
|
died: json['died'] as String?,
|
||||||
image: json['image'] 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,9 +1,17 @@
|
|||||||
import 'package:pmu/data/dtos/characters_dto.dart';
|
import 'package:pmu/data/dtos/characters_dto.dart';
|
||||||
import 'package:pmu/domain/models/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import '../../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 CharactersDtoToModel on CharactersDto {
|
||||||
|
HomeData toDomain() => HomeData(
|
||||||
|
data: data?.map((e) => e.toDomain()).toList(),
|
||||||
|
nextPage: meta?.pagination?.next,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
extension CharacterDataDtoToModel on CharacterDataDto {
|
extension CharacterDataDtoToModel on CharacterDataDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() => CardData(
|
||||||
attributes?.name ?? 'UNKNOWN',
|
attributes?.name ?? 'UNKNOWN',
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import 'package:pmu/domain/models/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import 'package:pmu/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>?> loadData({OnErrorCallback? onError});
|
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||||
}
|
}
|
@ -1,67 +1,33 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:pmu/data/repositories/api_interface.dart';
|
import 'package:pmu/data/repositories/api_interface.dart';
|
||||||
import 'package:pmu/domain/models/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import 'package:pmu/domain/models/home.dart';
|
||||||
|
|
||||||
class MockRepository extends ApiInterface {
|
class MockRepository extends ApiInterface {
|
||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
|
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||||
return [
|
return HomeData(
|
||||||
CardData(
|
data: [
|
||||||
'собирается к первой паре',
|
CardData(
|
||||||
descriptionText: 'надел носочек и думает о смысле жизни',
|
'Freeze',
|
||||||
icon: Icons.favorite,
|
descriptionText: 'so cold..',
|
||||||
imageUrl:
|
imageUrl:
|
||||||
'https://i.pinimg.com/564x/7d/87/eb/7d87eb75ac6fcb46beabc5cb388c7d34.jpg',
|
'https://www.skedaddlewildlife.com/wp-content/uploads/2018/09/depositphotos_22425309-stock-photo-a-lonely-raccoon-in-winter.jpg',
|
||||||
),
|
),
|
||||||
CardData(
|
CardData(
|
||||||
'уснул за завтраком',
|
'Hi',
|
||||||
descriptionText: 'всю ночь делал лабы....а кто не делал?',
|
descriptionText: 'pretty face',
|
||||||
icon: Icons.favorite,
|
icon: Icons.hail,
|
||||||
imageUrl:
|
imageUrl:
|
||||||
'https://i.pinimg.com/564x/c5/30/b6/c530b6de14c58fd8f56ec28dac9376c9.jpg',
|
'https://www.thesprucepets.com/thmb/nKNaS4I586B_H7sEUw9QAXvWM_0=/2121x0/filters:no_upscale():strip_icc()/GettyImages-135630198-5ba7d225c9e77c0050cff91b.jpg',
|
||||||
),
|
),
|
||||||
CardData(
|
CardData(
|
||||||
'едет к первой паре',
|
'Orange',
|
||||||
descriptionText: '(спит)',
|
descriptionText: 'I like autumn',
|
||||||
icon: Icons.favorite,
|
icon: Icons.warning_amber,
|
||||||
imageUrl:
|
imageUrl: 'https://furmanagers.com/wp-content/uploads/2019/11/dreamstime_l_22075357.jpg',
|
||||||
'https://i.pinimg.com/564x/b2/af/d9/b2afd919443c31d2d309a1c67b11930e.jpg',
|
),
|
||||||
),
|
],
|
||||||
CardData(
|
);
|
||||||
'довольный',
|
|
||||||
descriptionText: 'пришел в столовку строилки',
|
|
||||||
icon: Icons.favorite,
|
|
||||||
imageUrl:
|
|
||||||
'https://i.pinimg.com/736x/a3/12/0f/a3120feb082602247d5ab6d66bf2db61.jpg',
|
|
||||||
),
|
|
||||||
CardData(
|
|
||||||
'делает лабы ночью',
|
|
||||||
descriptionText: 'опять... ну ничему жизнь не учит',
|
|
||||||
icon: Icons.favorite,
|
|
||||||
imageUrl:
|
|
||||||
'https://sun9-69.userapi.com/impg/MLc9nPmTUsLHA3Q53nqxz8tao0oyQ_5dGWidNQ/cXmzhWa97Qc.jpg?size=421x421&quality=95&sign=04c80f2ff2a43ff5b936190987e591e5&type=album',
|
|
||||||
),
|
|
||||||
CardData(
|
|
||||||
'всё бросил и уехал в саратов',
|
|
||||||
descriptionText: 'я ему завидую',
|
|
||||||
icon: Icons.favorite,
|
|
||||||
imageUrl:
|
|
||||||
'https://i.pinimg.com/564x/d4/25/fb/d425fb23ef181e36ad93d7c8c7f7292e.jpg',
|
|
||||||
),
|
|
||||||
CardData(
|
|
||||||
'кушоет арбуз',
|
|
||||||
descriptionText: 'и никакой депрессии!',
|
|
||||||
icon: Icons.favorite,
|
|
||||||
imageUrl:
|
|
||||||
'https://i.pinimg.com/736x/cc/e4/38/cce4384798b795b8d69e9caa8bf02c46.jpg',
|
|
||||||
),
|
|
||||||
CardData(
|
|
||||||
'ой, а этот как сюда попал?',
|
|
||||||
descriptionText: 'отвернитесь, он стесняется',
|
|
||||||
icon: Icons.favorite,
|
|
||||||
imageUrl:
|
|
||||||
'https://i.pinimg.com/736x/64/33/86/6433869ef972b167fb43cf74e0ad40aa.jpg',
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,8 @@ import 'package:pmu/data/mappers/characters_mapper.dart';
|
|||||||
import 'package:pmu/data/repositories/api_interface.dart';
|
import 'package:pmu/data/repositories/api_interface.dart';
|
||||||
import 'package:pmu/domain/models/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
import 'package:pmu/domain/models/home.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/events.dart';
|
||||||
|
|
||||||
class PotterRepository extends ApiInterface {
|
class PotterRepository extends ApiInterface {
|
||||||
static final Dio _dio = Dio()
|
static final Dio _dio = Dio()
|
||||||
@ -15,20 +17,29 @@ class PotterRepository extends ApiInterface {
|
|||||||
static const String _baseUrl = 'https://api.potterdb.com';
|
static const String _baseUrl = 'https://api.potterdb.com';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
|
Future<HomeData?> loadData({
|
||||||
|
OnErrorCallback? onError,
|
||||||
|
String? q,
|
||||||
|
int page = 1,
|
||||||
|
int pageSize = 5,
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
const String url = '$_baseUrl/v1/characters';
|
const String url = '$_baseUrl/v1/characters';
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||||
url,
|
url,
|
||||||
queryParameters: q != null ? {'filter[name_cont]': q} : null,
|
queryParameters: {
|
||||||
|
'filter[name_cont]': q,
|
||||||
|
'page[number]': page,
|
||||||
|
'page[size]': pageSize,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
final HomeData data = dto.toDomain();
|
||||||
return data;
|
return data;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
onError?.call(e.response?.statusMessage);
|
onError?.call(e.error?.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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});
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/bloc.dart';
|
||||||
import 'package:pmu/presentation/home_page/home_page.dart';
|
import 'package:pmu/presentation/home_page/home_page.dart';
|
||||||
|
import 'data/repositories/potter_repository.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -17,7 +20,15 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Сафиулова Камилия Наилевна'),
|
home: RepositoryProvider<PotterRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => PotterRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||||
|
child: const MyHomePage(title: 'Сафиулова Камилия Наилевна'),
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
39
lib/presentation/home_page/bloc/bloc.dart
Normal file
39
lib/presentation/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu/data/repositories/potter_repository.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/state.dart';
|
||||||
|
|
||||||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
|
final PotterRepository 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(
|
||||||
|
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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
10
lib/presentation/home_page/bloc/events.dart
Normal file
10
lib/presentation/home_page/bloc/events.dart
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
abstract class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomeLoadDataEvent extends HomeEvent {
|
||||||
|
final String? search;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
const HomeLoadDataEvent({this.search, this.nextPage});
|
||||||
|
}
|
29
lib/presentation/home_page/bloc/state.dart
Normal file
29
lib/presentation/home_page/bloc/state.dart
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
import '../../../domain/models/home.dart';
|
||||||
|
|
||||||
|
part 'state.g.dart';
|
||||||
|
|
||||||
|
@CopyWith()
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
data,
|
||||||
|
isLoading,
|
||||||
|
isPaginationLoading,
|
||||||
|
error,
|
||||||
|
];
|
||||||
|
}
|
92
lib/presentation/home_page/bloc/state.g.dart
Normal file
92
lib/presentation/home_page/bloc/state.g.dart
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'state.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// CopyWithGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
abstract class _$HomeStateCWProxy {
|
||||||
|
HomeState data(HomeData? data);
|
||||||
|
|
||||||
|
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.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
HomeState call({
|
||||||
|
HomeData? data,
|
||||||
|
bool? isLoading,
|
||||||
|
bool? isPaginationLoading,
|
||||||
|
String? error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfHomeState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfHomeState.copyWith.fieldName(...)`
|
||||||
|
class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||||
|
const _$HomeStateCWProxyImpl(this._value);
|
||||||
|
|
||||||
|
final HomeState _value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState data(HomeData? data) => this(data: data);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState isPaginationLoading(bool isPaginationLoading) =>
|
||||||
|
this(isPaginationLoading: isPaginationLoading);
|
||||||
|
|
||||||
|
@override
|
||||||
|
HomeState error(String? error) => this(error: error);
|
||||||
|
|
||||||
|
@override
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
HomeState call({
|
||||||
|
Object? data = const $CopyWithPlaceholder(),
|
||||||
|
Object? isLoading = const $CopyWithPlaceholder(),
|
||||||
|
Object? isPaginationLoading = const $CopyWithPlaceholder(),
|
||||||
|
Object? error = const $CopyWithPlaceholder(),
|
||||||
|
}) {
|
||||||
|
return HomeState(
|
||||||
|
data: data == const $CopyWithPlaceholder()
|
||||||
|
? _value.data
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: data as HomeData?,
|
||||||
|
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
|
||||||
|
? _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
|
||||||
|
: error as String?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension $HomeStateCopyWith on HomeState {
|
||||||
|
/// Returns a callable class that can be used as follows: `instanceOfHomeState.copyWith(...)` or like so:`instanceOfHomeState.copyWith.fieldName(...)`.
|
||||||
|
// ignore: library_private_types_in_public_api
|
||||||
|
_$HomeStateCWProxy get copyWith => _$HomeStateCWProxyImpl(this);
|
||||||
|
}
|
@ -1,9 +1,15 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:pmu/domain/models/card.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu/components/utils/debounce.dart';
|
||||||
import '../details_page/details_page.dart';
|
|
||||||
import 'package:pmu/data/repositories/potter_repository.dart';
|
import 'package:pmu/data/repositories/potter_repository.dart';
|
||||||
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import 'package:pmu/main.dart';
|
||||||
|
import 'package:pmu/presentation/details_page/details_page.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/state.dart';
|
||||||
|
|
||||||
part 'card.dart';
|
part 'card.dart';
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class MyHomePage extends StatefulWidget {
|
||||||
@ -16,7 +22,6 @@ class MyHomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const Scaffold(body: Body());
|
return const Scaffold(body: Body());
|
||||||
@ -24,24 +29,46 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Body extends StatefulWidget {
|
class Body extends StatefulWidget {
|
||||||
const Body({super.key});
|
const Body();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<Body> createState() => _BodyState();
|
State<Body> createState() => BodyState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _BodyState extends State<Body> {
|
class BodyState extends State<Body> {
|
||||||
final searchController = TextEditingController();
|
final searchController = TextEditingController();
|
||||||
late Future<List<CardData>?> data;
|
final scrollController = ScrollController();
|
||||||
|
|
||||||
final repo = PotterRepository();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
data = repo.loadData();
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
});
|
||||||
|
|
||||||
|
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
|
||||||
|
void dispose() {
|
||||||
|
searchController.dispose();
|
||||||
|
scrollController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Padding(
|
return Padding(
|
||||||
@ -53,40 +80,55 @@ class _BodyState extends State<Body> {
|
|||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
onChanged: (search) {
|
onChanged: (search) {
|
||||||
setState(() {
|
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||||
data = repo.loadData(q: search);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
child: Center(
|
builder: (context, state) => state.error != null
|
||||||
child: FutureBuilder<List<CardData>?>(
|
? Text(
|
||||||
future: data,
|
state.error ?? '',
|
||||||
builder: (context, snapshot) => SingleChildScrollView(
|
style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.red),
|
||||||
child: snapshot.hasData
|
)
|
||||||
? Column(
|
: state.isLoading
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
? const CircularProgressIndicator()
|
||||||
children: snapshot.data?.map((data) {
|
: Expanded(
|
||||||
return _Card.fromData(
|
child: RefreshIndicator(
|
||||||
data,
|
onRefresh: _onRefresh,
|
||||||
onLike: (String title, bool isLiked) =>
|
child: ListView.builder(
|
||||||
_showSnackBar(context, title, isLiked),
|
controller: scrollController,
|
||||||
onTap: () => _navToDetails(context, data),
|
padding: EdgeInsets.zero,
|
||||||
);
|
itemCount: state.data?.data?.length ?? 0,
|
||||||
}).toList() ??
|
itemBuilder: (context, index) {
|
||||||
[],
|
final data = state.data?.data?[index];
|
||||||
)
|
return data != null
|
||||||
: const CircularProgressIndicator(),
|
? _Card.fromData(
|
||||||
|
data,
|
||||||
|
onLike: (title, isLiked) =>
|
||||||
|
_showSnackBar(context, title, isLiked),
|
||||||
|
onTap: () => _navToDetails(context, data),
|
||||||
|
)
|
||||||
|
: const SizedBox.shrink();
|
||||||
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.isPaginationLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const SizedBox.shrink(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onRefresh() {
|
||||||
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
||||||
|
return Future.value(null);
|
||||||
|
}
|
||||||
|
|
||||||
void _navToDetails(BuildContext context, CardData data) {
|
void _navToDetails(BuildContext context, CardData data) {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
@ -101,9 +143,9 @@ class _BodyState extends State<Body> {
|
|||||||
'$title ${isLiked ? 'liked!' : 'disliked :('}',
|
'$title ${isLiked ? 'liked!' : 'disliked :('}',
|
||||||
style: Theme.of(context).textTheme.bodyLarge,
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
),
|
),
|
||||||
backgroundColor: Colors.brown.shade300,
|
backgroundColor: Colors.orangeAccent,
|
||||||
duration: const Duration(seconds: 1),
|
duration: const Duration(seconds: 1),
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
56
pubspec.lock
56
pubspec.lock
@ -38,6 +38,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.11.0"
|
version: "2.11.0"
|
||||||
|
bloc:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: bloc
|
||||||
|
sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.4"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -158,6 +166,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.1"
|
version: "3.1.1"
|
||||||
|
copy_with_extension:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: copy_with_extension
|
||||||
|
sha256: fbcf890b0c34aedf0894f91a11a579994b61b4e04080204656b582708b5b1125
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.4"
|
||||||
|
copy_with_extension_gen:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: copy_with_extension_gen
|
||||||
|
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.4"
|
||||||
crypto:
|
crypto:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -198,6 +222,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
equatable:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: equatable
|
||||||
|
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.5"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -227,6 +259,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_bloc:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_bloc
|
||||||
|
sha256: b594505eac31a0518bdcb4b5b79573b8d9117b193cc80cc12e17d639b10aa27a
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.6"
|
||||||
flutter_lints:
|
flutter_lints:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
@ -392,6 +432,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
package_config:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -424,6 +472,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.4.0"
|
version: "1.4.0"
|
||||||
|
provider:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.2"
|
||||||
pub_semver:
|
pub_semver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -33,7 +33,9 @@ dependencies:
|
|||||||
json_annotation: ^4.8.1
|
json_annotation: ^4.8.1
|
||||||
dio: ^5.4.2+1
|
dio: ^5.4.2+1
|
||||||
pretty_dio_logger: ^1.3.1
|
pretty_dio_logger: ^1.3.1
|
||||||
|
equatable: ^2.0.5
|
||||||
|
flutter_bloc: ^8.1.5
|
||||||
|
copy_with_extension_gen: ^5.0.4
|
||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
|
Loading…
Reference in New Issue
Block a user