getDefaultGridItems function

List<RCKExtensionMenuItemConfig> getDefaultGridItems({
  1. TapBeforePermissionCallback? onTapBeforePermission,
})

Implementation

List<RCKExtensionMenuItemConfig> getDefaultGridItems(
    {TapBeforePermissionCallback? onTapBeforePermission}) {
  return [
    RCKExtensionMenuItemConfig(
      title: '照片',
      icon:
          ImageUtil.getImageWidget(RCKThemeProvider().themeIcon.gallery ?? ''),
      onTapWithContext: (context) async {
        // 检查相册权限
        Permission checkPermission;
        if (Platform.isAndroid) {
          final androidInfo = await DeviceInfoPlugin().androidInfo;
          if (androidInfo.version.sdkInt <= 32) {
            checkPermission = Permission.storage;
          } else {
            checkPermission = Permission.photos;
          }
        } else {
          checkPermission = Permission.photos;
        }
        if (onTapBeforePermission != null && context.mounted) {
          await onTapBeforePermission(context, checkPermission);
        }
        final status = await checkPermission.request();

        if (status.isGranted) {
          // 有权限
          if (context.mounted) {
            context.read<RCKAudioPlayerProvider>().stopVoiceMessage();
          }

          List<AssetEntity>? assets = [];
          if (context.mounted) {
            assets = await AssetPicker.pickAssets(
              context,
              pickerConfig: AssetPickerConfig(
                requestType: RequestType.image,
                maxAssets: 50,
                textDelegate: const AssetPickerTextDelegate(),
              ),
            );
          }

          if (assets == null || assets.isEmpty) return;

          String imageFileListString = '';
          for (int i = 0; i < assets.length; i++) {
            final asset = assets[i];
            final file = await asset.file;
            final path = file?.path ?? '';
            imageFileListString += path;
            if (i < assets.length - 1) imageFileListString += '|';

            if (path.isEmpty) continue;
            final mime = asset.mimeType ?? '';
            if (mime == 'image/gif' || path.toLowerCase().endsWith('.gif')) {
              if (context.mounted) {
                await context.read<RCKChatProvider>().addGifMessage(path);
              }
            } else {
              if (context.mounted) {
                await context.read<RCKChatProvider>().addImageMessage(path);
              }
            }
          }
          RCIMWrapperPlatform.instance.writeLog(
              'GridButtonWidget pickAssets(images)',
              '',
              0,
              imageFileListString);
        } else {
          // 权限被拒绝,显示提示
          if (context.mounted) {
            showDialog(
              context: context,
              builder: (context) => AlertDialog(
                title: const Text('提示'),
                content: const Text('需要相册权限才能选择图片'),
                actions: [
                  TextButton(
                    onPressed: () => Navigator.pop(context),
                    child: const Text('确定'),
                  ),
                ],
              ),
            );
          }
        }
      },
      iconSize: kInputExtentionIconSize,
    ),
    RCKExtensionMenuItemConfig(
      title: '视频',
      icon: ImageUtil.getImageWidget(
          RCKThemeProvider().themeIcon.playVideoInMore ?? '',
          color: RCKThemeProvider().themeColor.textPrimary),
      onTapWithContext: (context) async {
        // 检查相册权限
        Permission checkPermission;
        if (Platform.isAndroid) {
          final androidInfo = await DeviceInfoPlugin().androidInfo;
          if (androidInfo.version.sdkInt <= 32) {
            checkPermission = Permission.storage;
          } else {
            checkPermission = Permission.photos;
          }
        } else {
          checkPermission = Permission.photos;
        }
        if (onTapBeforePermission != null && context.mounted) {
          await onTapBeforePermission(context, checkPermission);
        }
        final status = await checkPermission.request();

        if (status.isGranted) {
          // 有权限
          if (context.mounted) {
            context.read<RCKAudioPlayerProvider>().stopVoiceMessage();
          }

          List<AssetEntity>? result = [];
          if (context.mounted) {
            result = await AssetPicker.pickAssets(
              context,
              pickerConfig: AssetPickerConfig(
                requestType: RequestType.video,
                maxAssets: 1,
                filterOptions: pm.FilterOptionGroup()
                  ..setOption(
                    pm.AssetType.video,
                    const pm.FilterOption(
                      durationConstraint:
                          pm.DurationConstraint(max: Duration(seconds: 11)),
                    ),
                  ),
                textDelegate: const AssetPickerTextDelegate(),
              ),
            );
          }

          if (result != null && result.isNotEmpty) {
            final AssetEntity picked = result.first;
            final file = await picked.file;
            final path = file?.path;
            final durationSec = picked.duration;
            if (path == null || path.isEmpty) return;
            // 获取视频时长(双保险)
            if (durationSec > 10) {
              if (context.mounted) {
                await showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                    title: const Text('提示'),
                    content: const Text('视频时长不能超过10秒'),
                    actions: [
                      TextButton(
                        onPressed: () => Navigator.pop(context),
                        child: const Text('确定'),
                      ),
                    ],
                  ),
                );
              }
              return;
            }
            RCIMWrapperPlatform.instance.writeLog('GridButtonWidget pickVideo',
                '', 0, 'video: $path duration: $durationSec');

            if (context.mounted) {
              context
                  .read<RCKChatProvider>()
                  .addSightMessage(path, durationSec);
            }
          }
        } else {
          // 权限被拒绝,显示提示
          if (context.mounted) {
            showDialog(
              context: context,
              builder: (context) => AlertDialog(
                title: const Text('提示'),
                content: const Text('需要相册权限才能选择视频'),
                actions: [
                  TextButton(
                    onPressed: () => Navigator.pop(context),
                    child: const Text('确定'),
                  ),
                ],
              ),
            );
          }
        }
      },
      iconSize: kInputExtentionIconSize,
    ),
    RCKExtensionMenuItemConfig(
      title: '拍照',
      icon: ImageUtil.getImageWidget(RCKThemeProvider().themeIcon.camera ?? ''),
      onTapWithContext: (context) async {
        // 检查相机权限
        Permission checkPermission = Permission.camera;
        if (onTapBeforePermission != null) {
          await onTapBeforePermission(context, checkPermission);
        }
        final status = await checkPermission.request();
        if (status.isGranted) {
          // 有权限
          if (context.mounted) {
            context.read<RCKAudioPlayerProvider>().stopVoiceMessage();
          }

          AssetEntity? shot;
          if (context.mounted) {
            shot = await CameraPicker.pickFromCamera(
              context,
              pickerConfig: const CameraPickerConfig(
                enableRecording: false,
                textDelegate: CameraPickerTextDelegate(),
              ),
            );
          }

          final file = await shot?.file;
          final String? path = file?.path;
          RCIMWrapperPlatform.instance
              .writeLog('GridButtonWidget cameraPhoto', '', 0, 'path: $path');

          if (path != null && path.isNotEmpty) {
            if (context.mounted) {
              context.read<RCKChatProvider>().addImageMessage(path);
            }
          }
        } else {
          // 权限被拒绝,显示提示
          if (context.mounted) {
            showDialog(
              context: context,
              builder: (context) => AlertDialog(
                title: const Text('提示'),
                content: const Text('需要相机权限才能拍照'),
                actions: [
                  TextButton(
                      onPressed: () => Navigator.pop(context),
                      child: const Text('确定')),
                ],
              ),
            );
          }
        }
      },
      iconSize: kInputExtentionIconSize,
    ),
    RCKExtensionMenuItemConfig(
      title: '拍摄',
      icon: ImageUtil.getImageWidget(RCKThemeProvider().themeIcon.filming ?? '',
          color: RCKThemeProvider().themeColor.textPrimary),
      onTapWithContext: (context) async {
        // 检查相机权限
        Permission checkPermission = Permission.camera;
        if (onTapBeforePermission != null) {
          await onTapBeforePermission(context, checkPermission);
        }
        final status = await checkPermission.request();
        if (status.isGranted) {
          // 有权限
          if (context.mounted) {
            context.read<RCKAudioPlayerProvider>().stopVoiceMessage();
          }

          AssetEntity? pickMedia;
          if (context.mounted) {
            pickMedia = await CameraPicker.pickFromCamera(
              context,
              pickerConfig: const CameraPickerConfig(
                enableRecording: true,
                onlyEnableRecording: true,
                enableTapRecording: true,
                maximumRecordingDuration: Duration(seconds: 10),
                textDelegate: CameraPickerTextDelegate(),
              ),
            );
          }

          if (pickMedia != null) {
            final file = await pickMedia.file;
            final String? capturedPath = file?.path;
            if (capturedPath == null || capturedPath.isEmpty) return;

            VideoPlayerController videoPlayerController =
                VideoPlayerController.file(File(capturedPath));
            await videoPlayerController.initialize();
            int videoDuration = videoPlayerController.value.duration.inSeconds;

            RCIMWrapperPlatform.instance.writeLog('GridButtonWidget pickVideo',
                '', 0, 'pickMedia: $capturedPath duration: $videoDuration');

            if (videoDuration <= 1) {
              if (context.mounted) {
                showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                    title: const Text('提示'),
                    content: const Text('视频时长不能小于1秒'),
                    actions: [
                      TextButton(
                          onPressed: () => Navigator.pop(context),
                          child: const Text('确定')),
                    ],
                  ),
                );
              }
              return;
            }
            if (context.mounted) {
              context
                  .read<RCKChatProvider>()
                  .addSightMessage(capturedPath, videoDuration);
            }
            await videoPlayerController.dispose();
          }
        } else {
          // 权限被拒绝,显示提示
          if (context.mounted) {
            showDialog(
              context: context,
              builder: (context) => AlertDialog(
                title: const Text('提示'),
                content: const Text('需要相机权限才能拍摄'),
                actions: [
                  TextButton(
                      onPressed: () => Navigator.pop(context),
                      child: const Text('确定')),
                ],
              ),
            );
          }
        }
      },
      iconSize: kInputExtentionIconSize,
    ),
    RCKExtensionMenuItemConfig(
      title: '文件',
      icon:
          ImageUtil.getImageWidget(RCKThemeProvider().themeIcon.document ?? ''),
      onTapWithContext: (context) async {
        FilePickerResult? res = await FilePicker.platform.pickFiles();
        if (res != null) {
          String resString = '';
          for (int i = 0; i < res.files.length; i++) {
            resString += res.files[i].path ?? '';
            if (i < res.files.length - 1) {
              resString += '|';
            }
          }

          RCIMWrapperPlatform.instance
              .writeLog('GridButtonWidget pickFiles', '', 0, 'res: $resString');

          if (context.mounted) {
            context
                .read<RCKChatProvider>()
                .addFileMessage(res.files.single.path!);
          }
        }
      },
      iconSize: kInputExtentionIconSize,
    ),
  ];
}