기존에는 image_picker를 사용하여 사진 선택 처리를 하였는데 image_picker는 기존 네이티브 갤러리 UI를 제공한다. UI를 커스텀하고자 image_picker가 아닌 photo_manager를 이용하여 구현하고자 한다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 카메라 권한 -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- Devices running Android (API level 33) or higher -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- Devices running Android 12L (API level 32) or lower -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<key>NSCameraUsageDescription</key>
<string>카메라 권한 허용을 해주세요</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>포토 라이브러리 권한을 허용해주세요.</string>
List<AssetEntity> _imageList = [];
List<AssetEntity> _selectedImageList = [];
List<AssetPathEntity> _albumList = [];
AssetPathEntity? _selectedAlbum;
int _currentPage = 0; // 현재 페이지
final int _pageSize = 30; // 한 번에 로드할 이미지 개수
final ScrollController _scrollController = ScrollController();
bool _isRefresh = true;
Future<void> _loadAlbumList() async {
final fetchedAlbumList =
await PhotoManager.getAssetPathList(type: RequestType.image);
if (fetchedAlbumList.isEmpty) return;
setState(() {
_albumList = fetchedAlbumList;
_selectedAlbum = _albumList.first;
});
_loadImageList(_selectedAlbum!, albumChanged: true);
}
Future<void> _loadImageList(AssetPathEntity album,
{bool albumChanged = false}) async {
if (albumChanged) {
_isRefresh = true;
_currentPage = 0;
} else {
// 앨범 변경을 안한 경우는 스크롤을 내려서 불러오는 경우
final totalImagesCount = await album.assetCountAsync;
if (_imageList.length == totalImagesCount) {
return;
}
}
final assets =
await album.getAssetListPaged(page: _currentPage, size: _pageSize);
setState(() {
if (albumChanged) {
_imageList = assets;
} else {
_imageList.addAll(assets);
}
_currentPage++;
_isRefresh = false;
});
}
class _AppBar extends StatelessWidget {
final AssetPathEntity? selectedAlbum;
final List<AssetPathEntity> albumList;
final ValueChanged<AssetPathEntity?> onAlbumChanged;
final VoidCallback onClose;
final VoidCallback? onComplete;
const _AppBar({
super.key,
this.selectedAlbum,
required this.albumList,
required this.onAlbumChanged,
required this.onClose,
this.onComplete,
});
Widget build(BuildContext context) {
return Stack(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: onClose,
icon: const Icon(Icons.close, size: 24),
),
TextButton(
onPressed: onComplete,
child: Text('완료'),
),
],
),
Align(
alignment: Alignment.center,
child: Container(
child: albumList.isNotEmpty
? DropdownButton(
value: selectedAlbum,
dropdownColor: Colors.white,
items: albumList.map((album) {
return DropdownMenuItem(
value: album,
child: Text(
album.isAll ? '최근항목' : album.name,
),
);
}).toList(),
onChanged: onAlbumChanged,
underline: const SizedBox.shrink(),
)
: const SizedBox(),
),
),
],
);
}
}
class _PhotoGridView extends StatelessWidget {
final List<AssetEntity> imageList;
final List<AssetEntity> selectedImageList;
final ScrollController scrollController;
final Function(AssetEntity) onImageTap;
final VoidCallback onCameraTap;
const _PhotoGridView({
super.key,
required this.imageList,
required this.selectedImageList,
required this.scrollController,
required this.onImageTap,
required this.onCameraTap,
});
Widget build(BuildContext context) {
return GridView.builder(
padding: const EdgeInsets.all(8.5),
controller: scrollController,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 2,
mainAxisSpacing: 2,
),
itemCount: imageList.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
return GestureDetector(
onTap: () => onCameraTap.call(),
child: ClipRRect(
borderRadius: BorderRadius.circular(12),
child: Container(
decoration: const BoxDecoration(
color: Colors.grey,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.camera_alt),
Text('촬영하기')
],
),
),
),
);
}
final image = imageList[index - 1];
final isSelected = selectedImageList.contains(image);
return GestureDetector(
onTap: () => onImageTap(image),
child: Stack(
fit: StackFit.expand,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(12),
child: AssetEntityImage(
image,
isOriginal: false,
thumbnailSize: const ThumbnailSize.square(200),
fit: BoxFit.cover,
),
),
if (isSelected)
Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.amber, width: 3)),
),
Positioned(
top: 0,
right: 0,
child: Checkbox(
value: isSelected,
onChanged: (value) {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
checkColor: Colors.white,
activeColor: Colors.amber,
),
),
],
),
);
},
);
}
}
Widget build(BuildContext context) {
return Container(
color: Colors.white,
padding: EdgeInsets.only(
top: widget.mediaQuery.padding.top,
bottom: widget.mediaQuery.padding.bottom,
),
child: Column(
children: [
_AppBar(
albumList: _albumList,
selectedAlbum: _selectedAlbum,
onComplete: _selectedImageList.isNotEmpty ? _onComplete : null,
onAlbumChanged: (value) {
if (value != null) {
setState(() {
_selectedAlbum = value;
});
_loadImageList(value, albumChanged: true);
}
},
onClose: () => Navigator.of(context).pop(),
),
Expanded(
child: _isRefresh
? Center(child: CircularProgressIndicator())
: _PhotoGridView(
imageList: _imageList,
selectedImageList: _selectedImageList,
scrollController: _scrollController,
onImageTap: _onImageTap,
onCameraTap: _onCameraTap,
),
),
],
),
);
}
void _onImageTap(AssetEntity image) {
if (widget.multiSelect) {
setState(() {
_selectedImageList.contains(image)
? _selectedImageList.remove(image)
: _selectedImageList.add(image);
});
} else {
setState(() {
_selectedImageList.contains(image)
? _selectedImageList.remove(image)
: _selectedImageList = [image];
});
}
}
void _onCameraTap() async {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.camera);
if (pickedFile != null) {
widget.onSelectImages.call([File(pickedFile.path)]);
Navigator.of(context).pop();
}
}
void _onComplete() async {
List<File> imageFiles = [];
for (var image in _selectedImageList) {
final file = await image.file;
if (file != null) {
imageFiles.add(file);
}
}
widget.onSelectImages.call(imageFiles);
Navigator.of(context).pop();
}
void showPhotoPickerModal(BuildContext context,
{bool multiSelect = false,
required Function(List<File>) onSelectedImages}) {
final mediaQuery = MediaQuery.of(context);
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.white,
builder: (context) {
return DraggableScrollableSheet(
initialChildSize: 1,
builder: (context, scrollController) {
return PhotoPickerModal(
mediaQuery: mediaQuery,
multiSelect: multiSelect,
onSelectImages: onSelectedImages,
);
},
);
},
);
}