2024-10-29 18:39:40 +04:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
2024-11-19 23:39:56 +04:00
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-11-15 21:47:58 +04:00
|
|
|
|
import 'package:mobiles_labs_5th_semester/data/repositories/mock_repository.dart';
|
2024-10-29 18:39:40 +04:00
|
|
|
|
import 'package:mobiles_labs_5th_semester/domain/models/game.dart';
|
2024-11-19 23:39:56 +04:00
|
|
|
|
import 'package:mobiles_labs_5th_semester/main.dart';
|
2024-10-29 18:39:40 +04:00
|
|
|
|
import 'package:mobiles_labs_5th_semester/presentation/details_page/details_page.dart';
|
2024-11-19 23:39:56 +04:00
|
|
|
|
import 'package:mobiles_labs_5th_semester/presentation/home_page/bloc/bloc.dart';
|
|
|
|
|
import 'package:mobiles_labs_5th_semester/presentation/home_page/bloc/events.dart';
|
|
|
|
|
import 'package:mobiles_labs_5th_semester/presentation/home_page/bloc/state.dart';
|
2024-10-29 18:39:40 +04:00
|
|
|
|
|
2024-11-19 23:39:56 +04:00
|
|
|
|
import '../../components/utils/debounce.dart';
|
2024-11-15 21:47:58 +04:00
|
|
|
|
import '../../data/repositories/games_repository.dart';
|
2024-11-19 23:39:56 +04:00
|
|
|
|
import 'bloc/state.dart';
|
2024-11-15 21:47:58 +04:00
|
|
|
|
|
2024-10-29 18:39:40 +04:00
|
|
|
|
part 'gameCard.dart';
|
|
|
|
|
|
2024-11-19 23:39:56 +04:00
|
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
|
const HomePage({super.key, required this.title});
|
2024-10-29 18:39:40 +04:00
|
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
|
|
@override
|
2024-11-19 23:39:56 +04:00
|
|
|
|
State<HomePage> createState() => _HomePageState();
|
2024-10-29 18:39:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 23:39:56 +04:00
|
|
|
|
class _HomePageState extends State<HomePage> {
|
2024-11-15 21:47:58 +04:00
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 18:39:40 +04:00
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Scaffold(
|
|
|
|
|
backgroundColor: Color.fromARGB(255, 46, 65, 80),
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
backgroundColor: Color.fromARGB(255, 56, 90, 128),
|
|
|
|
|
title: Text(
|
|
|
|
|
widget.title,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
color: Colors.white, fontWeight: FontWeight.bold),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
body: const Body());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 00:37:34 +04:00
|
|
|
|
|
2024-11-15 21:47:58 +04:00
|
|
|
|
class Body extends StatefulWidget {
|
2024-10-29 18:39:40 +04:00
|
|
|
|
const Body({super.key});
|
|
|
|
|
|
2024-11-15 21:47:58 +04:00
|
|
|
|
@override
|
|
|
|
|
State<Body> createState() => _BodyState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _BodyState extends State<Body> {
|
|
|
|
|
late Future<List<GameData>?> data;
|
|
|
|
|
final searchController = TextEditingController();
|
2024-11-19 23:39:56 +04:00
|
|
|
|
final scrollController = ScrollController();
|
|
|
|
|
|
|
|
|
|
// final repo = GamesRepository();
|
2024-11-15 21:47:58 +04:00
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
2024-11-19 23:39:56 +04:00
|
|
|
|
//добавление кастомного события
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
scrollController.addListener(_onNextPageListener);
|
2024-11-15 21:47:58 +04:00
|
|
|
|
super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 23:39: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-11-15 21:47:58 +04:00
|
|
|
|
|
2024-10-29 18:39:40 +04:00
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2024-11-15 21:47:58 +04:00
|
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(top: 12, left: 12, right: 12),
|
|
|
|
|
child: CupertinoSearchTextField(
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
style: TextStyle(color: Color.fromARGB(255, 46, 65, 80)),
|
|
|
|
|
// borderRadius: const BorderRadiusTween(2.0),
|
|
|
|
|
controller: searchController,
|
|
|
|
|
onChanged: (search) {
|
2024-11-19 23:39:56 +04:00
|
|
|
|
Debounce.run(() => context
|
|
|
|
|
.read<HomeBloc>()
|
|
|
|
|
.add(HomeLoadDataEvent(search: search)));
|
2024-11-15 21:47:58 +04:00
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
2024-11-19 23:39: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,
|
|
|
|
|
itemCount: state.data?.data?.length ?? 0,
|
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
final data = state.data?.data?[index];
|
|
|
|
|
return data != null
|
|
|
|
|
? _GameCard.fromData(
|
|
|
|
|
data,
|
|
|
|
|
onLike: (title, isLiked) =>
|
|
|
|
|
_showSnackBar(context, title, isLiked),
|
|
|
|
|
onTap: () => _navToDetails(context, data),
|
|
|
|
|
)
|
|
|
|
|
: const SizedBox.shrink();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2024-11-15 21:47:58 +04:00
|
|
|
|
),
|
2024-11-19 23:39:56 +04:00
|
|
|
|
//значок загрузки при пагинации
|
|
|
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
|
|
|
builder: (context, state) => state.isPaginationLoading
|
|
|
|
|
? const CircularProgressIndicator()
|
|
|
|
|
: const SizedBox.shrink(),
|
|
|
|
|
)
|
2024-11-15 21:47:58 +04:00
|
|
|
|
],
|
|
|
|
|
);
|
2024-10-29 18:39:40 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 23:39:56 +04:00
|
|
|
|
Future<void> _onRefresh() {
|
|
|
|
|
context
|
|
|
|
|
.read<HomeBloc>()
|
|
|
|
|
.add(HomeLoadDataEvent(search: searchController.text));
|
|
|
|
|
return Future.value(null);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 18:39:40 +04:00
|
|
|
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
|
content: Text(
|
|
|
|
|
isLiked
|
|
|
|
|
? 'Вы поставили лайк игре "$title"'
|
|
|
|
|
: 'Вы убрали лайк у игры "$title"',
|
|
|
|
|
style: Theme.of(context)
|
|
|
|
|
.textTheme
|
|
|
|
|
.bodyLarge
|
|
|
|
|
?.copyWith(color: Colors.pink, fontWeight: FontWeight.bold),
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
),
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-15 21:47:58 +04:00
|
|
|
|
|
2024-10-29 18:39:40 +04:00
|
|
|
|
void _navToDetails(BuildContext context, GameData data) {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
2024-11-15 21:47:58 +04:00
|
|
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data.id ?? 0)),
|
2024-10-29 18:39:40 +04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|