|
@@ -0,0 +1,308 @@
|
|
1
|
+import 'dart:typed_data';
|
|
2
|
+
|
|
3
|
+import 'package:flutter/material.dart';
|
|
4
|
+import 'package:photo/src/engine/lru_cache.dart';
|
|
5
|
+import 'package:photo/src/entity/options.dart';
|
|
6
|
+import 'package:photo/src/provider/i18n_provider.dart';
|
|
7
|
+import 'package:photo/src/provider/selected_provider.dart';
|
|
8
|
+import 'package:photo_manager/photo_manager.dart';
|
|
9
|
+
|
|
10
|
+class PhotoMainPage extends StatefulWidget {
|
|
11
|
+ final Options options;
|
|
12
|
+ final I18nProvider provider;
|
|
13
|
+
|
|
14
|
+ const PhotoMainPage({Key key, this.options, this.provider}) : super(key: key);
|
|
15
|
+
|
|
16
|
+ @override
|
|
17
|
+ _PhotoMainPageState createState() => _PhotoMainPageState();
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+class _PhotoMainPageState extends State<PhotoMainPage> with SelectedProvider {
|
|
21
|
+ Options get options => widget.options;
|
|
22
|
+
|
|
23
|
+ I18nProvider get i18nProvider => widget.provider;
|
|
24
|
+
|
|
25
|
+ List<ImageEntity> selectedList = [];
|
|
26
|
+ List<ImageParentPath> pathList = [];
|
|
27
|
+
|
|
28
|
+ List<ImageEntity> list = [];
|
|
29
|
+
|
|
30
|
+ Color get themeColor => options.themeColor;
|
|
31
|
+
|
|
32
|
+ ImageParentPath _currentPath;
|
|
33
|
+
|
|
34
|
+ ImageParentPath get currentPath {
|
|
35
|
+ if (_currentPath == null) {
|
|
36
|
+ return null;
|
|
37
|
+ }
|
|
38
|
+ return _currentPath;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ set currentPath(ImageParentPath value) {
|
|
42
|
+ _currentPath = value;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ @override
|
|
46
|
+ void initState() {
|
|
47
|
+ super.initState();
|
|
48
|
+ _refreshList();
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ @override
|
|
52
|
+ Widget build(BuildContext context) {
|
|
53
|
+ var textStyle = TextStyle(
|
|
54
|
+ color: options.textColor,
|
|
55
|
+ fontSize: 14.0,
|
|
56
|
+ );
|
|
57
|
+ return DefaultTextStyle(
|
|
58
|
+ style: textStyle,
|
|
59
|
+ child: Scaffold(
|
|
60
|
+ appBar: AppBar(
|
|
61
|
+ backgroundColor: options.themeColor,
|
|
62
|
+ title: Text(
|
|
63
|
+ i18nProvider.getTitleText(options),
|
|
64
|
+ ),
|
|
65
|
+ actions: <Widget>[
|
|
66
|
+ FlatButton(
|
|
67
|
+ child: Text(
|
|
68
|
+ i18nProvider.getSureText(options, this),
|
|
69
|
+ style: textStyle,
|
|
70
|
+ ),
|
|
71
|
+ onPressed: sure,
|
|
72
|
+ ),
|
|
73
|
+ ],
|
|
74
|
+ ),
|
|
75
|
+ body: _buildBody(),
|
|
76
|
+ bottomSheet: _BottomWidget(
|
|
77
|
+ provider: i18nProvider,
|
|
78
|
+ options: options,
|
|
79
|
+ onGalleryChange: _onGalleryChange,
|
|
80
|
+ selectedProvider: this,
|
|
81
|
+ ),
|
|
82
|
+ ),
|
|
83
|
+ );
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ void _refreshList() async {
|
|
87
|
+ pathList = await ImageScanner.getImagePathList();
|
|
88
|
+ var path = pathList[0];
|
|
89
|
+ var imageList = await path.imageList;
|
|
90
|
+ this.list.clear();
|
|
91
|
+ this.list.addAll(imageList);
|
|
92
|
+ print(list);
|
|
93
|
+ setState(() {});
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ Widget _buildBody() {
|
|
97
|
+ return Container(
|
|
98
|
+ color: options.paddingColor,
|
|
99
|
+ child: GridView.builder(
|
|
100
|
+ gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
101
|
+ crossAxisCount: options.rowCount,
|
|
102
|
+ childAspectRatio: options.itemRadio,
|
|
103
|
+ crossAxisSpacing: options.padding,
|
|
104
|
+ mainAxisSpacing: options.padding,
|
|
105
|
+ ),
|
|
106
|
+ itemBuilder: _buildItem,
|
|
107
|
+ itemCount: list.length,
|
|
108
|
+ ),
|
|
109
|
+ );
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ @override
|
|
113
|
+ int get selectedCount => selectedList.length;
|
|
114
|
+
|
|
115
|
+ @override
|
|
116
|
+ bool containsEntity(ImageEntity entity) {
|
|
117
|
+ return selectedList.contains(entity);
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ @override
|
|
121
|
+ int indexOfSelected(ImageEntity entity) {
|
|
122
|
+ return selectedList.indexOf(entity);
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ Widget _buildItem(BuildContext context, int index) {
|
|
126
|
+ var data = list[index];
|
|
127
|
+ return GestureDetector(
|
|
128
|
+ onTap: () => _onItemClick(data),
|
|
129
|
+ child: Stack(
|
|
130
|
+ children: <Widget>[
|
|
131
|
+ ImageItem(
|
|
132
|
+ entity: data,
|
|
133
|
+ themeColor: themeColor,
|
|
134
|
+ ),
|
|
135
|
+ _buildMask(containsEntity(data)),
|
|
136
|
+ _buildSelected(data),
|
|
137
|
+ ],
|
|
138
|
+ ),
|
|
139
|
+ );
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ _buildMask(bool showMask) {
|
|
143
|
+ return IgnorePointer(
|
|
144
|
+ child: AnimatedContainer(
|
|
145
|
+ color: showMask ? Colors.black.withOpacity(0.5) : Colors.transparent,
|
|
146
|
+ duration: Duration(milliseconds: 300),
|
|
147
|
+ ),
|
|
148
|
+ );
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ Widget _buildSelected(ImageEntity entity) {
|
|
152
|
+ var currentSelected = containsEntity(entity);
|
|
153
|
+ return Positioned(
|
|
154
|
+ right: 0.0,
|
|
155
|
+ width: 36.0,
|
|
156
|
+ height: 36.0,
|
|
157
|
+ child: GestureDetector(
|
|
158
|
+ onTap: () {
|
|
159
|
+ changeCheck(!currentSelected, entity);
|
|
160
|
+ },
|
|
161
|
+ behavior: HitTestBehavior.translucent,
|
|
162
|
+ child: _buildText(entity),
|
|
163
|
+ ),
|
|
164
|
+ );
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ Widget _buildText(ImageEntity entity) {
|
|
168
|
+ var isSelected = containsEntity(entity);
|
|
169
|
+ Widget child;
|
|
170
|
+ BoxDecoration decoration;
|
|
171
|
+ if (isSelected) {
|
|
172
|
+ child = Text(
|
|
173
|
+ (indexOfSelected(entity) + 1).toString(),
|
|
174
|
+ textAlign: TextAlign.center,
|
|
175
|
+ style: TextStyle(
|
|
176
|
+ fontSize: 12.0,
|
|
177
|
+ color: options.textColor,
|
|
178
|
+ ),
|
|
179
|
+ );
|
|
180
|
+ decoration = BoxDecoration(color: themeColor);
|
|
181
|
+ } else {
|
|
182
|
+ decoration = BoxDecoration(
|
|
183
|
+ borderRadius: BorderRadius.circular(1.0),
|
|
184
|
+ border: Border.all(
|
|
185
|
+ color: themeColor,
|
|
186
|
+ ),
|
|
187
|
+ );
|
|
188
|
+ }
|
|
189
|
+ return Padding(
|
|
190
|
+ padding: const EdgeInsets.all(8.0),
|
|
191
|
+ child: AnimatedContainer(
|
|
192
|
+ duration: Duration(milliseconds: 300),
|
|
193
|
+ decoration: decoration,
|
|
194
|
+ alignment: Alignment.center,
|
|
195
|
+ child: child,
|
|
196
|
+ ),
|
|
197
|
+ );
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ void changeCheck(bool value, ImageEntity entity) {
|
|
201
|
+ if (value) {
|
|
202
|
+ selectedList.add(entity);
|
|
203
|
+ } else {
|
|
204
|
+ selectedList.remove(entity);
|
|
205
|
+ }
|
|
206
|
+ setState(() {});
|
|
207
|
+ }
|
|
208
|
+
|
|
209
|
+ void sure() {}
|
|
210
|
+
|
|
211
|
+ void _onItemClick(ImageEntity data) {}
|
|
212
|
+
|
|
213
|
+ void _onGalleryChange(ImageParentPath value) {}
|
|
214
|
+}
|
|
215
|
+
|
|
216
|
+class _BottomWidget extends StatefulWidget {
|
|
217
|
+ final ValueChanged<ImageParentPath> onGalleryChange;
|
|
218
|
+
|
|
219
|
+ final Options options;
|
|
220
|
+
|
|
221
|
+ final I18nProvider provider;
|
|
222
|
+
|
|
223
|
+ final SelectedProvider selectedProvider;
|
|
224
|
+
|
|
225
|
+ final String galleryName;
|
|
226
|
+
|
|
227
|
+ const _BottomWidget({
|
|
228
|
+ Key key,
|
|
229
|
+ this.onGalleryChange,
|
|
230
|
+ this.options,
|
|
231
|
+ this.provider,
|
|
232
|
+ this.selectedProvider,
|
|
233
|
+ this.galleryName = "",
|
|
234
|
+ }) : super(key: key);
|
|
235
|
+
|
|
236
|
+ @override
|
|
237
|
+ __BottomWidgetState createState() => __BottomWidgetState();
|
|
238
|
+}
|
|
239
|
+
|
|
240
|
+class __BottomWidgetState extends State<_BottomWidget> {
|
|
241
|
+ Options get options => widget.options;
|
|
242
|
+
|
|
243
|
+ I18nProvider get i18nProvider => widget.provider;
|
|
244
|
+
|
|
245
|
+ @override
|
|
246
|
+ Widget build(BuildContext context) {
|
|
247
|
+ return Container(
|
|
248
|
+ color: options.themeColor,
|
|
249
|
+ height: 44.0,
|
|
250
|
+ child: Row(
|
|
251
|
+ children: <Widget>[
|
|
252
|
+ Text(
|
|
253
|
+ widget.galleryName,
|
|
254
|
+ ),
|
|
255
|
+ ],
|
|
256
|
+ ),
|
|
257
|
+ );
|
|
258
|
+ }
|
|
259
|
+}
|
|
260
|
+
|
|
261
|
+class ImageItem extends StatelessWidget {
|
|
262
|
+ final ImageEntity entity;
|
|
263
|
+
|
|
264
|
+ final Color themeColor;
|
|
265
|
+
|
|
266
|
+ const ImageItem({
|
|
267
|
+ Key key,
|
|
268
|
+ this.entity,
|
|
269
|
+ this.themeColor,
|
|
270
|
+ }) : super(key: key);
|
|
271
|
+
|
|
272
|
+ @override
|
|
273
|
+ Widget build(BuildContext context) {
|
|
274
|
+ var thumb = ImageLruCache.getData(entity);
|
|
275
|
+ if (thumb != null) {
|
|
276
|
+ return _buildImageItem(thumb);
|
|
277
|
+ }
|
|
278
|
+ return FutureBuilder<Uint8List>(
|
|
279
|
+ future: entity.thumbData,
|
|
280
|
+ builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
|
|
281
|
+ var futureData = snapshot.data;
|
|
282
|
+ if (snapshot.connectionState == ConnectionState.done &&
|
|
283
|
+ futureData != null) {
|
|
284
|
+ ImageLruCache.setData(entity, futureData);
|
|
285
|
+ return _buildImageItem(futureData);
|
|
286
|
+ }
|
|
287
|
+ return Center(
|
|
288
|
+ child: Container(
|
|
289
|
+ width: 20.0,
|
|
290
|
+ height: 20.0,
|
|
291
|
+ child: CircularProgressIndicator(
|
|
292
|
+ valueColor: AlwaysStoppedAnimation(themeColor),
|
|
293
|
+ ),
|
|
294
|
+ ),
|
|
295
|
+ );
|
|
296
|
+ },
|
|
297
|
+ );
|
|
298
|
+ }
|
|
299
|
+
|
|
300
|
+ Widget _buildImageItem(Uint8List data) {
|
|
301
|
+ return Image.memory(
|
|
302
|
+ data,
|
|
303
|
+ width: double.infinity,
|
|
304
|
+ height: double.infinity,
|
|
305
|
+ fit: BoxFit.cover,
|
|
306
|
+ );
|
|
307
|
+ }
|
|
308
|
+}
|