PMU-PIbd-31-Potapov-N-S/lib/presentation/home_page/home_page.dart

87 lines
2.7 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pmu/domain/card.dart';
import 'package:pmu/presentation/detail_pages/card_detail_page.dart';
import 'package:pmu/presentation/home_page/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(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: const Body(),
);
}
}
class Body extends StatelessWidget {
const Body({super.key});
void _navToDetails(BuildContext context, CardPostData data) {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => DetailPage(data)),
);
}
void _showSnackBar(BuildContext context, String title, bool isLiked) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'${isLiked ? 'Вы поставили лайк: ' : 'Вы убрали лайк: '} $title',
style: const TextStyle(
color: Colors.black45,
fontSize: 20,
fontWeight: FontWeight.bold
),
),
backgroundColor: isLiked ? Colors.green : Colors.red,
duration: const Duration(seconds: 2),
));
});
}
@override
Widget build(BuildContext context) {
final data = [
const CardPostData(
"Евгения",
"Люблю программирование и кататься на скейте",
"https://avatar.iran.liara.run/public/girl",
true),
const CardPostData("Алекс", "Junior Flutter Engineer",
"https://avatar.iran.liara.run/public/36", false),
const CardPostData(
"Станислав",
"Нет ничего приятнее прогулки на природе!",
"https://avatar.iran.liara.run/public/boy",
true),
const CardPostData("Вероника", "В свободное время хожу на уроки вокала",
"https://avatar.iran.liara.run/public/93", false),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((data) {
return CardPost.fromData(data,
onLike: (String title, bool isLiked) =>
_showSnackBar(context, title, isLiked),
onTap: () => _navToDetails(context, data));
}).toList(),
)));
}
}