1
1
import 'package:blog_flutter/config.dart' ;
2
- import 'package:blog_flutter/http .dart' ;
2
+ import 'package:blog_flutter/services/article_service .dart' ;
3
3
import 'package:flutter/material.dart' ;
4
4
5
5
class HomeWidget extends StatefulWidget {
@@ -8,8 +8,8 @@ class HomeWidget extends StatefulWidget {
8
8
}
9
9
10
10
class HomeWidgetState extends State <HomeWidget > {
11
- var _articles = < dynamic > [];
12
- var _start = 0 , _limit = 10 , _total = 0 ;
11
+ List _articles = < dynamic > [];
12
+ int _start = 0 , _limit = 10 , _total = 0 ;
13
13
14
14
@override
15
15
void initState () {
@@ -18,14 +18,29 @@ class HomeWidgetState extends State<HomeWidget> {
18
18
}
19
19
20
20
void _getArticles () async {
21
- var response = await http
22
- . get ( '/articles' , queryParameters : { 'start' : _start, 'limit' : _limit} );
21
+ var response = await ArticleService . getArticles (_start, _limit);
22
+ print (response );
23
23
setState (() {
24
24
_articles.addAll (response.data['data' ]);
25
25
_total = response.data['total' ];
26
26
});
27
27
}
28
28
29
+ void _handleAdd () async {
30
+ await Navigator .of (context).pushNamed ('/add' );
31
+ _handlePullDownRefresh ();
32
+ }
33
+
34
+ Future _handlePullDownRefresh () {
35
+ return Future .delayed (Duration (microseconds: 500 ), () {
36
+ setState (() {
37
+ _start = 0 ;
38
+ _articles = [];
39
+ _getArticles ();
40
+ });
41
+ });
42
+ }
43
+
29
44
@override
30
45
Widget build (BuildContext context) {
31
46
return Scaffold (
@@ -36,62 +51,64 @@ class HomeWidgetState extends State<HomeWidget> {
36
51
style: TextStyle (color: Colors .white),
37
52
)),
38
53
),
39
- body: ListView .separated (
40
- itemCount: _articles.length,
41
- itemBuilder: (context, index) {
42
- final backgroundColor = index % 2 == 0 ? lightGreen : lightGrey;
54
+ body: RefreshIndicator (
55
+ onRefresh: _handlePullDownRefresh,
56
+ child: ListView .separated (
57
+ itemCount: _articles.length,
58
+ itemBuilder: (context, index) {
59
+ final backgroundColor = index % 2 == 0 ? lightGreen : lightGrey;
43
60
44
- if (_articles.length == 0 || index == _articles.length - 1 ) {
45
- if (_articles.length < _total - 1 ) {
46
- _start += _limit;
47
- _getArticles ();
48
- return Container (
49
- padding: EdgeInsets .all (16 ),
50
- alignment: Alignment .center,
51
- decoration: BoxDecoration (color: backgroundColor),
52
- child: SizedBox (
53
- width: 24 ,
54
- height: 24 ,
55
- child: CircularProgressIndicator (
56
- strokeWidth: 2 ,
57
- )),
58
- );
59
- } else {
60
- return Container (
61
- padding: EdgeInsets .all (16 ),
62
- alignment: Alignment .center,
63
- decoration: BoxDecoration (color: backgroundColor),
64
- child: Text ('没有更多了' , style: TextStyle (color: Colors .black87)),
65
- );
61
+ if (_articles.length == 0 || index == _articles.length - 1 ) {
62
+ if (_articles.length < _total - 1 ) {
63
+ _start += _limit;
64
+ _getArticles ();
65
+ return Container (
66
+ padding: EdgeInsets .all (16 ),
67
+ alignment: Alignment .center,
68
+ decoration: BoxDecoration (color: backgroundColor),
69
+ child: SizedBox (
70
+ width: 24 ,
71
+ height: 24 ,
72
+ child: CircularProgressIndicator (
73
+ strokeWidth: 2 ,
74
+ )),
75
+ );
76
+ } else {
77
+ return Container (
78
+ padding: EdgeInsets .all (16 ),
79
+ alignment: Alignment .center,
80
+ decoration: BoxDecoration (color: backgroundColor),
81
+ child: Text ('没有更多了' , style: TextStyle (color: Colors .black87)),
82
+ );
83
+ }
66
84
}
67
- }
68
85
69
- return Container (
70
- decoration: BoxDecoration (color: backgroundColor),
71
- child: ListTile (
72
- title: Text (_articles[index]['title' ],
86
+ return Container (
87
+ decoration: BoxDecoration (color: backgroundColor),
88
+ child: ListTile (
89
+ title: Text (_articles[index]['title' ],
90
+ overflow: TextOverflow .ellipsis,
91
+ style: TextStyle (
92
+ fontSize: 22 , height: 1.5 , color: Colors .green)),
93
+ subtitle: Text (
94
+ _articles[index]['content' ],
73
95
overflow: TextOverflow .ellipsis,
74
- style: TextStyle (
75
- fontSize: 22 , height: 1.5 , color: Colors .green)),
76
- subtitle: Text (
77
- _articles[index]['content' ],
78
- overflow: TextOverflow .ellipsis,
79
- maxLines: 2 ,
80
- style: TextStyle (fontSize: 16 , height: 1.5 ),
96
+ maxLines: 2 ,
97
+ style: TextStyle (fontSize: 16 , height: 1.5 ),
98
+ ),
99
+ onTap: () => Navigator .of (context)
100
+ .pushNamed ('/article' , arguments: _articles[index]),
81
101
),
82
- onTap: () => Navigator .of (context)
83
- .pushNamed ('/article' , arguments: _articles[index]),
84
- ),
85
- );
86
- },
87
- separatorBuilder: (context, index) => Divider (
88
- height: 0 ,
102
+ );
103
+ },
104
+ separatorBuilder: (context, index) => Divider (
105
+ height: 0 ,
106
+ ),
89
107
),
90
108
),
91
109
floatingActionButton: FloatingActionButton (
92
110
child: Icon (Icons .add),
93
- onPressed: () =>
94
- Navigator .of (context).pushNamed ('/form' , arguments: '新增文章' ),
111
+ onPressed: _handleAdd,
95
112
),
96
113
);
97
114
}
0 commit comments