PIbd-31_MasenkinMS_PMU/lib/view/home_page/home_page.dart

171 lines
4.7 KiB
Dart
Raw Normal View History

2024-09-30 02:09:26 +04:00
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2024-09-30 18:24:51 +04:00
import 'package:pmu_labworks/data/repositories/potterdb_repository.dart';
2024-09-30 02:09:26 +04:00
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';
2024-09-30 18:24:51 +04:00
import 'package:pmu_labworks/view/dialogs/show_dialog.dart';
2024-09-30 02:09:26 +04:00
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: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
)),
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);
}
}
2024-09-30 18:24:51 +04:00
class Body extends StatefulWidget {
2024-09-30 02:09:26 +04:00
const Body({super.key});
2024-09-30 18:24:51 +04:00
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final searchController = TextEditingController();
late Future<List<CommentModel>?> data;
final repo = PotterDBRepository();
@override
void initState() {
data = repo.loadData(onError: (e) => showErrorDialog(context, error: e));
super.initState();
}
@override
void dispose() {
searchController.dispose();
super.dispose();
}
2024-09-30 02:09:26 +04:00
@override
Widget build(BuildContext context) {
2024-09-30 18:24:51 +04:00
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) {
setState(() {
data = repo.loadData(q: search);
});
},
),
),
Expanded(
child: Center(
child: FutureBuilder<List<CommentModel>?>(
future: data,
builder: (context, snapshot) => SingleChildScrollView(
child: snapshot.hasData
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data?.map((data) {
return _Comment.fromData(
data,
onAction: (String nickname, bool isLiked,
bool isDisliked) =>
_showSnackBar(context, nickname, isLiked,
isDisliked),
onTap: () => _navToDetails(context, data),
);
}).toList() ??
[],
)
2024-09-30 18:24:51 +04:00
: const CircularProgressIndicator(),
),
),
),
),
],
2024-09-30 02:09:26 +04:00
),
);
}
void _navToDetails(BuildContext context, CommentModel 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),
));
});
}
}