213 lines
6.3 KiB
Dart
213 lines
6.3 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:pmu_labworks/components/utils/debounce.dart';
|
|
import 'package:pmu_labworks/domain/models/comment.dart';
|
|
import 'package:pmu_labworks/domain/models/user.dart';
|
|
import 'package:pmu_labworks/view/details_page/details_page.dart';
|
|
import 'package:pmu_labworks/view/home_page/bloc/bloc.dart';
|
|
import 'package:pmu_labworks/view/home_page/bloc/events.dart';
|
|
import 'package:pmu_labworks/view/home_page/bloc/state.dart';
|
|
|
|
part 'comment.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key, required this.title});
|
|
|
|
final String title;
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
Color _color = Colors.transparent;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_color = _generateColor();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: _color,
|
|
title: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(widget.title,
|
|
style: const TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
)),
|
|
const Text(
|
|
'made by Factorino',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontStyle: FontStyle.italic,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
body: const _Body(),
|
|
);
|
|
}
|
|
|
|
Color _generateColor() {
|
|
final Random random = Random();
|
|
final int red = (random.nextInt(106) + 150);
|
|
final int green = (random.nextInt(106) + 150);
|
|
final int blue = (random.nextInt(106) + 150);
|
|
|
|
return Color.fromARGB(255, red, green, blue);
|
|
}
|
|
}
|
|
|
|
class _Body extends StatefulWidget {
|
|
const _Body();
|
|
|
|
@override
|
|
State<_Body> createState() => _BodyState();
|
|
}
|
|
|
|
class _BodyState extends State<_Body> {
|
|
final searchController = TextEditingController();
|
|
final scrollController = ScrollController();
|
|
|
|
@override
|
|
void initState() {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
|
});
|
|
|
|
scrollController.addListener(_onNextPageListener);
|
|
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
searchController.dispose();
|
|
scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@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.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
|
|
? _Comment.fromData(
|
|
data,
|
|
onAction:
|
|
(nickname, isLiked, isDisliked) =>
|
|
_showSnackBar(context, nickname,
|
|
isLiked, isDisliked),
|
|
onTap: () => _navToDetails(context, data),
|
|
)
|
|
: const SizedBox.shrink();
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
BlocBuilder<HomeBloc, HomeState>(
|
|
builder: (context, state) => state.isPaginationLoading
|
|
? const CircularProgressIndicator()
|
|
: const SizedBox.shrink(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _onRefresh() {
|
|
context
|
|
.read<HomeBloc>()
|
|
.add(HomeLoadDataEvent(search: searchController.text));
|
|
return Future.value(null);
|
|
}
|
|
|
|
void _navToDetails(BuildContext context, CommentData data) {
|
|
Navigator.push(
|
|
context,
|
|
CupertinoPageRoute(builder: (context) => DetailsPage(data)),
|
|
);
|
|
}
|
|
|
|
void _showSnackBar(
|
|
BuildContext context, String nickname, bool isLiked, bool isDisliked) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
final String action;
|
|
|
|
if (isLiked) {
|
|
action = 'liked';
|
|
} else if (isDisliked) {
|
|
action = 'disliked';
|
|
} else {
|
|
action = 'neutralized'; // Состояние без лайков/дизлайков
|
|
}
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
content: Text(
|
|
'$nickname\'s comment $action',
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
),
|
|
backgroundColor: Colors.white70,
|
|
duration: const Duration(seconds: 1),
|
|
));
|
|
});
|
|
}
|
|
|
|
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,
|
|
));
|
|
}
|
|
}
|
|
}
|
|
}
|