-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlistviewpagingandloading
77 lines (67 loc) · 2.16 KB
/
listviewpagingandloading
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
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int page = 1;
List<String> items = ['item1', 'item2', 'item3', 'item4'];
bool isLoading = false;
Future<void> _loadingData() async {
await Future.delayed(Duration(seconds: 2));
print('loading more....');
setState(() {
items.addAll(['item1']);
print('items: ' + items.toString());
isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Column(
children: <Widget>[
//create an indicator when data is being retrieved
Container(
height: isLoading ? 50.0:0,
color: Colors.transparent,
child: Center(
child: CircularProgressIndicator()),
),
//end =============
//Notification Listener to listen to Scroll
//Expanded coz the data will expand out listview
Expanded(
child: NotificationListener<ScrollNotification>(
//this part to get notificaion info
onNotification: (ScrollNotification scrollInfo) {
if(!isLoading && scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent) {
_loadingData();
setState(() {
isLoading = true;
});
}
return null;
},
//this part display data in Listview
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(items[index]),
);
},
),
)
),
],
)),
);
}
}