import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:image_gallery_saver/image_gallery_saver.dart'; import 'package:image_picker/image_picker.dart'; import 'package:lszlgl/base/base_lifecycle_state.dart'; import 'package:lszlgl/utils/permission_utils.dart'; import 'package:lszlgl/widget/card_item.dart'; import 'package:lszlgl/widget/gallery_photo_view_wrapper.dart'; import 'package:permission_handler/permission_handler.dart'; class PhotoCardItem extends StatefulWidget { final String title; final String? imgUrl; final bool isDetail; final Function(String filePath) filePathCallBack; const PhotoCardItem({super.key, required this.title, required this.isDetail, this.imgUrl, required this.filePathCallBack}); @override State createState() => _PhotoCardItemState(); } class _PhotoCardItemState extends State { final ImagePicker _picker = ImagePicker(); XFile? photoPic; // 弹窗选择 照片或拍照 void showBotSheet() { showModalBottomSheet( context: context, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.only(topLeft: Radius.circular(14), topRight: Radius.circular(14))), builder: (context) { return Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const SizedBox(height: 8), TextButton( child: const Text('相册'), onPressed: () { Navigator.pop(context); pickImage(false); }), TextButton( child: const Text('拍照'), onPressed: () async { Navigator.pop(context); bool res = await PermissionHandler.handleWith(Permission.storage); if (res) { pickImage(true); } }), const SizedBox(height: 8), const Divider(color: Color(0xFFF3F3F3), height: 0, thickness: 1), TextButton( child: const Text('取消', style: TextStyle(color: Colors.grey)), onPressed: () { Navigator.pop(context); }), const SizedBox(height: 4), ], ); }, ); } /// 保存照片到本地 void savePicture(String? path) async { if (path == null) return; final File imageFile = File(path); final Uint8List imageBytes = await imageFile.readAsBytes(); await ImageGallerySaver.saveImage(imageBytes); } /// 选择照片或拍照 void pickImage(bool isCamera) async { try { XFile? photo = await _picker.pickImage( source: isCamera ? ImageSource.camera : ImageSource.gallery, imageQuality: 25, ); if (photo == null) return; if (isCamera) { savePicture(photo.path); // 保存到本地 } setState(() { photoPic = photo; }); widget.filePathCallBack.call(photo.path); } catch (e) { MyNavigator.showToast('$e'); } } void showBigImage({XFile? file}) { FocusManager.instance.primaryFocus?.unfocus(); Navigator.push( context, MaterialPageRoute( builder: (context) { return file != null ? GalleryPhotoViewWrapper( galleryItems: [file], ) : GalleryPhotoViewWrapper( netImgList: [widget.imgUrl!], ); }, ), ); } @override Widget build(BuildContext context) { return Stack( alignment: Alignment.centerLeft, children: [ CardItemWidget( widget.title, bottomLine: true, // rightText: widget.isDetail ? '点击查看' : '点击上传', rightChild: widget.isDetail || photoPic == null ? const SizedBox.shrink() : GestureDetector( child: Image.file( File(photoPic!.path), width: 44, height: 44, fit: BoxFit.cover, ), onTap: () => showBigImage(file: photoPic!), ), trailing: GestureDetector( child: Text( widget.isDetail ? '点击查看' : photoPic == null ? '点击选择' : '重新选择', style: const TextStyle(fontSize: 14, color: Color(0xFF01B2C8)), ), onTap: () { if (widget.isDetail) { // 详情 查看照片 if (widget.imgUrl == null) { MyNavigator.showToast('地址错误'); return; } showBigImage(); } else { showBotSheet(); // 编辑-选择照片 } }, ), onTap: () {}, ), Container(width: 4, height: 4, color: const Color(0xFF01B2C8)) ], ); } }