-
Notifications
You must be signed in to change notification settings - Fork 13
/
document_content.dart
141 lines (130 loc) · 4.67 KB
/
document_content.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
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:voice/model/country.dart';
import 'package:voice/constants/keys.dart';
import 'package:voice/widget/error_widget.dart';
import 'package:voice/widget/loading_widget.dart';
import 'package:url_launcher/url_launcher.dart';
class DocumentContent extends StatefulWidget {
final String selectedCategory;
final String selectedCountry;
DocumentContent({this.selectedCategory, this.selectedCountry});
@override
State<StatefulWidget> createState() {
return new _DocumentState();
}
}
class _DocumentState extends State<DocumentContent> {
var stream;
@override
Widget build(BuildContext context) {
_initialiseContentStream();
return StreamBuilder(
stream: stream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Container(
child: LoadingWidget(
document: widget.selectedCategory,
));
} else if (snapshot.data.documents.length > 0) {
return new ListView.builder(
itemBuilder: (BuildContext context, int index) =>
_buildDocumentTile(
snapshot.data.documents[index],
index > 0 ? snapshot.data.documents[index - 1] : null,
index,
snapshot.data.documents.length),
itemCount: snapshot.data.documents.length,
);
} else {
return Container(
child: ContentErrorWidget(onTryAgain: _reloadContent));
}
});
}
_buildDocumentTile(DocumentSnapshot document,
DocumentSnapshot previousDocument, int index, int itemCount) {
if (index >= itemCount) return null;
bool showHeader = widget.selectedCountry != countries[3].label &&
(previousDocument == null ||
previousDocument.data[Keys.KEY_CITY] !=
document.data[Keys.KEY_CITY]);
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
showHeader
? Padding(
padding: const EdgeInsets.only(
top: 16.0,
left: 16.0,
bottom: 16.0,
),
child: Text(
capitalize(document[Keys.KEY_CITY]),
textAlign: TextAlign.left,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
))
: index != 0
? Padding(
padding: const EdgeInsets.only(
left: 74.0,
),
child: Divider(
color: Colors.grey[400],
),
)
: Container(),
ListTile(
leading: Container(
width: 42,
height: 42,
child: ClipOval(
child: Center(
child: FadeInImage.assetNetwork(
fit: BoxFit.cover,
placeholder: 'assets/preload.png',
image: document[Keys.KEY_IMAGE]),
),
)),
onTap: () => _launchURL(document[Keys.KEY_URL]),
title: Text(capitalize(document[Keys.KEY_NAME])),
subtitle: document[Keys.KEY_CITY] != null &&
document[Keys.KEY_COUNTRY] != null
? Text(capitalize(document[Keys.KEY_CITY]) +
", " +
capitalize(document[Keys.KEY_COUNTRY]))
: null,
),
],
),
);
}
_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
void _reloadContent() {
setState(() {
_initialiseContentStream();
});
}
_initialiseContentStream() {
CollectionReference collectionReference =
Firestore.instance.collection(widget.selectedCategory);
stream = widget.selectedCountry != countries[3].label
? collectionReference
.where(Keys.KEY_GROUP, isEqualTo: widget.selectedCountry)
.orderBy(Keys.KEY_CITY, descending: false)
.snapshots()
: collectionReference.snapshots(); // initial stream
}
String capitalize(String s) => s[0].toUpperCase() + s.substring(1);
}