import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:wechat_assets_picker/wechat_assets_picker.dart'; import 'package:wechat_camera_picker/wechat_camera_picker.dart'; import 'package:video_player/video_player.dart'; import '../../generated/assets.dart'; class MoreOptionsView extends StatelessWidget { final bool isVisible; final ValueChanged> onImageSelected; final ValueChanged onCameraSelected; final Function(String filePath, int duration)? onVideoSelected; const MoreOptionsView({ required this.isVisible, required this.onImageSelected, required this.onCameraSelected, this.onVideoSelected, super.key, }); @override Widget build(BuildContext context) { return AnimatedContainer( duration: Duration(milliseconds: 300), height: isVisible ? 180.h : 0, color: Colors.white, child: isVisible ? Container( padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.h), child: Column( children: [ SizedBox(height: 10.h), // 第一行选项 Row( mainAxisAlignment: MainAxisAlignment.start, children: [ // 图片/视频选项 GestureDetector( onTap: () async{ try { print('📷 [MoreOptionsView] 打开相册选择图片/视频'); // 支持同时选择图片和视频 List? result = await AssetPicker.pickAssets( context, pickerConfig: const AssetPickerConfig( requestType: RequestType.common, // 支持图片和视频 maxAssets: 9, ), ); if (result != null && result.isNotEmpty) { print('选择了 ${result.length} 个文件'); // 分别处理图片和视频 List imagePaths = []; for (var asset in result) { final file = await asset.file; if (file != null) { print('文件类型: ${asset.type}, 路径: ${file.path}'); if (asset.type == AssetType.video) { // 视频文件 print('检测到视频文件'); final duration = asset.duration; if (onVideoSelected != null) { onVideoSelected!(file.path, duration); } } else { // 图片文件 imagePaths.add(file.path); } } } // 批量发送图片 if (imagePaths.isNotEmpty) { print('发送 ${imagePaths.length} 张图片'); onImageSelected(imagePaths); } } } catch (e) { print('❌ 选择文件失败: $e'); if (Get.isLogEnable) { Get.log("选择图片失败: $e"); } } }, child: Column( children: [ Container( width: 60.w, height: 60.w, decoration: BoxDecoration( color: Color(0xffF0F5FF), borderRadius: BorderRadius.circular(8.w), ), padding: EdgeInsets.all(10.w), child: Image.asset(Assets.imagesPhoto, width: 40.w, height: 40.w), ), SizedBox(height: 8.h), Text( "相册", style: TextStyle( fontSize: 12.sp, color: Colors.black, ), ), ], ), ), SizedBox(width: 40.w), // 相机选项(支持拍照和拍视频) GestureDetector( onTap: () async{ try { print('📷 [MoreOptionsView] 打开相机'); // 支持拍照和录像 AssetEntity? entity = await CameraPicker.pickFromCamera( context, pickerConfig: const CameraPickerConfig( enableRecording: true, // 启用录像功能 ), ); if (entity != null) { final file = await entity.file; if (file != null) { print('文件类型: ${entity.type}, 路径: ${file.path}'); if (entity.type == AssetType.video) { // 拍摄的视频 print('检测到视频文件'); final duration = await _getVideoDuration(file.path); if (onVideoSelected != null) { onVideoSelected!(file.path, duration); } } else { // 拍摄的照片 print('检测到图片文件'); onCameraSelected(file.path); } } } } catch (e) { print('❌ 相机操作失败: $e'); if (Get.isLogEnable) { Get.log("拍照失败: $e"); } } }, child: Column( children: [ Container( width: 60.w, height: 60.w, decoration: BoxDecoration( color: Color(0xffF0F5FF), borderRadius: BorderRadius.circular(8.w), ), padding: EdgeInsets.all(10.w), child: Image.asset(Assets.imagesCamera, width: 40.w, height: 40.w), ), SizedBox(height: 8.h), Text( "相机", style: TextStyle( fontSize: 12.sp, color: Colors.black, ), ), ], ), ), ], ), ], ), ) : null, ); } // 获取视频时长 Future _getVideoDuration(String filePath) async { try { final controller = VideoPlayerController.file(File(filePath)); await controller.initialize(); final duration = controller.value.duration.inSeconds; await controller.dispose(); return duration; } catch (e) { print('获取视频时长失败: $e'); return 0; } } }