-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathimage_preview.dart
224 lines (211 loc) · 7.08 KB
/
image_preview.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import 'dart:io';
import 'dart:math';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_picgo/model/uploaded.dart';
import 'package:flutter_picgo/utils/extended.dart';
import 'package:toast/toast.dart';
class ImagePreviewUtils {
/// 打开图片预览页面
static void open(BuildContext context, Uploaded content) {
var page = GalleryPhotoViewWrapper(
galleryItems: [content],
);
Navigator.push(
context,
Platform.isAndroid
? MaterialPageRoute(builder: (_) => page)
: CupertinoPageRoute(builder: (_) => page),
);
}
static void openMulti(BuildContext context, int index, List<Uploaded> items) {
var page = GalleryPhotoViewWrapper(
galleryItems: items,
initialIndex: index,
);
Navigator.push(
context,
Platform.isAndroid
? MaterialPageRoute(builder: (_) => page)
: CupertinoPageRoute(builder: (_) => page));
}
}
class GalleryPhotoViewWrapper extends StatefulWidget {
final int initialIndex;
final List<Uploaded> galleryItems;
GalleryPhotoViewWrapper({
this.initialIndex = 0,
@required this.galleryItems,
});
@override
State<StatefulWidget> createState() {
return _GalleryPhotoViewWrapperState();
}
}
class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
int currentIndex;
@override
void initState() {
currentIndex = widget.initialIndex;
super.initState();
}
void onPageChanged(int index) {
setState(() {
currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return ExtendedImageSlidePage(
slideAxis: SlideAxis.both,
slideType: SlideType.onlyImage,
slidePageBackgroundHandler: (offset, pageSize) =>
defaultSlidePageBackgroundHandler(
offset: offset,
pageSize: pageSize,
color: Colors.black,
pageGestureAxis: SlideAxis.both),
child: GestureDetector(
child: Stack(
// fit: StackFit.expand,
alignment: Alignment.bottomRight,
children: [
ExtendedImageGesturePageView.builder(
itemBuilder: (BuildContext context, int index) {
var item = widget.galleryItems[index];
Widget image = item.path.startsWith('http')
? ExtendedImage.network(
item.path,
fit: BoxFit.contain,
cache: true,
mode: ExtendedImageMode.gesture,
enableSlideOutPage: true,
loadStateChanged: (state) =>
defaultLoadStateChanged(state, iconSize: 50),
)
: ExtendedImage.file(
File(item.path),
fit: BoxFit.contain,
mode: ExtendedImageMode.gesture,
enableSlideOutPage: true,
loadStateChanged: (state) =>
defaultLoadStateChanged(state, iconSize: 50),
);
image = Container(
child: image,
);
if (index == currentIndex) {
return Hero(
tag: index,
child: image,
);
} else {
return image;
}
},
itemCount: widget.galleryItems.length,
controller: PageController(
initialPage: currentIndex,
),
onPageChanged: onPageChanged,
scrollDirection: Axis.horizontal,
),
SafeArea(
child: Container(
margin: EdgeInsets.only(right: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
color: Colors.grey),
padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
child: Text(
'${currentIndex + 1} / ${widget.galleryItems.length}',
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
fontSize: 10),
),
))
],
),
onLongPress: () {
_showBottomPane();
},
),
);
}
/// 底部弹窗
_showBottomPane() {
showModalBottomSheet(
context: context,
builder: (context) {
return SafeArea(
child: Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
title: Text(
'图床类型',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(widget.galleryItems[currentIndex].type),
),
ListTile(
title: Text(
'图片链接',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(widget.galleryItems[currentIndex].path),
onTap: () {
_handleCopy(
widget.galleryItems[currentIndex].path, context);
},
),
ListTile(
title: Text(
'图片信息',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(widget.galleryItems[currentIndex].info),
onTap: () {
_handleCopy(
widget.galleryItems[currentIndex].info, context);
},
),
ListTile(
title: Text(
'取消',
style: TextStyle(fontWeight: FontWeight.bold),
),
onTap: () {
Navigator.pop(context);
},
),
],
),
));
});
}
/// 复制链接
_handleCopy(String content, BuildContext context) {
Clipboard.setData(ClipboardData(text: content));
Toast.show('已复制到剪切板', context);
Navigator.pop(context);
}
Color defaultSlidePageBackgroundHandler(
{Offset offset, Size pageSize, Color color, SlideAxis pageGestureAxis}) {
double opacity = 0.0;
if (pageGestureAxis == SlideAxis.both) {
opacity = offset.distance /
(Offset(pageSize.width, pageSize.height).distance / 2.0);
} else if (pageGestureAxis == SlideAxis.horizontal) {
opacity = offset.dx.abs() / (pageSize.width / 2.0);
} else if (pageGestureAxis == SlideAxis.vertical) {
opacity = offset.dy.abs() / (pageSize.height / 2.0);
}
return color.withOpacity(min(1.0, max(1.0 - opacity, 0.0)));
}
}