2024-09-29 20:01:48 +04:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-27 01:34:35 +04:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-10-29 14:48:26 +04:00
|
|
|
import 'package:laba1/components/extensions/context_x.dart';
|
2024-10-01 18:21:37 +04:00
|
|
|
import 'package:laba1/data/repositories/potter_repository.dart';
|
2024-09-29 20:01:48 +04:00
|
|
|
import 'package:laba1/presentation/details_page/details_page.dart';
|
2024-10-29 14:48:26 +04:00
|
|
|
import 'package:laba1/presentation/header.dart';
|
2024-09-29 20:01:48 +04:00
|
|
|
|
2024-10-27 21:53:28 +04:00
|
|
|
import '../../components/utils/debounce.dart';
|
2024-10-01 18:21:37 +04:00
|
|
|
import '../../data/repositories/mock_repository.dart';
|
2024-09-29 20:01:48 +04:00
|
|
|
import '../../domain/models/card.dart';
|
2024-10-29 14:48:26 +04:00
|
|
|
import '../comon/svg_objects.dart';
|
2024-10-01 18:21:37 +04:00
|
|
|
import '../dialogs/show_dialog.dart';
|
2024-10-29 14:48:26 +04:00
|
|
|
import '../like_bloc/like_bloc.dart';
|
|
|
|
import '../like_bloc/like_event.dart';
|
|
|
|
import '../like_bloc/like_state.dart';
|
|
|
|
import '../locale_bloc/locale_bloc.dart';
|
|
|
|
import '../locale_bloc/locale_events.dart';
|
|
|
|
import '../locale_bloc/locale_state.dart';
|
2024-10-27 01:34:35 +04:00
|
|
|
import 'bloc/bloc.dart';
|
|
|
|
import 'bloc/events.dart';
|
|
|
|
import 'bloc/state.dart';
|
2024-09-29 20:01:48 +04:00
|
|
|
|
|
|
|
part 'card.dart';
|
|
|
|
|
2024-10-27 01:34:35 +04:00
|
|
|
class MyHomePage extends StatefulWidget {
|
2024-09-29 20:01:48 +04:00
|
|
|
const MyHomePage({super.key, required this.title});
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
2024-10-27 01:34:35 +04:00
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
2024-09-29 20:01:48 +04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-10-27 01:34:35 +04:00
|
|
|
title: Text(widget.title),
|
2024-10-01 00:37:49 +04:00
|
|
|
backgroundColor: Colors.amber,
|
2024-09-29 20:01:48 +04:00
|
|
|
),
|
|
|
|
backgroundColor: Colors.yellow,
|
2024-10-27 01:34:35 +04:00
|
|
|
body: const _Body());
|
2024-09-29 20:01:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 01:34:35 +04:00
|
|
|
class _Body extends StatefulWidget {
|
|
|
|
const _Body({Key? key}) : super(key: key);
|
2024-09-29 20:01:48 +04:00
|
|
|
|
2024-10-01 18:21:37 +04:00
|
|
|
@override
|
2024-10-27 01:34:35 +04:00
|
|
|
State<_Body> createState() => _BodyState();
|
2024-10-01 18:21:37 +04:00
|
|
|
}
|
|
|
|
|
2024-10-27 01:34:35 +04:00
|
|
|
class _BodyState extends State<_Body> {
|
2024-10-01 18:21:37 +04:00
|
|
|
final searchController = TextEditingController();
|
2024-10-27 21:53:28 +04:00
|
|
|
final scrollController = ScrollController();
|
2024-10-01 18:21:37 +04:00
|
|
|
|
2024-09-29 20:01:48 +04:00
|
|
|
@override
|
2024-10-01 22:57:57 +04:00
|
|
|
void initState() {
|
2024-10-29 14:48:26 +04:00
|
|
|
SvgObjects.init();
|
2024-10-27 01:34:35 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
2024-10-29 14:48:26 +04:00
|
|
|
context.read<LikeBloc>().add(const LoadLikesEvent());
|
2024-10-27 01:34:35 +04:00
|
|
|
});
|
2024-10-27 21:53:28 +04:00
|
|
|
scrollController.addListener(_onNextPageListener);
|
2024-10-01 22:57:57 +04:00
|
|
|
super.initState();
|
|
|
|
}
|
2024-09-29 20:01:48 +04:00
|
|
|
|
2024-10-27 21:53:28 +04:00
|
|
|
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,
|
|
|
|
nextPage: bloc.state.data?.nextPage,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 01:34:35 +04:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
searchController.dispose();
|
2024-10-27 21:53:28 +04:00
|
|
|
scrollController.dispose();
|
2024-10-27 01:34:35 +04:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2024-10-01 22:57:57 +04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-10-27 22:23:28 +04:00
|
|
|
return Center(
|
|
|
|
child: Stack(
|
2024-10-01 22:57:57 +04:00
|
|
|
children: [
|
2024-10-27 21:53:28 +04:00
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.error != null
|
|
|
|
? Text(
|
|
|
|
state.error ?? '',
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.headlineSmall
|
|
|
|
?.copyWith(color: Colors.red),
|
|
|
|
)
|
|
|
|
: state.isLoading
|
2024-10-27 22:23:28 +04:00
|
|
|
? Center(child: CircularProgressIndicator())
|
2024-10-29 14:48:26 +04:00
|
|
|
: RefreshIndicator(
|
|
|
|
onRefresh: _onRefresh,
|
|
|
|
child: ListView.builder(
|
|
|
|
controller: scrollController,
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
itemCount: (state.data?.data?.length ?? 0) + 1,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
if (index == 0) {
|
|
|
|
return SizedBox(height: 40);
|
|
|
|
}
|
|
|
|
final data = state.data?.data?[index - 1];
|
|
|
|
return data != null
|
|
|
|
? BlocBuilder<LikeBloc, LikeState>(
|
|
|
|
builder: (context, likeState) {
|
|
|
|
return _Card.fromData(
|
2024-10-27 21:53:28 +04:00
|
|
|
data,
|
2024-10-29 14:48:26 +04:00
|
|
|
onLike: _onLike,
|
|
|
|
isLiked: likeState.likedIds
|
|
|
|
?.contains(data.id) ==
|
|
|
|
true,
|
2024-10-27 21:53:28 +04:00
|
|
|
onTap: () => _navToDetails(context, data),
|
2024-10-29 14:48:26 +04:00
|
|
|
);
|
|
|
|
})
|
|
|
|
: const SizedBox.expand();
|
|
|
|
},
|
2024-10-27 21:53:28 +04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-10-29 14:48:26 +04:00
|
|
|
Align(
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
child: Header(
|
|
|
|
searchController: searchController,
|
|
|
|
placeholder: context.locale.search,
|
|
|
|
onChanged: (search) {
|
|
|
|
Debounce.run(() => context
|
|
|
|
.read<HomeBloc>()
|
|
|
|
.add(HomeLoadDataEvent(search: search)));
|
|
|
|
},
|
|
|
|
)),
|
2024-10-27 22:23:28 +04:00
|
|
|
Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: SizedBox(
|
|
|
|
height: 50,
|
|
|
|
width: 50,
|
|
|
|
child: BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.isPaginationLoading
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const SizedBox.shrink(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-10-27 21:53:28 +04:00
|
|
|
),
|
2024-10-01 22:57:57 +04:00
|
|
|
],
|
2024-09-29 20:01:48 +04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-29 14:48:26 +04:00
|
|
|
void _onLike(String? id, String title, bool isLiked) {
|
|
|
|
if (id != null) {
|
|
|
|
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
|
|
|
_showSnackBar(context, title, !isLiked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-27 21:53:28 +04:00
|
|
|
Future<void> _onRefresh() {
|
|
|
|
context
|
|
|
|
.read<HomeBloc>()
|
|
|
|
.add(HomeLoadDataEvent(search: searchController.text));
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
|
|
|
|
2024-09-29 20:01:48 +04:00
|
|
|
void _navToDetails(BuildContext context, CardData data) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-27 01:34:35 +04:00
|
|
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
2024-09-29 20:01:48 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
2024-10-29 14:48:26 +04:00
|
|
|
'$title ${isLiked ? context.locale.liked : context.locale.unliked}',
|
2024-10-27 21:53:28 +04:00
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
2024-09-29 20:01:48 +04:00
|
|
|
),
|
|
|
|
backgroundColor: Colors.orangeAccent,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|