157 lines
5.1 KiB
Dart
157 lines
5.1 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_app/components/utils/debounce.dart';
|
|
import 'package:flutter_app/data/repositories/manga_repository.dart';
|
|
import 'package:flutter_app/data/repositories/mock_repository.dart';
|
|
import 'package:flutter_app/presentation/details_page/details_page.dart';
|
|
import 'package:flutter_app/presentation/home_page/bloc/bloc.dart';
|
|
import 'package:flutter_app/presentation/home_page/bloc/events.dart';
|
|
import 'package:flutter_app/presentation/home_page/bloc/state.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../../domain/models/carddata.dart';
|
|
|
|
part 'card.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
),
|
|
body: const Body(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class Body extends StatefulWidget {
|
|
const Body({super.key});
|
|
|
|
@override
|
|
State<Body> createState() => _BodyState();
|
|
}
|
|
|
|
class _BodyState extends State<Body> {
|
|
final scrollController = ScrollController();
|
|
final searchController = TextEditingController();
|
|
|
|
@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) {
|
|
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();
|
|
}
|
|
|
|
final MangaRepository repo = MangaRepository();
|
|
var data = MangaRepository().loadData();
|
|
|
|
void _showSnackbar(BuildContext context, String title, bool isLiked) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(
|
|
'Лайк на $title ${isLiked ? 'поставлен' : 'убран'}',
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
),
|
|
backgroundColor: Colors.orangeAccent,
|
|
duration: const Duration(seconds: 1),
|
|
));
|
|
});
|
|
}
|
|
|
|
void _navToDetails(BuildContext context, CardData data) {
|
|
Navigator.push(
|
|
context, CupertinoPageRoute(builder: (context) => DetailsPage(data)));
|
|
}
|
|
|
|
Future<void> _onRefresh() {
|
|
context
|
|
.read<HomeBloc>()
|
|
.add(HomeLoadDataEvent(search: searchController.text));
|
|
return Future.value(null);
|
|
}
|
|
|
|
@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.isLoading
|
|
? 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())
|
|
],
|
|
));
|
|
}
|
|
}
|
|
|
|
class HomeData {
|
|
final List<CardData>? data;
|
|
final int? nextPage;
|
|
|
|
HomeData({this.data, this.nextPage});
|
|
}
|