-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull_to_refresh.txt
More file actions
318 lines (274 loc) · 10.2 KB
/
pull_to_refresh.txt
File metadata and controls
318 lines (274 loc) · 10.2 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
Today we'll explore just how easy to implement pull to refresh on a flutter application using pull_to_refresh package and make use of list from the api.
*** image example of refresh
So let dive into our flutter project.
First we add the pull_to_refresh package to our project
Inside pubspec.yaml file Add
dependencies:
pull_to_refresh: ^1.5.8
For me 99% of package i use in my flutter app, i don't put version code in front, i prefer
dependencies:
pull_to_refresh:
There is different between this two example, which i will talk about later in my post Follow me to stay updated
So we have our main.dart file
import 'package:flutter/material.dart';
import 'refresh.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Refresh',
theme: ThemeData(
primarySwatch: Colors.teal,
primaryIconTheme: IconThemeData(color: Colors.white),
),
home: RefreshPage(),
);
}
}
So let move to the page we want to refresh
create another file let call it refresh.dart
Paste this code inside the file
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class RefreshPage extends StatefulWidget {
RefreshPage({Key key}) : super(key: key);
@override
_RefreshPageState createState() => _RefreshPageState();
}
class _RefreshPageState extends State<RefreshPage> {
bool _enablePullDown = true; // this enable our app to able to pull down
RefreshController _refreshController; // the refresh controller
var _scaffoldKey = GlobalKey<ScaffoldState>(); // this is our key to the scaffold widget
@override
void initState() {
_refreshController = RefreshController(); // we have to use initState because this part of the app have to restart
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey, // the key we create up there
appBar: AppBar(
title: Text('Pull to Refresh'),
centerTitle: true,
elevation: 0,
),
// So inside the body widget we will implement pull to refresh, So first we call
body: SmartRefresher(
enablePullDown: _enablePullDown, // the bool we create, so this gave access to be able to pull the app down
header: WaterDropHeader(
waterDropColor: Colors.teal,
// complete: If the refresh is completed show this else failed
complete: Text('Complete',
style: TextStyle(
color: Colors.teal,
fontSize: 18,
fontWeight: FontWeight.bold)), // you can customize this whatever you like
failed:
Text('Failed', style: TextStyle(color: Colors.red, fontSize: 18)),
),
controller: _refreshController,
onRefresh: _onRefresh, // we are going to inplement _onRefresh and _onLoading below our build method
onLoading: _onLoading,
child: ii(), // we are going to create a list of text in this dynamic ii()
),
);
}
txtlist() {
return ListView.builder(
itemCount: 20,
shrinkWrap: true,
itemBuilder: (context, index) {
return Card(
elevation: 10,
child: ListTile(
dense: true,
title: Text('Dummy Text'),
leading: Text('$index'),
subtitle: Text('push down'),
),
);
},
);
}
}
So below the txtlist paste this code
_onLoading() {
_refreshController.loadComplete(); // after data returned,set the footer state to idle
}
_onRefresh() {
setState(() {
RefreshPage(); // if you want to refresh the whole page you can put the page name or
txtlist(); // if you only want to refresh the list you can place this, so the two can be inside setState
_refreshController.refreshCompleted(); // request complete,the header will enter complete state,
// resetFooterState : it will set the footer state from noData to idle
});
}
I want to quickly show you some trick with this refresh package
If your app depends on internet you will need this trick else you can learn it for future purpose.
So inside the _onRefresh() function we want to add connectivity to it
Inside pubspec.yaml file Add
dependencies:
connectivity: ^0.4.8+2
fluttertoast:
Let create another file, name it customFunc.dart or whatever name you want
First we import our packages
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:connectivity/connectivity.dart';
create a class call it CustomFunction
class CustomFunction {}
let call the connectivityresult, inside the CustomFunction paste this code
Future<int> checkInternetConnection() async {
var result = await Connectivity().checkConnectivity();
if (result == ConnectivityResult.none) {
return 0;
} else if (result == ConnectivityResult.mobile) {
return 1;
} else if (result == ConnectivityResult.wifi) {
return 1;
} else {
return 0;
}
} // this will check though the phone internet access and see if the phone has access to internet or not
full customfunc code:
class CustomFunction{
showToast({String message}) {
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
backgroundColor: Colors.redAccent,
textColor: Colors.white,
fontSize: 20.0);
}
Future<int> checkInternetConnection() async {
var result = await Connectivity().checkConnectivity();
if (result == ConnectivityResult.none) {
return 0;
} else if (result == ConnectivityResult.mobile) {
return 1;
} else if (result == ConnectivityResult.wifi) {
return 1;
} else {
return 0;
}
}
}
we are done with this, let go back to the refresh file
the _onRefresh we have to change it to chack internet access
So delete the previous and paste this
_onRefresh() {
setState(() {
Future<int> a = CustomFunction().checkInternetConnection(); // check internet access
a.then((value) {
if (value == 0) {
CustomFunction().showToast(message: 'No Internet Connection'); // will show a toast if there is no internet
} else {
RefreshPage();// if you want to refresh the whole page you can put the page name or
txtlist();// if you only want to refresh the list you can place this, so the two can be inside setState
_refreshController.refreshCompleted();
// request complete,the header will enter complete state,
// resetFooterState : it will set the footer state from noData to idle
}
});
});
}
And we are good to go Restart your app or hot restart your app, you might need to do that several time to take effect on the app then pull the body down like you want to refresh
Complete Code:
import 'package:flutter/material.dart';
import 'customFunc.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class RefreshPage extends StatefulWidget {
RefreshPage({Key key}) : super(key: key);
@override
_RefreshPageState createState() => _RefreshPageState();
}
class _RefreshPageState extends State<RefreshPage> {
bool _enablePullDown = true; // this enable our app to able to pull down
RefreshController _refreshController; // the refresh controller
var _scaffoldKey =
GlobalKey<ScaffoldState>(); // this is our key to the scaffold widget
@override
void initState() {
_refreshController =
RefreshController(); // we have to use initState because this part of the app have to restart
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey, // the key we create up there
appBar: AppBar(
title: Text('Pull to Refresh'),
centerTitle: true,
elevation: 0,
),
// So inside the body widget we will implement pull to refresh, So first we call
body: SmartRefresher(
enablePullDown:
_enablePullDown, // the bool we create, so this gave access to be able to pull the app down
header: WaterDropHeader(
waterDropColor: Colors.teal,
// complete: If the refresh is completed show this else failed
complete: Text('Complete',
style: TextStyle(
color: Colors.teal,
fontSize: 18,
fontWeight: FontWeight
.bold)), // you can customize this whatever you like
failed:
Text('Failed', style: TextStyle(color: Colors.red, fontSize: 18)),
),
controller: _refreshController,
onRefresh:
_onRefresh, // we are going to inplement _onRefresh and _onLoading below our build method
onLoading: _onLoading,
child:
txtlist(), // we are going to create a list of text in this dynamic txtlist()
),
);
}
txtlist() {
return ListView.builder(
itemCount: 20,
shrinkWrap: true,
itemBuilder: (context, index) {
return Card(
elevation: 10,
child: ListTile(
dense: true,
title: Text('Dummy Text'),
leading: Text('$index'),
subtitle: Text('push down'),
),
);
},
);
}
_onLoading() {
_refreshController
.loadComplete(); // after data returned,set the footer state to idle
}
_onRefresh() {
setState(() {
Future<int> a =
CustomFunction().checkInternetConnection(); // check internet access
a.then((value) {
if (value == 0) {
CustomFunction().showToast(
message:
'No Internet Connection'); // will show a toast if there is no internet
} else {
RefreshPage(); // if you want to refresh the whole page you can put the page name or
txtlist(); // if you only want to refresh the list you can place this, so the two can be inside setState
_refreshController.refreshCompleted();
// request complete,the header will enter complete state,
// resetFooterState : it will set the footer state from noData to idle
}
});
});
}
}