-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25fdbd8
commit 9e0ccb5
Showing
1 changed file
with
19 additions
and
11 deletions.
There are no files selected for viewing
30 changes: 19 additions & 11 deletions
30
packages/mottai_flutter_app/lib/development/image_pick/image_pick_function.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
// ignore_for_file: avoid_catches_without_on_clauses | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:image_picker/image_picker.dart'; | ||
|
||
|
||
enum PickType { | ||
camera, | ||
gallery, | ||
} | ||
|
||
final ImagePicker _picker = ImagePicker(); | ||
|
||
///一枚のみ選択する場合 | ||
///写真を撮影するか選択するかはenumのPickTypeで渡す | ||
Future<File> fetchSingleImage(PickType pickType) async { | ||
final _imageFile = await _picker.pickImage( | ||
source: | ||
pickType == PickType.camera ? ImageSource.camera : ImageSource.gallery, | ||
); | ||
if (_imageFile == null) { | ||
throw Exception('画像が選択されませんでした。'); | ||
Future<File?> fetchSingleImage(PickType pickType) async { | ||
try { | ||
final _imageFile = await _picker.pickImage( | ||
source: pickType == PickType.camera | ||
? ImageSource.camera | ||
: ImageSource.gallery, | ||
); | ||
return _imageFile == null ? null : File(_imageFile.path); | ||
} catch (e) { | ||
return null; | ||
} | ||
return File(_imageFile.path); | ||
} | ||
|
||
///複数選択する場合 | ||
Future<List<File>> fetchMultipleImage() async { | ||
final _imageFiles = await _picker.pickMultiImage(); | ||
return _imageFiles.map((e) => File(e.path)).toList(); | ||
try { | ||
final _imageFiles = await _picker.pickMultiImage(); | ||
return _imageFiles.map((e) => File(e.path)).toList(); | ||
} catch (e) { | ||
return []; | ||
} | ||
} |