commit
f4c117125c
18
lib/components/debounce.dart
Normal file
18
lib/components/debounce.dart
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import 'package:pmu/data/dto/user_dto.dart';
|
import 'package:pmu/data/dto/user_dto.dart';
|
||||||
import 'package:pmu/domain/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
|
||||||
extension UserDtoToModel on UserDto {
|
extension UserDtoToModel on UserDto {
|
||||||
CardPostData toDomain() {
|
CardData toDomain() {
|
||||||
const allowedExtensions = ['jpg', 'jpeg', 'gif'];
|
const allowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
|
||||||
|
|
||||||
bool isValidImageUrl(String? url) {
|
bool isValidImageUrl(String? url) {
|
||||||
if (url == null) return false;
|
if (url == null) return false;
|
||||||
@ -11,7 +11,7 @@ extension UserDtoToModel on UserDto {
|
|||||||
return allowedExtensions.contains(extension);
|
return allowedExtensions.contains(extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CardPostData(
|
return CardData(
|
||||||
name: name ?? "",
|
name: name ?? "",
|
||||||
surname: surname ?? "",
|
surname: surname ?? "",
|
||||||
description: description ?? "",
|
description: description ?? "",
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import 'package:pmu/domain/card.dart';
|
import 'package:pmu/domain/models/home.dart';
|
||||||
|
|
||||||
|
typedef OnErrorCallback = void Function(String? error);
|
||||||
|
|
||||||
abstract class ApiInterface {
|
abstract class ApiInterface {
|
||||||
Future<List<CardPostData>?> loadData();
|
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||||
}
|
}
|
||||||
|
53
lib/data/repositories/api_repository.dart
Normal file
53
lib/data/repositories/api_repository.dart
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import 'package:pmu/data/dto/page_dto.dart';
|
||||||
|
import 'package:pmu/data/mappers/user_mapper.dart';
|
||||||
|
import 'package:pmu/data/repositories/api_interface.dart';
|
||||||
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:pmu/domain/models/home.dart';
|
||||||
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
|
class ApiRepository extends ApiInterface {
|
||||||
|
static final Dio _dio = Dio()
|
||||||
|
..interceptors.add(PrettyDioLogger(requestHeader: true, requestBody: true));
|
||||||
|
static const String _baseUrl = 'http://lovesearch-api.nspotapov.ru';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<HomeData?> loadData({
|
||||||
|
OnErrorCallback? onError,
|
||||||
|
String? q,
|
||||||
|
int pageNumber = 0,
|
||||||
|
int itemsByPage = 5,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
const String url = '$_baseUrl/api/users/';
|
||||||
|
final List<CardData> data = [];
|
||||||
|
Map<String, dynamic> queryParameters = {
|
||||||
|
'pageNumber': pageNumber,
|
||||||
|
'itemsByPage': itemsByPage
|
||||||
|
};
|
||||||
|
if (q != null) {
|
||||||
|
queryParameters['q'] = q;
|
||||||
|
}
|
||||||
|
final Response<dynamic> response = await _dio
|
||||||
|
.get<Map<dynamic, dynamic>>(url, queryParameters: queryParameters);
|
||||||
|
final PageDto pageDto =
|
||||||
|
PageDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
|
int itemsCount = pageDto.itemsCount ?? 0;
|
||||||
|
for (int i = 0; i < itemsCount; i++) {
|
||||||
|
final CardData cardPost = pageDto.items![i].toDomain();
|
||||||
|
data.add(cardPost);
|
||||||
|
}
|
||||||
|
|
||||||
|
HomeData homeData = HomeData(
|
||||||
|
data: data,
|
||||||
|
pageNumber: pageDto.pageNumber,
|
||||||
|
nextPageNumber: pageDto.nextPageNumber);
|
||||||
|
|
||||||
|
return homeData;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
onError?.call(e.response?.statusMessage);
|
||||||
|
onError?.call(e.error?.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,30 +0,0 @@
|
|||||||
import 'package:pmu/data/dto/page_dto.dart';
|
|
||||||
import 'package:pmu/data/dto/user_dto.dart';
|
|
||||||
import 'package:pmu/data/mappers/user_mapper.dart';
|
|
||||||
import 'package:pmu/data/repositories/api_interface.dart';
|
|
||||||
import 'package:pmu/domain/card.dart';
|
|
||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
|
||||||
|
|
||||||
class ApiUserRepository extends ApiInterface {
|
|
||||||
static final Dio _dio = Dio()
|
|
||||||
..interceptors.add(PrettyDioLogger(requestHeader: true, requestBody: true));
|
|
||||||
static const String _baseUrl =
|
|
||||||
'http://lovesearch-api.nspotapov.ru/api/users/';
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<List<CardPostData>?> loadData() async {
|
|
||||||
const String url = _baseUrl;
|
|
||||||
final List<CardPostData> data = [];
|
|
||||||
final Response<dynamic> response =
|
|
||||||
await _dio.get<Map<dynamic, dynamic>>(url);
|
|
||||||
final PageDto pageDto =
|
|
||||||
PageDto.fromJson(response.data as Map<String, dynamic>);
|
|
||||||
int itemsCount = pageDto.itemsCount ?? 0;
|
|
||||||
for (int i = 0; i < itemsCount; i++) {
|
|
||||||
final CardPostData cardPost = pageDto.items![i].toDomain();
|
|
||||||
data.add(cardPost);
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +1,12 @@
|
|||||||
import 'package:pmu/data/repositories/api_interface.dart';
|
import 'package:pmu/data/repositories/api_interface.dart';
|
||||||
import 'package:pmu/domain/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import 'package:pmu/domain/models/home.dart';
|
||||||
|
|
||||||
class MockUserRepository extends ApiInterface {
|
class MockRepository extends ApiInterface {
|
||||||
@override
|
@override
|
||||||
Future<List<CardPostData>?> loadData() async {
|
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||||
return [
|
return HomeData(data: [
|
||||||
const CardPostData(
|
const CardData(
|
||||||
name: "Марьяна",
|
name: "Марьяна",
|
||||||
surname: "Ро",
|
surname: "Ро",
|
||||||
description: "Люблю вечерами погамать в майн",
|
description: "Люблю вечерами погамать в майн",
|
||||||
@ -13,7 +14,7 @@ class MockUserRepository extends ApiInterface {
|
|||||||
age: 21,
|
age: 21,
|
||||||
distance: 24.5,
|
distance: 24.5,
|
||||||
isLiked: false),
|
isLiked: false),
|
||||||
const CardPostData(
|
const CardData(
|
||||||
name: "Константин",
|
name: "Константин",
|
||||||
surname: "Злобин",
|
surname: "Злобин",
|
||||||
description: "Веду канал в тг про криптожизнь",
|
description: "Веду канал в тг про криптожизнь",
|
||||||
@ -21,6 +22,6 @@ class MockUserRepository extends ApiInterface {
|
|||||||
age: 24,
|
age: 24,
|
||||||
distance: 478.3,
|
distance: 478.3,
|
||||||
isLiked: false)
|
isLiked: false)
|
||||||
];
|
], pageNumber: 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
class CardPostData {
|
class CardData {
|
||||||
final String? name;
|
final String? name;
|
||||||
final String? surname;
|
final String? surname;
|
||||||
final String? description;
|
final String? description;
|
||||||
@ -7,7 +7,7 @@ class CardPostData {
|
|||||||
final double? distance;
|
final double? distance;
|
||||||
final bool? isLiked;
|
final bool? isLiked;
|
||||||
|
|
||||||
const CardPostData(
|
const CardData(
|
||||||
{this.name,
|
{this.name,
|
||||||
this.surname,
|
this.surname,
|
||||||
this.description,
|
this.description,
|
9
lib/domain/models/home.dart
Normal file
9
lib/domain/models/home.dart
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import 'card.dart';
|
||||||
|
|
||||||
|
class HomeData {
|
||||||
|
final List<CardData>? data;
|
||||||
|
final int? pageNumber;
|
||||||
|
final int? nextPageNumber;
|
||||||
|
|
||||||
|
HomeData({this.data, this.pageNumber, this.nextPageNumber});
|
||||||
|
}
|
@ -1,4 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu/data/repositories/api_repository.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';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -10,9 +13,15 @@ class MyApp extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const MaterialApp(
|
return MaterialApp(
|
||||||
title: 'Meets',
|
title: 'LoveSearch',
|
||||||
home: MyHomePage(title: 'Знакомства'),
|
home: RepositoryProvider<ApiRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => ApiRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) => HomeBloc(context.read<ApiRepository>()),
|
||||||
|
child: const HomePage())),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:pmu/domain/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
|
||||||
class DetailPage extends StatelessWidget {
|
class DetailPage extends StatelessWidget {
|
||||||
final CardPostData data;
|
final CardData data;
|
||||||
|
|
||||||
const DetailPage(this.data, {super.key});
|
const DetailPage(this.data, {super.key});
|
||||||
|
|
||||||
|
42
lib/presentation/home_page/bloc/bloc.dart
Normal file
42
lib/presentation/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu/data/repositories/api_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 ApiRepository repository;
|
||||||
|
|
||||||
|
HomeBloc(this.repository) : super(const HomeState()) {
|
||||||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onLoadData(
|
||||||
|
HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||||
|
if (event.nextPageNumber == null) {
|
||||||
|
emit(state.copyWith(isLoading: true));
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(isPaginationLoading: true));
|
||||||
|
}
|
||||||
|
|
||||||
|
String? error;
|
||||||
|
|
||||||
|
final data = await repository.loadData(
|
||||||
|
q: event.search,
|
||||||
|
pageNumber: event.nextPageNumber ?? 0,
|
||||||
|
onError: (e) => error = e,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (event.nextPageNumber != null) {
|
||||||
|
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
isPaginationLoading: false,
|
||||||
|
data: data,
|
||||||
|
error: error,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
11
lib/presentation/home_page/bloc/events.dart
Normal file
11
lib/presentation/home_page/bloc/events.dart
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
abstract class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomeLoadDataEvent extends HomeEvent {
|
||||||
|
final String? search;
|
||||||
|
final int? pageNumber;
|
||||||
|
final int? nextPageNumber;
|
||||||
|
|
||||||
|
const HomeLoadDataEvent({this.search, this.pageNumber, this.nextPageNumber});
|
||||||
|
}
|
28
lib/presentation/home_page/bloc/state.dart
Normal file
28
lib/presentation/home_page/bloc/state.dart
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:pmu/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,14 +1,11 @@
|
|||||||
import 'dart:async';
|
part of 'home_page.dart';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:pmu/domain/card.dart';
|
|
||||||
|
|
||||||
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
|
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
|
||||||
|
|
||||||
const double NORMAL_ICON_SCALE = 2.0;
|
const double NORMAL_ICON_SCALE = 2.0;
|
||||||
const double SCALED_ICON_SCALE = 2.5;
|
const double SCALED_ICON_SCALE = 2.5;
|
||||||
|
|
||||||
class CardPost extends StatefulWidget {
|
class _Card extends StatefulWidget {
|
||||||
final String? description;
|
final String? description;
|
||||||
final String? imageUrl;
|
final String? imageUrl;
|
||||||
final bool? isLiked;
|
final bool? isLiked;
|
||||||
@ -18,7 +15,7 @@ class CardPost extends StatefulWidget {
|
|||||||
final OnLikeCallback onLike;
|
final OnLikeCallback onLike;
|
||||||
final VoidCallback? onTap;
|
final VoidCallback? onTap;
|
||||||
|
|
||||||
const CardPost(
|
const _Card(
|
||||||
{this.name,
|
{this.name,
|
||||||
this.surname,
|
this.surname,
|
||||||
this.description,
|
this.description,
|
||||||
@ -27,9 +24,9 @@ class CardPost extends StatefulWidget {
|
|||||||
this.onLike,
|
this.onLike,
|
||||||
this.onTap});
|
this.onTap});
|
||||||
|
|
||||||
factory CardPost.fromData(CardPostData data,
|
factory _Card.fromData(CardData data,
|
||||||
{OnLikeCallback onLike, VoidCallback? onTap}) =>
|
{OnLikeCallback onLike, VoidCallback? onTap}) =>
|
||||||
CardPost(
|
_Card(
|
||||||
name: data.name,
|
name: data.name,
|
||||||
surname: data.surname,
|
surname: data.surname,
|
||||||
description: data.description,
|
description: data.description,
|
||||||
@ -39,10 +36,10 @@ class CardPost extends StatefulWidget {
|
|||||||
onTap: onTap);
|
onTap: onTap);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<CardPost> createState() => _CardPostState();
|
State<_Card> createState() => _CardState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _CardPostState extends State<CardPost> {
|
class _CardState extends State<_Card> {
|
||||||
bool isLiked = false;
|
bool isLiked = false;
|
||||||
|
|
||||||
double iconScale = NORMAL_ICON_SCALE;
|
double iconScale = NORMAL_ICON_SCALE;
|
||||||
@ -124,13 +121,6 @@ class _CardPostState extends State<CardPost> {
|
|||||||
onTap: () => {
|
onTap: () => {
|
||||||
setState(() {
|
setState(() {
|
||||||
isLiked = !isLiked;
|
isLiked = !isLiked;
|
||||||
iconScale = SCALED_ICON_SCALE;
|
|
||||||
|
|
||||||
Timer(const Duration(milliseconds: 110), () {
|
|
||||||
setState(() {
|
|
||||||
iconScale = NORMAL_ICON_SCALE;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
widget.onLike?.call(widget.name ?? "", isLiked);
|
widget.onLike?.call(widget.name ?? "", isLiked);
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -1,36 +1,139 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:pmu/data/repositories/api_user_repository.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:pmu/domain/card.dart';
|
import 'package:pmu/components/debounce.dart';
|
||||||
|
import 'package:pmu/domain/models/card.dart';
|
||||||
import 'package:pmu/presentation/detail_pages/card_detail_page.dart';
|
import 'package:pmu/presentation/detail_pages/card_detail_page.dart';
|
||||||
import 'package:pmu/presentation/home_page/card.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';
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
part 'card.dart';
|
||||||
const MyHomePage({super.key, required this.title});
|
|
||||||
|
|
||||||
final String title;
|
class HomePage extends StatefulWidget {
|
||||||
|
const HomePage({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<MyHomePage> createState() => _MyHomePageState();
|
State<HomePage> createState() => _HomePageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _HomePageState extends State<HomePage> {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return const Scaffold(body: _Body());
|
||||||
appBar: AppBar(
|
}
|
||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
}
|
||||||
title: Text(widget.title),
|
|
||||||
|
class _Body extends StatefulWidget {
|
||||||
|
const _Body();
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<StatefulWidget> createState() => _BodyState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _BodyState extends State<_Body> {
|
||||||
|
final searchController = TextEditingController();
|
||||||
|
final scrollController = ScrollController();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
});
|
||||||
|
|
||||||
|
scrollController.addListener(_onNextPageListener);
|
||||||
|
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onNextPageListener() {
|
||||||
|
if (scrollController.offset >= scrollController.position.maxScrollExtent) {
|
||||||
|
// preventing multiple pagination request on multiple swipes
|
||||||
|
final bloc = context.read<HomeBloc>();
|
||||||
|
if (!bloc.state.isPaginationLoading) {
|
||||||
|
bloc.add(HomeLoadDataEvent(
|
||||||
|
search: searchController.text,
|
||||||
|
nextPageNumber: bloc.state.data?.nextPageNumber,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
searchController.dispose();
|
||||||
|
scrollController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Padding(
|
||||||
|
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
child: CupertinoSearchTextField(
|
||||||
|
controller: searchController,
|
||||||
|
onChanged: (search) {
|
||||||
|
Debounce.run(() => context
|
||||||
|
.read<HomeBloc>()
|
||||||
|
.add(HomeLoadDataEvent(search: search)));
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.error != null
|
||||||
|
? Text(
|
||||||
|
state.error ?? '',
|
||||||
|
style: Theme.of(context)
|
||||||
|
.textTheme
|
||||||
|
.headlineSmall
|
||||||
|
?.copyWith(color: Colors.red),
|
||||||
|
)
|
||||||
|
: state.isLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: Expanded(
|
||||||
|
child: RefreshIndicator(
|
||||||
|
onRefresh: _onRefresh,
|
||||||
|
child: ListView.builder(
|
||||||
|
controller: scrollController,
|
||||||
|
padding: EdgeInsets.zero,
|
||||||
|
itemCount: state.data?.data?.length ?? 0,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final data = state.data?.data?[index];
|
||||||
|
return data != null
|
||||||
|
? _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(),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
body: const Body(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onRefresh() {
|
||||||
|
context
|
||||||
|
.read<HomeBloc>()
|
||||||
|
.add(HomeLoadDataEvent(search: searchController.text));
|
||||||
|
return Future.value(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Body extends StatelessWidget {
|
void _navToDetails(BuildContext context, CardData data) {
|
||||||
const Body({super.key});
|
|
||||||
|
|
||||||
void _navToDetails(BuildContext context, CardPostData data) {
|
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
CupertinoPageRoute(builder: (context) => DetailPage(data)),
|
CupertinoPageRoute(builder: (context) => DetailPage(data)),
|
||||||
@ -50,27 +153,28 @@ class Body extends StatelessWidget {
|
|||||||
));
|
));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
//
|
||||||
Widget build(BuildContext context) {
|
// @override
|
||||||
final data = ApiUserRepository().loadData();
|
// Widget build(BuildContext context) {
|
||||||
return Center(
|
// final data = ApiRepository().loadData();
|
||||||
child: FutureBuilder<List<CardPostData>?>(
|
// return Center(
|
||||||
future: data,
|
// child: FutureBuilder<List<CardData>?>(
|
||||||
builder: (context, snapshot) => SingleChildScrollView(
|
// future: data,
|
||||||
child: snapshot.hasData
|
// builder: (context, snapshot) => SingleChildScrollView(
|
||||||
? Column(
|
// child: snapshot.hasData
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
// ? Column(
|
||||||
children: snapshot.data
|
// mainAxisAlignment: MainAxisAlignment.center,
|
||||||
?.map((data) => CardPost.fromData(data,
|
// children: snapshot.data
|
||||||
onLike: (String title, bool isLiked) =>
|
// ?.map((data) => CardPost.fromData(data,
|
||||||
_showSnackBar(context, title, isLiked),
|
// onLike: (String title, bool isLiked) =>
|
||||||
onTap: () => _navToDetails(context, data)))
|
// _showSnackBar(context, title, isLiked),
|
||||||
.toList() ??
|
// onTap: () => _navToDetails(context, data)))
|
||||||
[])
|
// .toList() ??
|
||||||
: const CircularProgressIndicator(),
|
// [])
|
||||||
),
|
// : const CircularProgressIndicator(),
|
||||||
),
|
// ),
|
||||||
);
|
// ),
|
||||||
}
|
// );
|
||||||
}
|
// }
|
||||||
|
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.2"
|
version: "3.1.2"
|
||||||
|
copy_with_extension:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: copy_with_extension
|
||||||
|
sha256: ed472ae80d807094d7a7d7ef67901f8167d18c7998e6db81785a51364aede627
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.0.0"
|
||||||
|
copy_with_extension_gen:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: copy_with_extension_gen
|
||||||
|
sha256: "0be2694d3d50df16d91c04e7444181bfb2a0ad8a3b169d58de0c38a69864e4bd"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.0.0"
|
||||||
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: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.7"
|
||||||
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:
|
||||||
|
@ -35,10 +35,16 @@ dependencies:
|
|||||||
pretty_dio_logger: ^1.3.1
|
pretty_dio_logger: ^1.3.1
|
||||||
json_annotation: ^4.8.1
|
json_annotation: ^4.8.1
|
||||||
|
|
||||||
|
copy_with_extension: ^6.0.0
|
||||||
|
copy_with_extension_gen: ^6.0.0
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
|
flutter_bloc: ^8.1.6
|
||||||
|
equatable: ^2.0.7
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
Loading…
Reference in New Issue
Block a user