xCMD_XMedia_GetMedia method

Future<File?> xCMD_XMedia_GetMedia(
  1. void setState(
    1. void ()
    ),
  2. XFDataItem item
)

Comando per ottenre gli XMedia e aprire XMediaRecorder

Implementation

Future<File?> xCMD_XMedia_GetMedia(void Function(void Function()) setState, XFDataItem item) async {
  item.isShowDetails = true;
  FocusScope.of(context).unfocus();
  // late CameraController _photoController;
  if (_cameras.isEmpty) _cameras = await availableCameras();
  widget.xApp!.state.cameras.clear();
  widget.xApp!.state.cameras.addAll(_cameras);
  late CameraController _photoController;
  if (XMediaRecorder.xCurrentCamera == null) {
    XMediaRecorder.xCurrentCamera = (kIsWeb && _cameras.length > 1)
        ? _cameras[1]
        : kIsWeb
            ? _cameras[0]
            : _cameras.length > 1
                ? _cameras[1]
                : _cameras[0];
  }

  await showDialog<bool?>(
      context: context,
      barrierDismissible: false,
      builder: (builder) {
        return FutureBuilder(future: () async {
          _photoController = CameraController(XMediaRecorder.xCurrentCamera!, ResolutionPreset.medium, enableAudio: true);
          await _photoController.initialize();
          setState(() {});
          return true;
        }(),
            // xApp.app_Name = "Coffee Meeting\n${xclAnag.cognome} ${xclAnag.nome}";
            builder: (context, snapshot) {
          return StatefulBuilder(builder: (context, setState) {
            final global_Key = GlobalKey();
            return XAlertDialog(
                colorBackGround: Colors.grey[900],
                actionsBTNarea_Padding: EdgeInsets.all(0),
                content_VerticalPadding: 0,
                title_Margin: EdgeInsets.all(0),
                child: Container(
                    alignment: Alignment.topCenter,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        saving
                            ? Container()

                            ///Row delle Preview dei media fatti
                            : Row(mainAxisAlignment: liXMedias_MediaRecorder.value.isNotEmpty ? MainAxisAlignment.start : MainAxisAlignment.center, children: [
                                Expanded(
                                    child: SingleChildScrollView(
                                        scrollDirection: Axis.horizontal,
                                        child: Row(
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            crossAxisAlignment: CrossAxisAlignment.center,
                                            children: liXMedias_MediaRecorder.value.isEmpty
                                                ? [Container(height: 80, width: MediaQuery.of(context).size.width, color: Colors.grey[800], alignment: Alignment.center)]
                                                : liXMedias_MediaRecorder.value
                                                    .map((e) => InkWell(
                                                        onTap: () {
                                                          if (e.mediaType == XEnums.mediaType.image.value) {
                                                            xCMD_XMedia_OpenPhoto(e, item);
                                                          } else if (e.mediaType == XEnums.mediaType.video.value) {
                                                            xCMD_XMedia_OpenVideo(e, pathPreviewVideo: "${XLibStorage.filePath}/XMEDIAS/${e.id}_.mp4", setState: setState);
                                                          } else if (e.mediaType == XEnums.mediaType.audio.value) {
                                                            xCMD_XMedia_OpenAudio(e, item, true);
                                                          }
                                                        },
                                                        onLongPress: () async {
                                                          await xOnDelXMedia_InPreview(e, setState);
                                                        },
                                                        child: e.mediaType == XEnums.mediaType.image.value
                                                            ? Container(
                                                                width: 80,
                                                                height: 80,
                                                                child: Stack(children: [
                                                                  Container(margin: EdgeInsets.symmetric(horizontal: 5), width: 80, child: Image.memory(e.payload!, width: 80, height: 80)),
                                                                  e.overlaySurface != null
                                                                      ? Container(
                                                                          margin: EdgeInsets.only(bottom: 50),
                                                                          child: Image.memory(
                                                                            e.overlaySurface!,
                                                                            height: 80,
                                                                            width: 80,
                                                                          ))
                                                                      : Container()
                                                                ]))
                                                            : e.mediaType == XEnums.mediaType.video.value
                                                                ? Container(
                                                                    margin: EdgeInsets.symmetric(horizontal: 5),
                                                                    decoration: BoxDecoration(color: Colors.grey[800], image: DecorationImage(image: MemoryImage(e.miniatura!))),
                                                                    width: 80,
                                                                    height: 80,
                                                                    child: Container(
                                                                      child: Icon(Icons.play_arrow, size: 28, color: Colors.blueAccent[900]),
                                                                    ))
                                                                : Container(
                                                                    margin: EdgeInsets.symmetric(horizontal: 5),
                                                                    decoration: BoxDecoration(color: Colors.grey[800]),
                                                                    width: 80,
                                                                    height: 80,
                                                                    child: Icon(Icons.mic),
                                                                  )))
                                                    .toList())))
                              ]),

                        ///XMEDIA RECORDER
                        ///XMEDIA RECORDER
                        Container(
                            width: MediaQuery.of(context).size.width,
                            height: MediaQuery.of(context).orientation == Orientation.portrait ? MediaQuery.of(context).size.height / 1.34 : MediaQuery.of(context).size.height / 1.42,
                            child: XMediaRecorder<XFDataItem>(
                              global_Key,
                              parentSetState: setState,
                              photoController: _photoController,
                              itemOriginal: item,
                              lXMedias: liXMedias_MediaRecorder.value,
                              xApp: widget.xApp!,
                              onStartTakeVideo: (parentState) => setState(() => isRecording = true),
                              onEndTakeVideo: (parentState) => setState(() => isRecording = false),
                              onTakeMedia: (parentState) => setState(() => isTakingPhoto = isTakingPhoto.not()),
                            ))

                        ///XMEDIA RECORDER
                        ///XMEDIA RECORDER
                      ],
                    )),
                title_Widget: Container(),
                title_Style: XStyles.xStyTextForLabel(textColor: Colors.yellow),
                content_insetPadding: EdgeInsets.all(0),
                actionsBTNarea_Child: Container(
                    height: 70,
                    child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                      isRecording || isTakingPhoto
                          ? Expanded(
                              child: isRecording
                                  ? Text("Sto Registrando...", style: XStyles.xStyTextForSubLabel(textColor: Colors.red), textAlign: TextAlign.center) //
                                  : isTakingPhoto.not()
                                      ? Container()
                                      : XStatefulWidgetBase.LoaderIndicator(isTakingPhoto, label: "Acquisizione Media..", fixHeight: 30, fixWidth: 30))
                          : Container(),
                      isTakingPhoto || isRecording
                          ? Container()
                          : Expanded(
                              child: XBtnbase(
                              height: 45,
                              icon_Widget: Icon(Icons.undo, color: Colors.red, size: 30),
                              backGroundColor: Colors.grey[800]!,
                              onPressed: () async {
                                liXMedias_MediaRecorder.value.clear();
                                await _photoController.dispose();
                                Navigator.pop(context, false);
                              },
                              elevation: 0,
                              label: "",
                            )),
                      SizedBox(width: 6),
                      isTakingPhoto || isRecording
                          ? Container()
                          : Expanded(
                              child: XBtnbase(
                              label: "Dalla Galleria...",
                              height: 45,
                              backGroundColor: Colors.grey[800]!,
                              elevation: 0,
                              onPressed: () async {
                                await _photoController.pausePreview();
                                await _getImages_FromGallery(item, setState);
                                setState(() {});
                                // if (_images.isNotEmpty) {
                                //   _images.forEach((xclFile) async {
                                //     print(xclFile.name);
                                //     var xMedia = XMediaItem(
                                //       Uuid().v4(),
                                //       xDocID: item.id,
                                //       mediaType: xclFile.name.endsWith("mp4")
                                //           ? XEnums.mediaType.video.value
                                //           : (xclFile.name.endsWith("jpg") || xclFile.name.endsWith("png"))
                                //               ? XEnums.mediaType.image.value
                                //               : xclFile.name.endsWith("aac")
                                //                   ? XEnums.mediaType.audio.value
                                //                   : XEnums.mediaType.image.value,
                                //       payload: File(xclFile.path).readAsBytesSync(),
                                //       isNew: true,
                                //     );
                                //     setState(() => liXMedias_MediaRecorder.value.add(xMedia));
                                //   });
                                // }
                                await _photoController.resumePreview();
                              },
                            )),
                      SizedBox(width: 6),
                      liXMedias_MediaRecorder.value.isNotEmpty
                          ? isTakingPhoto || isRecording
                              ? Container()
                              : Expanded(
                                  child: XBtnbase(
                                  height: 45,
                                  backGroundColor: Colors.grey[900]!,
                                  onPressed: () async {
                                    _photoController.pausePreview();
                                    setState(() => saving = true);
                                    if (liXMedias_MediaRecorder.value.isNotEmpty) {
                                      isTakingPhoto = true;
                                      await Future.forEach(liXMedias_MediaRecorder.value, (xclMedia) => xOnTakeXMedia_handling(xclMedia.payload!, isBusy.value, setState, item, xclMedia.mediaType!));
                                      await xOnTakeXMedia_INTERNAL(liMediaRAW, item)?.then((value) => Navigator.pop(context, true));
                                      item.showXMedia = true;
                                      item.isModified = true;
                                      if (item is K)
                                        widget.rootItemEdited != null ? widget.rootItemEdited!(item) : null;
                                      else
                                        widget.itemEdited != null ? widget.itemEdited!(item) : null;
                                    }
                                    liXMedias_MediaRecorder.value.clear();
                                    isTakingPhoto = false;
                                    setState(() => saving = false);
                                    await _photoController.dispose();
                                  },
                                  elevation: 0,
                                  icon_Widget: Icon(Icons.save, color: Colors.green, size: 30),
                                  label_Style: XStyles.xStyleText(colorText: Colors.green, fontSize: 24, activeBold: false),
                                ))
                          : Container()
                    ])));
          });
        });
      });
  return null;
}