/
main.dart
206 lines (187 loc) · 6.76 KB
/
main.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
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
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:local_image_pager/local_image_pager.dart';
/// An example app for local_image_pager plugin.
void main() {
runApp(MaterialApp(
title: 'Local images pager example',
home: LocalImagePagerDemo(),
));
}
class LocalImagePagerDemo extends StatefulWidget {
@override
_LocalImagePagerDemoState createState() => _LocalImagePagerDemoState();
}
/// How many images you want to fetch per 1 page.
const per_load_count = 30;
/// GridView's crossAxisCount.
const cross_axis_count = 4;
/// GridView's mainAxisSpacing, crossAxisSpacing, and padding. (Used to calculate the thumbnail size.)
const double spacing = 5;
/// GridView's padding. (Used to calculate the thumbnail size.)
const double horizontal_padding = 10;
class _LocalImagePagerDemoState extends State<LocalImagePagerDemo> {
/// You need to ask [Permission.storage].
final _permissions = [Permission.storage];
final _completers = <Completer>[];
@override
void initState() {
super.initState();
}
Future<bool> get _initPermissions async {
final statuses = (await _permissions.request()).values;
for (final status in statuses) {
if (status != PermissionStatus.granted) {
print('_initPermissions:not granted');
return false;
}
}
print('_initPermissions:granted');
return true;
}
@override
Widget build(BuildContext context) {
// Calculate the thumbnail's size first to improve performance.
final itemSize = ((MediaQuery.of(context).size.width -
spacing * (cross_axis_count - 1) - horizontal_padding * 2) /
cross_axis_count);
final cacheSize = (itemSize * 1.5).round();
final pager = LocalImagePager();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Local images pager'),
),
body: Column(children: [
const SizedBox(height: 100,
child: Center(child: Text('Your own widget here.', style: TextStyle(fontSize: 20, color: Colors.blue, fontWeight: FontWeight.bold),),)),
Expanded(child: FutureBuilder(
future: _initPermissions,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data) {
return FutureBuilder(
future: LocalImagePager.totalNumber,
builder: (context, snapshot) {
if (snapshot.hasData) {
final count = snapshot.data;
if (count == 0) {
return const Center(
child: Text('No images'),
);
} else {
return GridView.builder(
padding: const EdgeInsets.symmetric(horizontal: horizontal_padding),
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: cross_axis_count,
mainAxisSpacing: spacing,
crossAxisSpacing: spacing,
),
itemCount: count,
itemBuilder: (context, index) =>
_itemBuilder(
pager, count, index, itemSize, cacheSize));
}
} else if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error.toString(),
style: const TextStyle(color: Colors.red),
),
);
} else {
return const Center(
child: CircularProgressIndicator());
}
},
);
} else {
return Column(
children: [
const Text(
'Permission not granted, try again.',
style: const TextStyle(color: Colors.red),
),
FlatButton(
onPressed: () {
setState(() {});
},
child: const Text('OK'))
],
);
}
} else if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error.toString(),
style: const TextStyle(color: Colors.red),
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),)
])),
);
}
Widget _itemBuilder(
LocalImagePager paginate, int totalNumber, int index, double itemSize, int cacheSize) {
_load(paginate, totalNumber, index);
return FutureBuilder(
future: _completers[index].future,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Image.file(
File(snapshot.data),
fit: BoxFit.cover,
height: itemSize,
cacheHeight: cacheSize,
);
} else if (snapshot.hasError) {
print('_itemBuilder:error:${snapshot.error.toString()}');
return const SizedBox(
width: 0,
height: 0,
);
} else {
return const SizedBox(
width: 0,
height: 0,
);
}
},
);
}
_load(LocalImagePager paginate, int count, int itemIndex) async {
if (itemIndex % per_load_count != 0) {
return;
}
if (itemIndex >= _completers.length) {
final toLoad = min(count - itemIndex, per_load_count);
if (toLoad > 0) {
_completers.addAll(List.generate(toLoad, (index) {
return Completer();
}));
try {
final images =
await paginate.latestImages(itemIndex, itemIndex + toLoad - 1);
images.asMap().forEach((index, item) {
_completers[itemIndex + index].complete(item);
});
} catch (e) {
print('_load:error:${e.toString()}');
_completers
.sublist(itemIndex, itemIndex + toLoad)
.forEach((completer) {
completer.completeError(e);
});
}
}
}
}
}