126 lines
3.9 KiB
Dart
126 lines
3.9 KiB
Dart
part of 'home_page.dart';
|
|
|
|
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
|
|
|
|
class _Card extends StatefulWidget {
|
|
final String _text;
|
|
final String? description;
|
|
List<String>? descriptionByLines;
|
|
List<Widget>? descriptionWidgets;
|
|
final String? imageUrl;
|
|
final OnLikeCallback onLike;
|
|
final VoidCallback? onTap;
|
|
|
|
_Card(this._text, {Key? key, this.imageUrl, this.description, this.onLike, this.onTap})
|
|
: super(key: key){
|
|
descriptionByLines = (description ?? '').split('\n');
|
|
}
|
|
|
|
factory _Card.fromData(
|
|
CardData data, {
|
|
OnLikeCallback onLike,
|
|
VoidCallback? onTap,
|
|
}) =>
|
|
_Card(
|
|
data.text,
|
|
imageUrl: data.imageUrl,
|
|
description: data.description,
|
|
onLike: onLike,
|
|
onTap: onTap,
|
|
);
|
|
|
|
@override
|
|
State<_Card> createState() => _CardState();
|
|
}
|
|
|
|
class _CardState extends State<_Card> {
|
|
bool isLiked = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: widget.onTap,
|
|
child: Container(
|
|
margin: const EdgeInsets.all(10),
|
|
constraints: const BoxConstraints(minHeight: 140, maxHeight: 255),
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(20),
|
|
color: Colors.amber,
|
|
border: Border.all(color: Colors.amberAccent, width: 2),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.grey.withOpacity(.5),
|
|
blurRadius: 8,
|
|
offset: const Offset(0, 5),
|
|
spreadRadius: 4,
|
|
)
|
|
]),
|
|
child: IntrinsicHeight(
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
ClipRRect(
|
|
borderRadius:
|
|
const BorderRadius.horizontal(left: Radius.circular(20)),
|
|
child: SizedBox(
|
|
height: double.infinity,
|
|
width: 120,
|
|
child: Image.network(
|
|
widget.imageUrl ?? '',
|
|
fit: BoxFit.cover,
|
|
errorBuilder: (_, __, ___) => const Placeholder(),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(left: 8.0, bottom: 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
widget._text,
|
|
style: Theme.of(context).textTheme.headlineMedium,
|
|
overflow: TextOverflow.ellipsis,
|
|
maxLines: 2,
|
|
),
|
|
...(widget.descriptionByLines ?? ['']).map((e) => Text(
|
|
e,
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
isLiked = !isLiked;
|
|
setState(() {});
|
|
widget.onLike?.call(widget._text, isLiked);
|
|
},
|
|
child: AnimatedSwitcher(
|
|
duration: const Duration(milliseconds: 100),
|
|
child: isLiked
|
|
? const Icon(
|
|
Icons.favorite,
|
|
key: ValueKey(0),
|
|
)
|
|
: const Icon(
|
|
Icons.favorite_border,
|
|
key: ValueKey(1),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|