123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 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<PhotoCardItem> createState() => _PhotoCardItemState();
- }
- class _PhotoCardItemState extends State<PhotoCardItem> {
- 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))
- ],
- );
- }
- }
|