长按时 GestureDetector 和滑动

问题描述

enter image description here

我想滑动用容器包裹的麦克风图标,我在长按时将此容器添加到 GestureDetector Widget 中,容器可以滑动删除记录,这意味着返回 isRecording = false,但所有关于 UI 的问题,在代码下面我试试

 GestureDetector(
    onLongPress: () async {
      await pressstart();
    },onLongPressUp: () async {
      await pressUp();

      Future.delayed(Duration(milliseconds: 100),() {
        setState(() {
          _onFileUploadButtonpressed();
          _onRecordAgainButtonpressed();
        });
      });
    },child: _isRecording != true
        ? Container(
            height: 55,width: 53,child: Center(
              child: Icon(
                Icons.mic,size: 30,color: Colors.white,),decoration: Boxdecoration(
                BoxShadow: [
                  BoxShadow(
                      offset: Offset(0,10),blurRadius: 10,color: Colors.grey)
                ],color: Colors.black,shape: BoxShape.circle),)
        : Container(
            height: 90,width: 90,child: Center(
              child: Container(
                decoration: Boxdecoration(
                    BoxShadow: [
                      BoxShadow(
                          offset: Offset(0,blurRadius: 100,color: Colors.grey)
                    ],color: Colors.red[300],child: Icon(
                  Icons.pause_circle_filled,size: 60,color: Colors.red,)),

在下面,有我用来录制语音上传到 firestore 和 firebase 存储的功能

Future<void> pressstart() async {
//anlasildi
_isRecorded = false;
_isRecording = true;
 before = DateTime.Now();
 await _startRecording();
 }

  Future<void> pressUp() async {
//anlasildi
 _audioRecorder!.stop().then((value) => after = DateTime.Now());
  setState(() {
  _isRecording = false;
  _isRecorded = true;
  Fluttertoast.showToast(msg: 'Yükleniyor..');
  });
  }

 Future<void> _startRecording() async {
//anlasildi
    final bool? hasRecordingPermission =
    await FlutterAudioRecorder2.hasPermissions;
     if (hasRecordingPermission!) {
    Directory directory = await getApplicationDocumentsDirectory();
    String filepath = directory.path +
      '/' +
      DateTime.Now().millisecondsSinceEpoch.toString() +
      '.aac';
  _audioRecorder =
      FlutterAudioRecorder2(filepath,audioFormat: AudioFormat.AAC);
  await _audioRecorder!.initialized;
  _audioRecorder!.start();
  _filePath = filepath;
  setState(() {});
  } else {
  SnackBar snackBar = SnackBar(
    content: Text('Please enable recording permission'),);
  ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }
}

 

     Future<void> _onFileUploadButtonpressed() async {
setState(() {
  _isuploading = true;
});
UploadTask reference = firebaseStorage
    .ref('upload-voice-firebase')
    .child(_filePath!
        .substring(_filePath!.lastIndexOf('/'),_filePath!.length))
    .putFile(File(_filePath!));
try {
  TaskSnapshot snapshot = await reference;
  voiceUrl = await snapshot.ref.getDownloadURL();
  Duration durations = after!.difference(before!);
  String minuteString =
      '${(durations.inSeconds / 60).floor() < 10 ? 0 : ''}${(durations.inSeconds / 60).floor()}';
  String secondString =
      '${durations.inSeconds % 60 < 10 ? 0 : ''}${durations.inSeconds % 60}';
  String lasttime = '$minuteString:$secondString';
  setState(() {
    onSendMessage(voiceUrl,2,lasttime);
  });
} catch (error) {
  print('Error occured while uplaoding to Firebase ${error.toString()}');
  SnackBar snackBar = SnackBar(
    content: Text('Error occured while uplaoding'),);
  ScaffoldMessenger.of(context).showSnackBar(snackBar);
} finally {
  setState(() {
    _isuploading = false;
  });
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)