4 почти

This commit is contained in:
GokaPek 2024-09-24 13:29:59 +04:00
parent 8a2e9bfb5c
commit b5d9663950
4 changed files with 141 additions and 108 deletions

View File

@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
class DetailPage extends StatelessWidget {
final String info;
const DetailPage({super.key, required this.info});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Информация о продукции'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
info,
style: const TextStyle(fontSize: 18.0),
),
),
);
}
}

View File

@ -1,95 +0,0 @@
part of 'home_page.dart';
class _CardData {
final String text;
final String info;
final String? urlImage;
_CardData({required this.text, required this.info, required this.urlImage});
}
class _Card extends StatefulWidget {
final String text;
final String info;
final String? urlImage;
final VoidCallback? onLike;
const _Card({super.key, required this.text, required this.info, required this.urlImage, this.onLike});
factory _Card.fromData(_CardData data, {VoidCallback? onLike}) => _Card(
text: data.text,
info: data.info,
urlImage: data.urlImage,
onLike: onLike,
);
@override
State<_Card> createState() => _CardState();
}
class _CardState extends State<_Card> {
bool isLiked = false;
@override
Widget build(BuildContext context) {
return Card(
elevation: 4.0,
margin: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Stack(
children: [
Image.network(
widget.urlImage ?? 'https://hlebzavod3.ru/images/virtuemart/product/011_IMG_9657.jpg',
fit: BoxFit.cover,
height: 150.0,
width: double.infinity,
),
Positioned(
top: 8.0,
right: 8.0,
child: GestureDetector(
onTap: () {
setState(() {
isLiked = !isLiked;
});
if(isLiked) widget.onLike?.call();
},
child: Icon(
Icons.favorite,
color: isLiked ? Colors.red : Colors.blueGrey,
size: 30.0,
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.text,
style: const TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 5.0),
Text(
widget.info,
style: const TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
],
),
),
],
),
);
}
}

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
part 'card.dart';
import '../../Presentation/detailPage.dart';
part '../../domain/models/card.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
@ -36,19 +36,19 @@ class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
final data = [
_CardData(text: "Батон",
CardData(text: "Батон",
info: "Белый хлеб, испечённый в виде продолговатой булки",
urlImage: "https://hlebzavod3.ru/images/virtuemart/product/011_IMG_9657.jpg"),
_CardData(text: "Багет",
CardData(text: "Багет",
info: "Длинный тонкий вид хлеба французского происхождения",
urlImage: "https://main-cdn.sbermegamarket.ru/big1/hlr-system/13/14/29/23/31/01/100027310078b0.jpg"),
_CardData(text: "Круассан",
CardData(text: "Круассан",
info: "Небольшое мучное кондитерское изделие, булочка в форме полумесяца из слоёного теста",
urlImage: "https://shokoladd.ru/wp-content/uploads/2019/02/Croissant_Chocolate_Plate_Two_547531_2048x1152.jpg"),
_CardData(text: "Чиаба́тта",
CardData(text: "Чиаба́тта",
info: "Итальянский пшеничный хлеб, выпекаемый с использованием закваски, дрожжей и оливкового масла",
urlImage: "https://avatars.mds.yandex.net/i?id=a9163f90a989a596b26b30ad39beaf47_l-10629685-images-thumbs&n=13"),
_CardData(text: "Булочка без картинки",
CardData(text: "Булочка без картинки",
info: "Очень кусьна",
urlImage: null)
];
@ -56,8 +56,8 @@ class Body extends StatelessWidget {
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((e) => _Card.fromData(e,
onLike: () => _showSnackBar(context),)).toList(),
children: data.map((e) => MyCard.fromData(e,
onLike: () => _showSnackBar(context),)).toList(),
)
)
);
@ -66,10 +66,10 @@ class Body extends StatelessWidget {
void _showSnackBar(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((_){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Bread liked!',
style: Theme.of(context).textTheme.bodyLarge,
),
content: Text(
'Bread liked!',
style: Theme.of(context).textTheme.bodyLarge,
),
backgroundColor: Colors.red,
duration: const Duration(seconds: 1),
));

105
lib/domain/models/card.dart Normal file
View File

@ -0,0 +1,105 @@
part of '../../Presentation/home_page/home_page.dart';
class CardData {
final String text;
final String info;
final String? urlImage;
CardData({required this.text, required this.info, required this.urlImage});
}
class MyCard extends StatefulWidget {
final String text;
final String info;
final String? urlImage;
final VoidCallback? onLike;
const MyCard({super.key, required this.text, required this.info, required this.urlImage, this.onLike});
factory MyCard.fromData(CardData data, {VoidCallback? onLike}) => MyCard(
text: data.text,
info: data.info,
urlImage: data.urlImage,
onLike: onLike,
);
@override
State<MyCard> createState() => _MyCardState();
}
class _MyCardState extends State<MyCard> {
bool isLiked = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(info: widget.info),
),
);
},
child: Card(
elevation: 4.0,
margin: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Stack(
children: [
Image.network(
widget.urlImage ?? 'https://hlebzavod3.ru/images/virtuemart/product/011_IMG_9657.jpg',
fit: BoxFit.cover,
height: 150.0,
width: double.infinity,
),
Positioned(
top: 8.0,
right: 8.0,
child: GestureDetector(
onTap: () {
setState(() {
isLiked = !isLiked;
});
if(isLiked) widget.onLike?.call();
},
child: Icon(
Icons.favorite,
color: isLiked ? Colors.red : Colors.blueGrey,
size: 30.0,
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.text,
style: const TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 5.0),
Text(
widget.info,
style: const TextStyle(
fontSize: 14.0,
color: Colors.grey,
),
),
],
),
),
],
),
),
);
}
}