-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnews_item_details_page.dart
168 lines (152 loc) · 5.54 KB
/
news_item_details_page.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:intl/intl.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:provider/provider.dart';
import '../../../resources/utils.dart';
import '../../../widgets/scaffold.dart';
import '../model/news_feed_item.dart';
import '../service/news_provider.dart';
import 'news_item_details_actions.dart';
class NewsItemDetailsPage extends StatefulWidget {
const NewsItemDetailsPage({@required this.newsItemGuid});
final String newsItemGuid;
@override
_NewsItemDetailsState createState() => _NewsItemDetailsState();
}
class _NewsItemDetailsState extends State<NewsItemDetailsPage> {
Future<dynamic> detailsFuture;
@override
void initState() {
super.initState();
detailsFuture = _getDetails();
}
Future<NewsFeedItem> _getDetails() async {
final NewsProvider newsProvider =
Provider.of<NewsProvider>(context, listen: false);
return newsProvider.fetchNewsItemDetails(widget.newsItemGuid);
}
String _formatDate(final String date) =>
DateFormat('yyyy-MM-dd').format(DateTime.parse(date));
@override
Widget build(final BuildContext context) {
return AppScaffold(
title: const Text('Detalii'),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(12, 12, 12, 0),
child: Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: FutureBuilder(
future: detailsFuture,
builder: (final context, final snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
final NewsFeedItem newsFeedItem = snapshot.data;
return _newsDetailsWrapper(
context: context, newsFeedItem: newsFeedItem);
}
return _detailsNotLoadedWidget();
}
return _detailsCircularProgressIndicator();
},
),
),
],
),
),
),
),
),
);
}
Widget _detailsCircularProgressIndicator() => const Padding(
padding: EdgeInsets.only(top: 12, bottom: 12),
child: Center(
child: CircularProgressIndicator(),
),
);
Widget _detailsNotLoadedWidget() => const Expanded(
child: Padding(
padding: EdgeInsets.only(top: 12, bottom: 12),
child: Center(
child: Text(
'More details could not be loaded',
textAlign: TextAlign.center,
),
),
),
);
Widget _newsDetailsWrapper(
{final BuildContext context, final NewsFeedItem newsFeedItem}) {
final captionStyle = Theme.of(context).textTheme.caption;
final captionSizeFactor =
captionStyle.fontSize / Theme.of(context).textTheme.bodyText1.fontSize;
final captionColor = captionStyle.color;
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_newsDetailsAuthor(author: newsFeedItem.source),
_newsDetailsContent(
content: newsFeedItem.body,
captionColor: captionColor,
captionSizeFactor: captionSizeFactor),
_newsDetailsTimestamp(createdAt: _formatDate(newsFeedItem.createdAt)),
_newsDetailsActions(),
],
);
}
Widget _newsDetailsAuthor({final String author}) => Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(author, style: const TextStyle(fontWeight: FontWeight.bold)),
const Padding(
padding: EdgeInsets.only(left: 4, right: 0, top: 0, bottom: 0),
child: Text('a postat:'),
),
],
);
Widget _newsDetailsContent(
{final String content,
final Color captionColor,
final double captionSizeFactor}) =>
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 12, bottom: 12),
child: MarkdownBody(
fitContent: false,
onTapLink: (final text, final link, final title) =>
Utils.launchURL(link),
/*
This is a workaround because the strings in Firebase represent
newlines as '\n' and Firebase replaces them with '\\n'. We need
to replace them back for them to display properly.
(See GitHub issue firebase/firebase-js-sdk#2366)
*/
data: content.replaceAll('\\n', '\n'),
extensionSet: md.ExtensionSet(
md.ExtensionSet.gitHubFlavored.blockSyntaxes, [
md.EmojiSyntax(),
...md.ExtensionSet.gitHubFlavored.inlineSyntaxes
]),
),
))
],
);
Widget _newsDetailsTimestamp({final String createdAt}) => Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text('Posted on: $createdAt', style: const TextStyle(fontSize: 12)),
],
);
Widget _newsDetailsActions() =>
NewsItemDetailsAction(newsItemGuid: widget.newsItemGuid);
}