2024-11-08 13:07:43 +04:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-11-26 01:44:56 +04:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:pmd_labs/components/utils/debounce.dart';
|
2024-11-08 13:07:43 +04:00
|
|
|
import 'package:pmd_labs/data/repositories/album_repository.dart';
|
|
|
|
import 'package:pmd_labs/details_page/detail_page.dart';
|
2024-11-26 01:44:56 +04:00
|
|
|
import 'package:pmd_labs/home_page/bloc/events.dart';
|
|
|
|
import 'package:pmd_labs/home_page/bloc/state.dart';
|
2024-11-08 13:07:43 +04:00
|
|
|
import 'package:pmd_labs/card_data.dart';
|
|
|
|
|
|
|
|
import '../data/dto/album_dto.dart';
|
2024-11-26 01:44:56 +04:00
|
|
|
import 'bloc/home_bloc.dart';
|
2024-11-08 13:07:43 +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) {
|
|
|
|
return Scaffold(
|
2024-11-26 01:44:56 +04:00
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: const Color(0xFF231a24),
|
|
|
|
title: Text(
|
|
|
|
widget.title,
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.headlineLarge
|
|
|
|
?.copyWith(color: Colors.orange),
|
2024-11-08 13:07:43 +04:00
|
|
|
),
|
2024-11-26 01:44:56 +04:00
|
|
|
),
|
|
|
|
body: Container(
|
|
|
|
color: const Color(0xFF403042),
|
|
|
|
child: const Body(), // Ваш виджет Body
|
|
|
|
),
|
2024-11-08 13:07:43 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Body extends StatefulWidget {
|
|
|
|
const Body({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<Body> createState() => _BodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BodyState extends State<Body> {
|
|
|
|
final AlbumRepository repo = AlbumRepository();
|
2024-11-26 01:44:56 +04:00
|
|
|
final ScrollController scrollController = ScrollController();
|
|
|
|
final TextEditingController searchController = TextEditingController();
|
|
|
|
late final Debounce debounce;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState(); // Настройка времени дебаунса
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
|
|
|
});
|
|
|
|
|
|
|
|
scrollController.addListener(_onNextPageListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onNextPageListener() {
|
|
|
|
if (scrollController.position.pixels >= scrollController.position.maxScrollExtent &&
|
|
|
|
!context.read<HomeBloc>().state.isPaginationLoading) {
|
|
|
|
final bloc = context.read<HomeBloc>();
|
|
|
|
bloc.add(HomeLoadDataEvent(
|
|
|
|
search: searchController.text,
|
|
|
|
nextPage: bloc.state.data?.nextPage,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 13:07:43 +04:00
|
|
|
|
|
|
|
void _showSnackbar(BuildContext context, String title, bool isLiked) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(
|
|
|
|
'Лайк на $title ${isLiked ? 'поставлен' : 'убран'}',
|
2024-11-26 01:44:56 +04:00
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
2024-11-08 13:07:43 +04:00
|
|
|
),
|
|
|
|
backgroundColor: Colors.orangeAccent,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _navToDetails(BuildContext context, CardData data) {
|
|
|
|
Navigator.push(
|
2024-11-26 01:44:56 +04:00
|
|
|
context,
|
|
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data))
|
|
|
|
);
|
2024-11-08 13:07:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
2024-11-26 01:44:56 +04:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
style: TextStyle(color: Colors.orange, fontFamily: 'Correction_Tape'),
|
2024-11-08 13:07:43 +04:00
|
|
|
),
|
2024-11-26 01:44:56 +04:00
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
if (state.isLoading) {
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
}
|
|
|
|
return RefreshIndicator(
|
|
|
|
onRefresh: () async {
|
|
|
|
// Добавим состояние загрузки
|
|
|
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
|
|
|
},
|
|
|
|
child: ListView.builder(
|
|
|
|
controller: scrollController,
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
itemCount: state.data?.data?.length ?? 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final cardData = state.data?.data?[index];
|
|
|
|
return cardData != null
|
|
|
|
? _Card.fromData(
|
|
|
|
cardData,
|
|
|
|
onLike: (title, isLiked) => _showSnackbar(context, title, isLiked),
|
|
|
|
onTap: () => _navToDetails(context, cardData),
|
2024-11-08 13:07:43 +04:00
|
|
|
)
|
2024-11-26 01:44:56 +04:00
|
|
|
: const SizedBox.shrink();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-11-08 13:07:43 +04:00
|
|
|
),
|
2024-11-26 01:44:56 +04:00
|
|
|
),
|
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
if (state.isPaginationLoading) {
|
|
|
|
return const Padding(
|
|
|
|
padding: EdgeInsets.all(8.0),
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
searchController.dispose();
|
|
|
|
scrollController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 13:07:43 +04:00
|
|
|
|
2024-11-26 01:44:56 +04:00
|
|
|
class HomeData {
|
|
|
|
final List<CardData>? data;
|
|
|
|
final int? nextPage;
|
2024-11-08 13:07:43 +04:00
|
|
|
|
2024-11-26 01:44:56 +04:00
|
|
|
HomeData({this.data, this.nextPage});
|
|
|
|
}
|