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

108 lines
3.0 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;
2024-09-17 00:03:15 +04:00
final String image;
final String species;
2024-09-11 22:57:02 +04:00
final OnLikeFunction? onLike;
final VoidCallback? onTap;
const _Card(
{required this.name,
2024-09-17 00:03:15 +04:00
required this.species,
required this.image,
2024-09-11 22:57:02 +04:00
this.onLike,
this.onTap});
factory _Card.fromData(
2024-09-25 14:35:14 +04:00
CardData data, {OnLikeFunction? onLike, VoidCallback? onTap}) =>
2024-09-11 22:57:02 +04:00
_Card(
name: data.name,
2024-09-17 00:03:15 +04:00
image: data.image,
species: data.species,
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
2024-09-17 00:03:15 +04:00
? "Card added in favourite"
: "Card deleted from favourite");
2024-09-11 22:57:02 +04:00
}
@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-17 00:03:15 +04:00
child: Image.network(
widget.image,
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)),
2024-09-17 00:03:15 +04:00
Text("Species: ${widget.species}",
2024-09-15 21:41:47 +04:00
style:
const TextStyle(fontSize: 17, color: Colors.orange))
2024-09-11 22:57:02 +04:00
],
),
)
],
),
),
);
}
}