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

165 lines
4.2 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';
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';
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);
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final data = [
CommentModel(
title: 'Oh my god',
text: 'this app is so cool ' * 10,
user: UserModel(
nickname: 'Steve',
avatarUrl:
'https://preview.free3d.com/img/2016/03/1875481443430303321/hld8c0oa.jpg',
),
),
CommentModel(
title: 'Listen to me',
text: 'This app is like Half-Life 3 - it will never be released',
user: UserModel(
nickname: 'G-Man',
avatarUrl:
'https://us-tuna-sounds-images.voicemod.net/356aeabd-18d5-40d8-af31-b479f4eff183-1704672174928.png',
),
),
CommentModel(
title: 'BREAKING!!!',
text:
'ONLY NOW you can purchase the ability to put likes and dislikes for only 299\$',
user: UserModel(
nickname: 'Bethesda',
avatarUrl: 'https://i.playground.ru/p/BWixorSTeZQfoPvdVL9lgA.jpeg',
),
),
CommentModel(
title: 'Test',
text: 'Test',
user: UserModel(
nickname: 'Noname',
),
),
CommentModel(
title: 'Test',
text: 'Test',
user: UserModel(
nickname: 'Noname',
),
),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((data) {
return _Comment.fromData(
data,
onAction: (String nickname, bool isLiked, bool isDisliked) =>
_showSnackBar(context, nickname, isLiked, isDisliked),
onTap: () => _navToDetails(context, data),
);
}).toList(),
),
),
);
}
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),
));
});
}
}