@@ -12,23 +12,37 @@ class HNComment {
12
12
int id;
13
13
double left;
14
14
Future <Map <String , dynamic >> future;
15
- Widget card;
16
15
List <HNComment > children;
17
16
}
18
17
19
- class CommentPage extends StatelessWidget {
20
- final HNStory story;
21
- List <Widget > cards;
22
- StreamController <Widget > _controller = StreamController <Widget >.broadcast ();
18
+ class CommentPage extends StatefulWidget {
19
+ final HNStory _story;
23
20
24
- CommentPage (this .story) {
25
- cards = new List ();
21
+ CommentPage (this ._story);
22
+
23
+ CommentPageState createState () => CommentPageState (_story);
24
+ }
25
+
26
+ class CommentPageState extends State <CommentPage > {
27
+ final HNStory _story;
28
+ List <Widget > _cards;
29
+
30
+ CommentPageState (this ._story) {
31
+ _cards = new List (_story.comments);
26
32
getComments ();
27
33
}
28
34
29
- Widget makeCard (String text, String by, int time, double left) {
35
+ void initState () {
36
+ super .initState ();
37
+ var loading = _makeCard ("..." , "..." , 0 , 0 );
38
+ for (int i = 0 ; i < _story.comments; i++ ) {
39
+ _cards[i] = loading;
40
+ }
41
+ }
42
+
43
+ Widget _makeCard (String text, String by, int time, double left) {
30
44
var before = DateTime .now ()
31
- .difference (DateTime .fromMillisecondsSinceEpoch (story. time * 1000 ));
45
+ .difference (DateTime .fromMillisecondsSinceEpoch (time * 1000 ));
32
46
String timeString = "${before .inHours } hours" ;
33
47
if (before.inHours < 1 ) {
34
48
timeString = "${before .inMinutes } minutes" ;
@@ -46,16 +60,22 @@ class CommentPage extends StatelessWidget {
46
60
}
47
61
48
62
void getComments () async {
49
- for (var comment in story.children) {
63
+ int j = 0 ;
64
+ for (var comment in _story.children) {
50
65
comment.future = HNAPI .fetchItem (comment.id);
51
66
ListQueue <HNComment > stack = new ListQueue ();
52
67
stack.addLast (comment);
53
68
while (stack.isNotEmpty) {
54
69
HNComment top = stack.last;
55
70
stack.removeLast ();
56
71
var res = await top.future;
57
- _controller.add (makeCard (
58
- res["text" ] ?? "deleted" , res["by" ] ?? "?" , res['time' ], top.left));
72
+ setState (() {
73
+ if (j < _story.comments) {
74
+ _cards[j] = _makeCard (res["text" ] ?? "deleted" , res["by" ] ?? "?" ,
75
+ res['time' ], top.left);
76
+ j++ ;
77
+ }
78
+ });
59
79
if (res['kids' ] != null ) {
60
80
top.children = new List (res["kids" ].length);
61
81
for (int i = 0 ; i < res["kids" ].length; i++ ) {
@@ -74,7 +94,7 @@ class CommentPage extends StatelessWidget {
74
94
length: 2 ,
75
95
child: Scaffold (
76
96
appBar: AppBar (
77
- title: Text ("${story .headline }" ),
97
+ title: Text ("${_story .headline }" ),
78
98
backgroundColor: Color (0xffff6600 ),
79
99
bottom: TabBar (tabs: [
80
100
Tab (text: "Website" ),
@@ -83,30 +103,14 @@ class CommentPage extends StatelessWidget {
83
103
),
84
104
body: Center (
85
105
child: TabBarView (children: [
106
+ WebViewScreen (_story.url),
86
107
Container (
87
108
decoration: BoxDecoration (color: Colors .grey[200 ]),
88
- child: StreamBuilder (
89
- stream: _controller.stream,
90
- builder: (context, snapshot) {
91
- if (snapshot.hasData) {
92
- cards.add (snapshot.data);
93
- return ListView .builder (
94
- itemBuilder: (context, index) {
95
- return cards[index];
96
- },
97
- itemCount: cards.length);
98
- } else if (snapshot.connectionState ==
99
- ConnectionState .done ||
100
- snapshot.connectionState == ConnectionState .waiting) {
101
- return ListView .builder (
102
- itemBuilder: (context, index) {
103
- return cards[index];
104
- },
105
- itemCount: cards.length);
106
- }
107
- return Text ("..." );
108
- })),
109
- WebViewScreen (story.url),
109
+ child: ListView .builder (
110
+ itemBuilder: (context, index) {
111
+ return _cards[index];
112
+ },
113
+ itemCount: _cards.length)),
110
114
])),
111
115
));
112
116
}
0 commit comments