-
Notifications
You must be signed in to change notification settings - Fork 188
/
future_builder_page.dart
257 lines (211 loc) · 6.24 KB
/
future_builder_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
class FutureBuilderPage extends StatefulWidget {
@override
_FutureBuilderPageState createState() => _FutureBuilderPageState();
}
class _FutureBuilderPageState extends State<FutureBuilderPage> {
Dio _dio;
int date = 20190523;
Future<List<Stories>> _future;
@override
void initState() {
super.initState();
_dio = Dio();
_future = getNewsList();
}
Future<List<Stories>> getNewsList() async {
var response =
await _dio.get('https://news-at.zhihu.com/api/4/news/before/$date');
return ZhiHuNews.fromJson(response.data)._stories;
}
Widget generateListView(List<Stories> data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
var indexData = data[index];
return Card(
margin: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 10.0, right: 10, left: 6),
child: Text(indexData.title, style: TextStyle(fontSize: 15),),
),
flex: 1,
),
Image.network(
indexData.images[0],
height: 100,
width: 100,
),
],
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('FutureBuilderPage'),
),
body: FutureBuilder<List<Stories>>(
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.active:
case ConnectionState.waiting:
print('waiting');
return Center(child: CupertinoActivityIndicator());
case ConnectionState.done:
print('done');
if (snapshot.hasError) {
return Center(
child: Text('网络请求出错'),
);
}
return generateListView(snapshot.data);
}
return null;
},
future: _future,
),
);
}
Future refresh() async {
setState(() {
_future = getNewsList();
});
}
}
class ZhiHuNews {
String _date;
List<Stories> _stories;
List<TopStories> _topStories;
ZhiHuNews({String date, List<Stories> stories, List<TopStories> topStories}) {
this._date = date;
this._stories = stories;
this._topStories = topStories;
}
String get date => _date;
set date(String date) => _date = date;
List<Stories> get stories => _stories;
set stories(List<Stories> stories) => _stories = stories;
List<TopStories> get topStories => _topStories;
set topStories(List<TopStories> topStories) => _topStories = topStories;
ZhiHuNews.fromJson(Map<String, dynamic> json) {
_date = json['date'];
if (json['stories'] != null) {
_stories = new List<Stories>();
json['stories'].forEach((v) {
_stories.add(new Stories.fromJson(v));
});
}
if (json['top_stories'] != null) {
_topStories = new List<TopStories>();
json['top_stories'].forEach((v) {
_topStories.add(new TopStories.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['date'] = this._date;
if (this._stories != null) {
data['stories'] = this._stories.map((v) => v.toJson()).toList();
}
if (this._topStories != null) {
data['top_stories'] = this._topStories.map((v) => v.toJson()).toList();
}
return data;
}
}
class Stories {
List<String> _images;
int _type;
int _id;
String _gaPrefix;
String _title;
Stories(
{List<String> images, int type, int id, String gaPrefix, String title}) {
this._images = images;
this._type = type;
this._id = id;
this._gaPrefix = gaPrefix;
this._title = title;
}
List<String> get images => _images;
set images(List<String> images) => _images = images;
int get type => _type;
set type(int type) => _type = type;
int get id => _id;
set id(int id) => _id = id;
String get gaPrefix => _gaPrefix;
set gaPrefix(String gaPrefix) => _gaPrefix = gaPrefix;
String get title => _title;
set title(String title) => _title = title;
Stories.fromJson(Map<String, dynamic> json) {
_images = json['images'].cast<String>();
_type = json['type'];
_id = json['id'];
_gaPrefix = json['ga_prefix'];
_title = json['title'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['images'] = this._images;
data['type'] = this._type;
data['id'] = this._id;
data['ga_prefix'] = this._gaPrefix;
data['title'] = this._title;
return data;
}
}
class TopStories {
String _image;
int _type;
int _id;
String _gaPrefix;
String _title;
TopStories({String image, int type, int id, String gaPrefix, String title}) {
this._image = image;
this._type = type;
this._id = id;
this._gaPrefix = gaPrefix;
this._title = title;
}
String get image => _image;
set image(String image) => _image = image;
int get type => _type;
set type(int type) => _type = type;
int get id => _id;
set id(int id) => _id = id;
String get gaPrefix => _gaPrefix;
set gaPrefix(String gaPrefix) => _gaPrefix = gaPrefix;
String get title => _title;
set title(String title) => _title = title;
TopStories.fromJson(Map<String, dynamic> json) {
_image = json['image'];
_type = json['type'];
_id = json['id'];
_gaPrefix = json['ga_prefix'];
_title = json['title'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['image'] = this._image;
data['type'] = this._type;
data['id'] = this._id;
data['ga_prefix'] = this._gaPrefix;
data['title'] = this._title;
return data;
}
}