-
Notifications
You must be signed in to change notification settings - Fork 12
/
news_card.dart
128 lines (116 loc) · 3.85 KB
/
news_card.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Flutter imports:
import 'package:flutter/material.dart';
// Package imports:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:shimmer/shimmer.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:ets_api_clients/models.dart';
// Project imports:
import 'package:notredame/features/app/navigation/router_paths.dart';
import 'package:notredame/features/app/navigation/navigation_service.dart';
import 'package:notredame/utils/locator.dart';
import 'package:notredame/utils/app_theme.dart';
class NewsCard extends StatefulWidget {
final News news;
const NewsCard(this.news, {super.key});
@override
_NewsCardState createState() => _NewsCardState();
}
class _NewsCardState extends State<NewsCard> {
final NavigationService _navigationService = locator<NavigationService>();
@override
Widget build(BuildContext context) {
timeago.setLocaleMessages('fr', timeago.FrShortMessages());
return GestureDetector(
onTap: () => _navigationService.pushNamed(RouterPaths.newsDetails,
arguments: widget.news),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
key: UniqueKey(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Hero(
tag: 'news_image_id_${widget.news.id}',
child: _buildImage(widget.news.imageUrl)),
const SizedBox(height: 8),
_buildTitleAndTime(widget.news, context),
],
),
),
],
),
),
);
}
Widget _buildImage(String? imageUrl) {
var isLoaded = false;
if (imageUrl != null && imageUrl != "") {
return ClipRRect(
borderRadius: BorderRadius.circular(16.0),
child: Image.network(
imageUrl == ""
? "https://www.shutterstock.com/image-vector/no-photo-thumbnail-graphic-element-600nw-2311073121.jpg"
: imageUrl,
frameBuilder: (context, child, frame, wasSynchronouslyLoaded) {
isLoaded = frame != null;
return child;
},
loadingBuilder: (context, child, loadingProgress) {
if (isLoaded && loadingProgress == null) {
return child;
} else {
return _shimmerEffect();
}
},
),
);
}
return const SizedBox();
}
Widget _shimmerEffect() {
return Shimmer.fromColors(
baseColor: Theme.of(context).brightness == Brightness.light
? AppTheme.lightThemeBackground
: AppTheme.darkThemeBackground,
highlightColor: Theme.of(context).brightness == Brightness.light
? AppTheme.lightThemeAccent
: AppTheme.darkThemeAccent,
child: Container(
height: 200,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(16),
),
),
);
}
Widget _buildTitleAndTime(News news, BuildContext context) {
final TextStyle textStyle =
Theme.of(context).textTheme.titleMedium!.copyWith(fontSize: 16);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Flexible(
child: Text(
news.title,
style: textStyle,
),
),
const SizedBox(width: 10),
Text(
timeago.format(news.publicationDate,
locale: AppIntl.of(context)!.localeName),
style: textStyle,
),
],
);
}
}