108 lines
3.0 KiB
Dart
108 lines
3.0 KiB
Dart
part of "home_page.dart";
|
|
|
|
typedef OnLikeFunction = void Function(String text);
|
|
|
|
class _Card extends StatefulWidget {
|
|
final String name;
|
|
final String image;
|
|
final String species;
|
|
|
|
final OnLikeFunction? onLike;
|
|
final VoidCallback? onTap;
|
|
|
|
const _Card(
|
|
{required this.name,
|
|
required this.species,
|
|
required this.image,
|
|
this.onLike,
|
|
this.onTap});
|
|
|
|
factory _Card.fromData(
|
|
CardData data, OnLikeFunction? onLike, VoidCallback? onTap) =>
|
|
_Card(
|
|
name: data.name,
|
|
image: data.image,
|
|
species: data.species,
|
|
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
|
|
? "Card added in favourite"
|
|
: "Card 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),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
const SizedBox(height: 20),
|
|
Center(
|
|
child: Image.network(
|
|
widget.image,
|
|
width: 250,
|
|
height: 250,
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 50),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(widget.name, style: const TextStyle(fontSize: 17)),
|
|
Text("Species: ${widget.species}",
|
|
style:
|
|
const TextStyle(fontSize: 17, color: Colors.orange))
|
|
],
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|