2024-10-16 19:52:56 +04:00
|
|
|
import 'package:app/components/utils/debounce.dart';
|
|
|
|
import 'package:app/main.dart';
|
|
|
|
import 'package:app/presentation/home_page/bloc/bloc.dart';
|
|
|
|
import 'package:app/presentation/home_page/bloc/events.dart';
|
|
|
|
import 'package:app/presentation/home_page/bloc/state.dart';
|
|
|
|
//import 'package:app/repositories/potter_repository.dart';
|
2024-10-16 16:41:27 +04:00
|
|
|
import 'package:app/repositories/rick&morty_repositoty.dart';
|
2024-10-02 21:32:18 +04:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-08 01:29:38 +04:00
|
|
|
import 'package:app/domain/models/card.dart';
|
|
|
|
import 'package:app/presentation/details_page/details_page.dart';
|
2024-10-16 19:52:56 +04:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-10-02 21:32:18 +04:00
|
|
|
|
|
|
|
part 'card.dart';
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
|
|
const MyHomePage({super.key, required this.title});
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-10-08 01:29:38 +04:00
|
|
|
return const Scaffold(body: Body());
|
2024-10-02 21:32:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-08 01:29:38 +04:00
|
|
|
class Body extends StatefulWidget {
|
2024-10-16 19:52:56 +04:00
|
|
|
const Body();
|
2024-10-02 21:32:18 +04:00
|
|
|
|
|
|
|
@override
|
2024-10-08 01:29:38 +04:00
|
|
|
State<Body> createState() => _BodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BodyState extends State<Body> {
|
|
|
|
final searchController = TextEditingController();
|
2024-10-16 19:52:56 +04:00
|
|
|
final scrollController = ScrollController();
|
2024-10-08 01:29:38 +04:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2024-10-16 19:52:56 +04:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
|
|
|
});
|
|
|
|
scrollController.addListener(_onNextPageListener);
|
|
|
|
|
2024-10-08 01:29:38 +04:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-10-16 19:52:56 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-10-08 01:29:38 +04:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
2024-10-02 21:32:18 +04:00
|
|
|
child: Column(
|
2024-10-08 01:29:38 +04:00
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
child: CupertinoSearchTextField(
|
|
|
|
controller: searchController,
|
|
|
|
onChanged: (search) {
|
2024-10-16 19:52:56 +04:00
|
|
|
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
2024-10-08 01:29:38 +04:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2024-10-16 19:52:56 +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
|
|
|
|
? 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();
|
|
|
|
},
|
2024-10-08 01:29:38 +04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-10-16 19:52:56 +04:00
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) => state.isPaginationLoading
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
: const SizedBox.shrink(),
|
|
|
|
),
|
2024-10-08 01:29:38 +04:00
|
|
|
],
|
2024-10-02 21:32:18 +04:00
|
|
|
),
|
2024-10-08 01:29:38 +04:00
|
|
|
);
|
2024-10-02 21:32:18 +04:00
|
|
|
}
|
2024-10-16 19:52:56 +04:00
|
|
|
Future<void> _onRefresh() {
|
|
|
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
|
|
|
return Future.value(null);
|
|
|
|
}
|
2024-10-02 21:32:18 +04:00
|
|
|
|
2024-10-08 01:29:38 +04:00
|
|
|
void _navToDetails(BuildContext context, CardData data) {
|
2024-10-02 21:32:18 +04:00
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
|
|
|
'Вы ${isLiked ? 'добавили в избранное' : 'убрали из избраных'} $title',
|
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
),
|
2024-10-16 16:41:27 +04:00
|
|
|
backgroundColor: Color(0xFFFFFFFF),
|
2024-10-02 21:32:18 +04:00
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|