lab4 #3
@ -8,7 +8,6 @@ typedef OnLikeCallback = void Function(String title, bool isLiked)?;
|
|||||||
const double NORMAL_ICON_SCALE = 2.0;
|
const double NORMAL_ICON_SCALE = 2.0;
|
||||||
const double SCALED_ICON_SCALE = 2.5;
|
const double SCALED_ICON_SCALE = 2.5;
|
||||||
|
|
||||||
|
|
||||||
class CardPost extends StatefulWidget {
|
class CardPost extends StatefulWidget {
|
||||||
final String description;
|
final String description;
|
||||||
final String? imageUrl;
|
final String? imageUrl;
|
||||||
@ -21,10 +20,10 @@ class CardPost extends StatefulWidget {
|
|||||||
const CardPost(this.name, this.description, this.imageUrl, this.isLiked,
|
const CardPost(this.name, this.description, this.imageUrl, this.isLiked,
|
||||||
this.onLike, this.onTap);
|
this.onLike, this.onTap);
|
||||||
|
|
||||||
factory CardPost.fromData(CardPostData data, {OnLikeCallback onLike,
|
factory CardPost.fromData(CardPostData data,
|
||||||
VoidCallback? onTap}) =>
|
{OnLikeCallback onLike, VoidCallback? onTap}) =>
|
||||||
CardPost(data.name, data.description, data.imageUrl, data.isLiked,
|
CardPost(data.name, data.description, data.imageUrl, data.isLiked, onLike,
|
||||||
onLike, onTap);
|
onTap);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<CardPost> createState() => _CardPostState();
|
State<CardPost> createState() => _CardPostState();
|
||||||
@ -35,20 +34,6 @@ class _CardPostState extends State<CardPost> {
|
|||||||
|
|
||||||
double iconScale = NORMAL_ICON_SCALE;
|
double iconScale = NORMAL_ICON_SCALE;
|
||||||
|
|
||||||
void _onLikeTap() {
|
|
||||||
setState(() {
|
|
||||||
isLiked = !isLiked;
|
|
||||||
iconScale = SCALED_ICON_SCALE; // Increase scale temporarily
|
|
||||||
});
|
|
||||||
|
|
||||||
// Reset the scale back to normal after 250 milliseconds
|
|
||||||
Timer(const Duration(milliseconds: 110), () {
|
|
||||||
setState(() {
|
|
||||||
iconScale = NORMAL_ICON_SCALE;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
@ -123,8 +108,7 @@ class _CardPostState extends State<CardPost> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
GestureDetector(
|
GestureDetector(
|
||||||
onTap: () =>
|
onTap: () => {
|
||||||
{
|
|
||||||
setState(() {
|
setState(() {
|
||||||
isLiked = !isLiked;
|
isLiked = !isLiked;
|
||||||
iconScale = SCALED_ICON_SCALE;
|
iconScale = SCALED_ICON_SCALE;
|
||||||
@ -137,30 +121,25 @@ class _CardPostState extends State<CardPost> {
|
|||||||
widget.onLike?.call(widget.name, isLiked);
|
widget.onLike?.call(widget.name, isLiked);
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
child: AnimatedScale(scale: iconScale,
|
child: AnimatedScale(
|
||||||
|
scale: iconScale,
|
||||||
duration: const Duration(milliseconds: 250),
|
duration: const Duration(milliseconds: 250),
|
||||||
child: AnimatedSwitcher(
|
child: AnimatedSwitcher(
|
||||||
duration: const Duration(milliseconds: 250),
|
duration: const Duration(milliseconds: 250),
|
||||||
child: isLiked
|
child: isLiked
|
||||||
? const Icon(
|
? const Icon(
|
||||||
Icons.favorite, color: Colors.red,
|
Icons.favorite,
|
||||||
key: ValueKey(
|
color: Colors.red,
|
||||||
1),)
|
key: ValueKey(1),
|
||||||
: const Icon(
|
)
|
||||||
Icons.favorite_border, color: Colors.black,
|
: const Icon(Icons.favorite_border,
|
||||||
key: ValueKey(1)
|
color: Colors.black, key: ValueKey(1)))),
|
||||||
)
|
|
||||||
)
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
)
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user