Skip to content

Commit a2f988a

Browse files
committed
fix: path
Signed-off-by: bggRGjQaUbCoE <[email protected]>
1 parent b2b53a1 commit a2f988a

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

lib/image_page.dart

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import 'dart:io';
22

3-
import 'package:flutter/material.dart';
3+
import 'package:flutter/material.dart' hide Path;
44
import 'package:image_cropper/image_cropper.dart';
55
import 'package:pic_share_helper/icon_button.dart';
66
import 'package:mime/mime.dart';
7+
import 'package:pic_share_helper/path.dart';
78

89
class ImagePage extends StatefulWidget {
910
const ImagePage({
@@ -16,10 +17,10 @@ class ImagePage extends StatefulWidget {
1617
required this.onSave,
1718
});
1819

19-
final String path;
20+
final Path path;
2021
final String index;
2122
final VoidCallback onRemove;
22-
final ValueChanged<String> onUpdate;
23+
final ValueChanged<String?> onUpdate;
2324
final VoidCallback onShare;
2425
final VoidCallback onSave;
2526

@@ -29,8 +30,6 @@ class ImagePage extends StatefulWidget {
2930

3031
class _ImagePageState extends State<ImagePage>
3132
with AutomaticKeepAliveClientMixin {
32-
String? _origin;
33-
3433
@override
3534
bool get wantKeepAlive => true;
3635

@@ -45,18 +44,18 @@ class _ImagePageState extends State<ImagePage>
4544
children: [
4645
const SizedBox(width: 16),
4746
Expanded(
48-
child: Text(
49-
widget.index,
50-
style: const TextStyle(fontSize: 16),
51-
)),
52-
if (_origin != null) ...[
47+
child: Text(
48+
widget.index,
49+
style: const TextStyle(fontSize: 16),
50+
),
51+
),
52+
if (widget.path.isCropped) ...[
5353
iconButton(
5454
context: context,
5555
tooltip: '恢复',
5656
icon: Icons.restore,
5757
onPressed: () {
58-
widget.onUpdate(_origin!);
59-
_origin = null;
58+
widget.onUpdate(null);
6059
},
6160
),
6261
const SizedBox(width: 5),
@@ -73,11 +72,12 @@ class _ImagePageState extends State<ImagePage>
7372
tooltip: '裁剪',
7473
icon: Icons.crop,
7574
onPressed: () {
76-
String type =
77-
lookupMimeType(widget.path)?.split('/').lastOrNull ??
78-
'png';
75+
String type = lookupMimeType(widget.path.valid)
76+
?.split('/')
77+
.lastOrNull ??
78+
'png';
7979
ImageCropper().cropImage(
80-
sourcePath: widget.path,
80+
sourcePath: widget.path.valid,
8181
compressFormat: _compressFormat(type),
8282
uiSettings: [
8383
AndroidUiSettings(
@@ -93,7 +93,6 @@ class _ImagePageState extends State<ImagePage>
9393
],
9494
).then((file) {
9595
if (file != null) {
96-
_origin ??= widget.path;
9796
widget.onUpdate(file.path);
9897
}
9998
});
@@ -119,7 +118,7 @@ class _ImagePageState extends State<ImagePage>
119118
const SizedBox(height: 10),
120119
Expanded(
121120
child: Image.file(
122-
File(widget.path),
121+
File(widget.path.valid),
123122
),
124123
),
125124
const SizedBox(height: 10),

lib/main.dart

+15-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:pic_share_helper/custom_toast.dart';
1212
import 'package:pic_share_helper/icon_button.dart';
1313
import 'package:pic_share_helper/image_page.dart';
1414
import 'package:pic_share_helper/config_model.dart';
15+
import 'package:pic_share_helper/path.dart';
1516
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
1617
import 'package:saver_gallery/saver_gallery.dart';
1718
import 'package:share_plus/share_plus.dart';
@@ -112,7 +113,7 @@ class _MainPageState extends State<MainPage> {
112113
'https://github.com/bggRGjQaUbCoE/Pic-Share-Helper';
113114

114115
late final StreamSubscription _intentSub;
115-
final List<String> _paths = <String>[];
116+
final List<Path> _paths = <Path>[];
116117

117118
bool _isPicking = false;
118119
late final _imagePicker = ImagePicker();
@@ -137,7 +138,7 @@ class _MainPageState extends State<MainPage> {
137138
(List<SharedMediaFile> files) {
138139
if (files.isNotEmpty) {
139140
setState(() {
140-
_paths.addAll(files.map((item) => item.path).toList());
141+
_paths.addAll(files.map((item) => Path(item.path, null)).toList());
141142
_configList
142143
.addAll(List.generate(files.length, (_) => ConfigModel()));
143144
});
@@ -154,7 +155,7 @@ class _MainPageState extends State<MainPage> {
154155
.then((List<SharedMediaFile> files) {
155156
if (files.isNotEmpty) {
156157
setState(() {
157-
_paths.addAll(files.map((item) => item.path).toList());
158+
_paths.addAll(files.map((item) => Path(item.path, null)).toList());
158159
_configList.addAll(List.generate(files.length, (_) => ConfigModel()));
159160
});
160161
}
@@ -208,7 +209,7 @@ class _MainPageState extends State<MainPage> {
208209
},
209210
onUpdate: (path) {
210211
setState(() {
211-
_paths[index] = path;
212+
_paths[index].cropped = path;
212213
});
213214
},
214215
onShare: () => _onShare([_paths[index]]),
@@ -342,7 +343,8 @@ class _MainPageState extends State<MainPage> {
342343
_imagePicker.pickMultiImage(imageQuality: 100).then((files) {
343344
if (files.isNotEmpty) {
344345
setState(() {
345-
_paths.addAll(files.map((item) => item.path).toList());
346+
_paths.addAll(
347+
files.map((item) => Path(item.path, null)).toList());
346348
_configList
347349
.addAll(List.generate(files.length, (_) => ConfigModel()));
348350
});
@@ -428,7 +430,7 @@ class _MainPageState extends State<MainPage> {
428430
),
429431
);
430432

431-
void _onShare(List<String> paths) async {
433+
void _onShare(List<Path> paths) async {
432434
if (_paths.isNotEmpty) {
433435
try {
434436
List<XFile> files = [];
@@ -438,10 +440,10 @@ class _MainPageState extends State<MainPage> {
438440
'processing${paths.length > 1 ? ' ${i + 1}/${paths.length}' : ''}',
439441
);
440442
String type =
441-
lookupMimeType(paths[i])?.split('/').lastOrNull ?? 'png';
443+
lookupMimeType(paths[i].valid)?.split('/').lastOrNull ?? 'png';
442444
await FlutterImageCompress.compressAndGetFile(
443-
paths[i],
444-
'${paths[i]}${DateTime.now().millisecondsSinceEpoch}.$type',
445+
paths[i].valid,
446+
'${paths[i].valid}${DateTime.now().millisecondsSinceEpoch}.$type',
445447
quality: _currentConfig.quality.round(),
446448
keepExif: !_currentConfig.removeExif,
447449
format: _compressFormat(type),
@@ -462,7 +464,7 @@ class _MainPageState extends State<MainPage> {
462464
}
463465
}
464466

465-
void _onSave(List<String> paths) async {
467+
void _onSave(List<Path> paths) async {
466468
if (paths.isNotEmpty) {
467469
try {
468470
for (int i = 0; i < paths.length; i++) {
@@ -471,15 +473,15 @@ class _MainPageState extends State<MainPage> {
471473
'processing${paths.length > 1 ? ' ${i + 1}/${paths.length}' : ''}',
472474
);
473475
String type =
474-
lookupMimeType(paths[i])?.split('/').lastOrNull ?? 'png';
476+
lookupMimeType(paths[i].valid)?.split('/').lastOrNull ?? 'png';
475477
await FlutterImageCompress.compressWithFile(
476-
paths[i],
478+
paths[i].valid,
477479
quality: _currentConfig.quality.round(),
478480
keepExif: !_currentConfig.removeExif,
479481
format: _compressFormat(type),
480482
).then((data) {
481483
if (data != null) {
482-
String imageName = paths[i].split('/').lastOrNull ??
484+
String imageName = paths[i].valid.split('/').lastOrNull ??
483485
'${DateTime.now().millisecondsSinceEpoch ~/ 1000}.jpg';
484486
SaverGallery.saveImage(
485487
data,

lib/path.dart

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Path {
2+
Path(
3+
this.origin,
4+
this.cropped,
5+
);
6+
String origin;
7+
String? cropped;
8+
9+
String get valid => cropped ?? origin;
10+
11+
bool get isCropped => cropped != null;
12+
}

0 commit comments

Comments
 (0)