-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_refresh_page.dart
77 lines (69 loc) · 1.96 KB
/
simple_refresh_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
/*
Author: Jayce
createTime:2020-10
*/
import 'package:example/model/entity/book.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:load_data/load_data.dart';
class SimpleRefreshPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BookList'),
),
body: LoadDataWidget<List<Book>>(
configCreate: (context, oldConfig) {
return LoadConfig(
dataSource: BookListDataSource(),
dataManager: ListDataManager(),
dataDelegate: BookListDelegate(),
refreshAdapter: PullToRefreshAdapter(),
);
},
),
);
}
}
///获取列表数据
class BookListDataSource implements DataSource<List<Book>> {
int page = 0;
@override
Future<List<Book>> refresh(CancelHandle cancelHandle, [progressCallback]) {
return _load(0, cancelHandle, progressCallback);
}
@override
Future<List<Book>> loadMore(CancelHandle cancelHandle, [progressCallback]) {
return _load(page + 1, cancelHandle, progressCallback);
}
@override
bool hasMore() {
return page < 5;
}
Future<List<Book>> _load(int page, CancelHandle cancelHandle,
[progressCallback]) async {
await Future.delayed(Duration(seconds: 2));
List<Book> list = List.generate(10,
(index) => Book('book$page-$index', '${DateTime.now().toString()}'));
this.page = page;
return list;
}
}
///显示列表数据
class BookListDelegate extends DataDelegate<List<Book>> {
@override
Widget build(BuildContext context, List<Book> list) {
return ListView.separated(
itemBuilder: (context, index) {
Book book = list[index];
return ListTile(
title: Text('${book.name}'),
subtitle: Text('${book.content}'),
);
},
separatorBuilder: (context, index) => Divider(),
itemCount: list.length,
);
}
}