PIbd31_Razubaev.S.M._PMU/lib/presentation/home_page/card.dart
2024-10-04 14:11:06 +04:00

104 lines
3.3 KiB
Dart

part of 'home_page.dart';
class _CardState extends State<_Card> {
bool isLiked = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
child: Container(
margin: const EdgeInsets.only(top: 16),
constraints: const BoxConstraints(minHeight: 140),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.amber,
borderRadius: BorderRadius.circular(20),
border: Border.all(
color: Colors.grey,
width: 2,
),
),
child: IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: SizedBox(
width: 140,
height: 100,
child: Image.network(
widget.imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
),
),
),
Flexible(
child: Padding(
padding: const EdgeInsets.only(left: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.text,
style: Theme.of(context).textTheme.headlineLarge),
Text(widget.descriptionText,
style: Theme.of(context).textTheme.bodyLarge),
],
),
)),
Padding(
padding:
const EdgeInsets.only(left: 8.0, right: 16, bottom: 16),
child: GestureDetector(
onTap: () {
setState(() {
isLiked = !isLiked;
});
widget.onLike?.call(widget.text, isLiked);
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: isLiked
? const Icon(
Icons.favorite,
color: Colors.redAccent,
key: ValueKey<int>(0),
)
: const Icon(Icons.favorite_border),
key: ValueKey<int>(1),
),
))
],
),
),
),
);
}
}
typedef onLikeCallback = void Function(String title, bool isliked)?;
class _Card extends StatefulWidget {
final String text;
final String descriptionText;
final String? imageUrl;
final onLikeCallback onLike;
final VoidCallback? onTap;
const _Card(this.text,
{required this.descriptionText, this.imageUrl, this.onLike, this.onTap});
factory _Card.fromData(CardData data, {onLikeCallback onLike, VoidCallback? onTap}) =>
_Card(data.text,
descriptionText: data.descriptionText,
imageUrl: data.imageUrl,
onLike: onLike,
onTap: onTap);
@override
State<_Card> createState() => _CardState();
}