PIbd-33_Dyakonov_R_R_PMD/lib/presentation/home_page/card.dart

104 lines
2.9 KiB
Dart
Raw Normal View History

2024-09-11 22:57:02 +04:00
part of "home_page.dart";
typedef OnLikeFunction = void Function(String text);
class _Card extends StatefulWidget {
final String name;
final double price;
2024-09-15 21:41:47 +04:00
final String imageUrl;
2024-09-11 22:57:02 +04:00
final OnLikeFunction? onLike;
final VoidCallback? onTap;
const _Card(
{required this.name,
required this.price,
2024-09-15 21:41:47 +04:00
required this.imageUrl,
2024-09-11 22:57:02 +04:00
this.onLike,
this.onTap});
factory _Card.fromData(
CardData data, OnLikeFunction? onLike, VoidCallback? onTap) =>
_Card(
name: data.name,
price: data.price,
2024-09-15 21:41:47 +04:00
imageUrl: data.imageUrl,
2024-09-11 22:57:02 +04:00
onTap: onTap,
onLike: onLike,
);
@override
State<StatefulWidget> createState() => _CardState();
}
class _CardState extends State<_Card> {
bool _isFavourite = false;
void toggleIsFavourite() {
setState(() {
_isFavourite = !_isFavourite;
});
widget.onLike?.call(_isFavourite
? "Product added in favourite"
: "Product deleted from favourite");
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
child: Container(
width: double.infinity,
padding:
const EdgeInsets.only(top: 15, bottom: 15, left: 30, right: 30),
decoration: BoxDecoration(
border: Border.all(color: Colors.black12, width: 2),
borderRadius: BorderRadius.circular(20)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: toggleIsFavourite,
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: _isFavourite
? const Icon(
Icons.favorite,
color: Colors.red,
key: ValueKey<int>(0),
)
: const Icon(
Icons.favorite_border,
color: Colors.red,
key: ValueKey<int>(1),
),
),
)
],
),
2024-09-15 21:41:47 +04:00
const SizedBox(height: 20),
2024-09-11 22:57:02 +04:00
Center(
2024-09-15 21:41:47 +04:00
child: Image.network(widget.imageUrl, width: 250, height: 250,),
2024-09-11 22:57:02 +04:00
),
Padding(
padding: const EdgeInsets.only(top: 50),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.name, style: const TextStyle(fontSize: 17)),
Text("${widget.price} Руб",
2024-09-15 21:41:47 +04:00
style:
const TextStyle(fontSize: 17, color: Colors.orange))
2024-09-11 22:57:02 +04:00
],
),
)
],
),
),
);
}
}