12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flustars/flustars.dart' as flustars;
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:image_picker/image_picker.dart';
- import 'package:liftmanager/common/common.dart';
- import 'package:liftmanager/internal/bbs/page/jubao_page.dart';
- import 'package:liftmanager/net/api_service.dart';
- import 'package:liftmanager/res/iconfont.dart';
- import 'package:liftmanager/utils/fast_notification.dart';
- import 'package:liftmanager/utils/oss_upload.dart';
- import 'package:liftmanager/utils/toast.dart';
- import 'package:liftmanager/widgets/app_bar.dart';
- import 'package:liftmanager/widgets/chat_list_view.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:provider/provider.dart';
- // import 'package:shared_preferences/shared_preferences.dart';
- import './chat_detail/chat_content_view.dart';
- import '../model/conversation.dart';
- import '../provide/websocket.dart';
- import 'package:permission_handler/permission_handler.dart';
- import 'package:flutter_sound/flutter_sound.dart';
- import 'package:flutter_sound/public/flutter_sound_player.dart';
- import 'package:flutter_sound/public/flutter_sound_recorder.dart';
- class ChatDetailPage extends StatefulWidget {
- ChatDetailPage(
- {this.id,
- this.type,
- this.toUserId,
- this.jubaoToUserId,
- this.title = '聊天室'});
- final String id;
- final String type;
- final String toUserId;
- final String title;
- String jubaoToUserId;
- @override
- _ChatDetailPageState createState() => _ChatDetailPageState();
- // _ChatDetailPageState createState() => _ChatDetailPageState(type,index);
- }
- class _ChatDetailPageState extends State<ChatDetailPage> {
- bool hasText = false;
- bool isAutoJump = true;
- bool isDownKeyboard = true;
- int type = 0;
- int index = 1;
- var msgList = [];
- var msgListBack = [];
- var assList = [];
- bool showEmoji = false;
- bool hasMore = true;
- int _page = 1;
- int pageSize = 10;
- Conversation data;
- // _ChatDetailPageState(this.type0,this.index0);
- WebSocketProvide provider = WebSocketProvide();
- ScrollController _scrollController;
- var userList;
- var storyList = [];
- var storyListUserOnline = [];
- //避免从附近的人打招呼重复的initRoom
- bool isTrim = false;
- int dataTable;
- final controller = TextEditingController();
- bool voiceModeOn = false;
- bool voiceRecordingOn = false;
- FlutterSoundPlayer _player = FlutterSoundPlayer();
- FlutterSoundRecorder _recorder = FlutterSoundRecorder();
- bool _playerIsInited = false;
- bool _recorderIsInited = false;
- bool _playbackReady = true;
- String _audioFilePath;
- int recordingDuration = 0;
- Timer recordingTimer;
- AnimationController _animationController;
- void _handleSubmitted(context, String text) {
- // FocusScope.of(context).requestFocus(FocusNode());
- if (controller.text.length > 0) {
- // FocusScope.of(context).requestFocus(FocusNode());
- print('发送${text}');
- print(type);
- if (Provider.of<WebSocketProvide>(context, listen: false)
- .socketIsConnect) {
- controller.clear(); //清空输入框
- }
- Provider.of<WebSocketProvide>(context, listen: false).sendMessage(context,
- msg: text,
- id: widget.id,
- msgType: 1,
- dataTable: dataTable,
- toUserId: widget.toUserId);
- showEmoji = false;
- hasMore = true;
- _page = 1;
- setState(() {});
- // FocusScope.of(context).requestFocus(FocusNode());
- } else {
- toasts("请输入");
- }
- }
- void _jumpBottom() {
- _scrollController.animateTo(_scrollController.position.maxScrollExtent,
- curve: Curves.easeOut, duration: Duration(milliseconds: 200));
- }
- String emoji =
- "😀,😁,😂,😃,😄,😅,😆,😉,😊,😋,😎,😍,😘,😗,😙,😚,😇,😐,😑,😶,😏,😣,😥,😮,😯,😪,😫,😴,😌,😛,😜,😝,😒,😓,😔,😕,😲,😷,😖,😞,😟,😤,😢,😭,😦,😧,😨,😬,😰,😱,😳,😵,😡,😠";
- var emojiList = <String>[];
- void setWebSocket() async {
- // SharedPreferences prefs = await SharedPreferences.getInstance();
- new Future.delayed(Duration(milliseconds: 500), () {
- Provider.of<WebSocketProvide>(context, listen: false)
- .incomeRoom(widget.id);
- // getStringEvent(widget.id);
- });
- }
- void initJump() async {
- new Future.delayed(Duration(milliseconds: 500), () {
- FocusScope.of(context).requestFocus(FocusNode());
- _jumpBottom();
- });
- }
- void disposeJump() async {
- new Future.delayed(Duration(milliseconds: 500), () {
- Provider.of<WebSocketProvide>(context, listen: false).closeWebSocket();
- });
- }
- // void getMsgList() async {
- // new Future.delayed(Duration(milliseconds: 2000), () {
- // setState(() {
- // msgList = Provider.of<WebSocketProvide>(context, listen: false)
- // .historyMessageqqq;
- // print('getMsgList msgList ===== $msgList');
- // _jumpBottom();
- // });
- // });
- // }
- // 输入框的焦点实例
- FocusNode _focusNode;
- @override
- void initState() {
- _player.openAudioSession().then((value) {
- setState(() {
- _playerIsInited = true;
- });
- });
- openTheRecorder().then((value) {
- setState(() {
- _recorderIsInited = true;
- });
- });
- emojiList = emoji.split(',');
- if (widget.type == "nearToOne") {
- dataTable = 3;
- } else {
- dataTable = null;
- var roomIds = flustars.SpUtil.getStringList('showAlert');
- if (!roomIds.contains(widget.id)) {
- Fluttertoast.showToast(
- msg: '欢迎使用,请在使用过程中注意个人信息及财产安全',
- backgroundColor: Colors.amber,
- textColor: Colors.black,
- fontSize: 13,
- toastLength: Toast.LENGTH_LONG,
- gravity: ToastGravity.TOP,
- timeInSecForIosWeb: 3);
- } else {
- var newRoodIds = [widget.id];
- newRoodIds.addAll(roomIds);
- flustars.SpUtil.putStringList('showAlert', newRoodIds);
- }
- }
- print(widget.id);
- FastNotification.addListener("chat_room", (historyMessageqqq) {
- if (mounted) {
- setState(() {
- msgList = historyMessageqqq;
- assList = historyMessageqqq;
- print('chat_room msgList ===== $msgList');
- });
- _jumpBottom();
- }
- });
- FastNotification.addListener("chat_room_chat", (historyMessageqqq) {
- if (mounted) {
- setState(() {
- msgList = historyMessageqqq;
- assList = historyMessageqqq;
- print('chat_room_chat msgList ===== $msgList');
- });
- _jumpBottom();
- }
- });
- FastNotification.addListener("set_user", (setUserList) {
- if (mounted) {
- new Future.delayed(Duration(milliseconds: 1000), () {
- // setState(() {
- // userList = setUserList;
- // });
- });
- }
- });
- FastNotification.addListener("initSocket", (initThisSocket) {
- if (mounted) {
- isTrim = true;
- setWebSocket();
- _jumpBottom();
- FocusScope.of(context).requestFocus(FocusNode());
- toasts("聊天室已重连!");
- }
- });
- if (!Provider.of<WebSocketProvide>(context, listen: false)
- .socketIsConnect) {
- Provider.of<WebSocketProvide>(context, listen: false).createWebsocket(
- Provider.of<WebSocketProvide>(context, listen: false).roomId);
- }
- super.initState();
- _scrollController = new ScrollController();
- _scrollController.addListener(() {
- // FocusScope.of(context).requestFocus(FocusNode());
- // print(_controller.offset);
- });
- setWebSocket();
- _focusNode = FocusNode();
- _focusNode.addListener(() {
- // _jumpBottom();
- Timer(Duration(milliseconds: 300), () => _jumpBottom());
- if (!_focusNode.hasFocus && !isDownKeyboard) {
- FocusScope.of(context).requestFocus(_focusNode);
- }
- if (!_focusNode.hasFocus) {
- setState(() {
- showEmoji = false;
- });
- }
- isDownKeyboard = true;
- });
- }
- @override
- void dispose() {
- stopPlayer();
- _player.closeAudioSession();
- _player = null;
- stopRecorder();
- _recorder.closeAudioSession();
- _recorder = null;
- if (_audioFilePath != null) {
- var outputFile = File(_audioFilePath);
- if (outputFile.existsSync()) {
- outputFile.delete();
- }
- }
- super.dispose();
- }
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- }
- void getStringEvent(roomId) async {
- // SharedPreferences prefs = await SharedPreferences.getInstance();
- var storyList = [];
- var storyListUserOnline = [];
- // if (prefs.getStringList(roomId) != null) {
- // List<String> hisString = prefs.getStringList(roomId);
- // storyList = hisString.map((item) => jsonDecode(item)).toList();
- // } else {
- // prefs.setStringList(roomId, []);
- // }
- // if (flustars.SpUtil.getString(roomId + "userOnline") != null &&
- // flustars.SpUtil.getString(roomId + "userOnline") != "") {
- // // if(prefs.getString(roomId+"userOnline")!=null){
- // // String hisStringUserOnline = prefs.getString(roomId+"userOnline");
- // // flustars.SpUtil.putString(roomId+"userOnline", res.token);
- // String hisStringUserOnline =
- // flustars.SpUtil.getString(roomId + "userOnline");
- // for (var value in JsonDecoder().convert(hisStringUserOnline)) {
- // print(value);
- // storyListUserOnline.add(value);
- // }
- // ;
- // // storyListUserOnline = JsonDecoder().convert(hisStringUserOnline);
- // // storyListUserOnline = hisStringUserOnline.map((item)=>jsonDecode(item)).toList();
- // } else {
- // // prefs.setString(roomId+"userOnline",'');
- // }
- // print(JsonEncoder().convert(storyList));
- // print(JsonEncoder().convert(storyListUserOnline));
- new Future.delayed(Duration(milliseconds: 1000), () {
- setState(() {
- if (storyList.length > 0) {
- msgList = storyList;
- print('storyList msgList ===== $msgList');
- }
- if (storyListUserOnline.length > 0) {
- userList = storyListUserOnline;
- }
- print(JsonEncoder().convert(msgList));
- _jumpBottom();
- });
- });
- }
- // @override
- // void didChangeDependencies() {
- // // Provider.of<WebSocketProvide>(context).closeWebSocket();
- // Provider.of<WebSocketProvide>(context,listen: false).closeWebSocket();
- // super.didChangeDependencies();
- // }
- FocusNode blankNode = FocusNode();
- ///选择图片
- void selectPicker() {
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return SimpleDialog(
- title: Text("选择方式"),
- children: ["拍照", '从手机相册选择'].map((String value) {
- print("$value");
- return SimpleDialogOption(
- child: Text(
- "${value}",
- style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
- ),
- onPressed: () {
- _getImage(value == '拍照' ? 1 : 0);
- Navigator.of(context).pop();
- },
- );
- }).toList());
- });
- }
- var imagesList = <String>[];
- var imagesListLast = <String>[];
- void _getImage(int key) async {
- try {
- var _imageFile = await ImagePicker.pickImage(
- source: key == 1 ? ImageSource.camera : ImageSource.gallery,
- maxWidth: 800,
- imageQuality: 95);
- print(_imageFile);
- print(3333);
- if (_imageFile != null) {
- // images.add(_imageFile);
- upLoadFileOnce(_imageFile.path);
- // setState(() {});
- }
- } catch (e) {
- toasts("没有权限,无法打开相册!");
- }
- }
- upLoadFileOnce(path) {
- showLoading(context, "正在发送...");
- NewApiService().upload(path, onSuccess: (res) {
- // imagesUrl.add(res.path);
- dismissLoading(context);
- String imagesUrl;
- imagesUrl = (res.pathUrl);
- Provider.of<WebSocketProvide>(context, listen: false).sendMessage(context,
- msg: imagesUrl,
- id: widget.id,
- msgType: 2,
- dataTable: dataTable,
- toUserId: widget.toUserId);
- }, onError: (code, msg) {
- dismissLoading(context);
- toasts(msg);
- });
- }
- @override
- Widget build(BuildContext context) {
- msgList = processMsgList(msgList);
- imagesList = [];
- msgListBack = [];
- msgList.forEach((element) {
- msgListBack.insert(0, element);
- });
- // print(JsonEncoder().convert(msgListBack));
- print('msgList ======= $msgList');
- print('msgList count ======= ${msgList.length}');
- try {
- if (msgList.length > 0) {
- msgList.forEach((element) {
- if (element["type"] == 2) {
- imagesList.add(element["msg"]);
- }
- });
- }
- } catch (e) {
- print('msgList e ======= $e');
- }
- if (isAutoJump) {
- Timer(Duration(milliseconds: 500), () {
- _jumpBottom();
- });
- } else {
- isAutoJump = true;
- }
- return
- // ChangeNotifierProvider<WebSocketProvide>(
- // create: (_) => provider,
- // child:
- Scaffold(
- appBar: MyAppBar(
- centerTitle: widget.title,
- // actions: [
- // new FlatButton(
- // onPressed: () {
- // Navigator.of(context).push(MaterialPageRoute(
- // builder: (context) => JuBaoPage(
- // toUserId: widget.jubaoToUserId,
- // )));
- // },
- // child: Text("举报"),
- // ),
- // ],
- // onPressed:(){
- // NavigatorUtils.push(context, FriendsRouter.friendsList);
- // }
- isFun: true,
- fun: () {
- Navigator.popUntil(context, ModalRoute.withName('/home'));
- },
- ),
- body: SafeArea(
- child: Stack(
- alignment: Alignment.center,
- children: [
- GestureDetector(
- onTap: () {
- // 点击空白页面关闭键盘
- // FocusScope.of(context).requestFocus(_focusNode);
- FocusScope.of(context).requestFocus(FocusNode());
- setState(() {
- showEmoji = false;
- });
- },
- child: Container(
- padding: EdgeInsets.only(top: 10),
- color: Color(0xffF6F6F6),
- child: Column(
- children: <Widget>[
- Expanded(
- child: ChatListView(
- key: Key('chat_list'),
- scrollController: _scrollController,
- onRefresh: _loadMore,
- pageSize: 10,
- itemCount: msgList.length,
- hasMore: hasMore,
- itemBuilder: (_, index) {
- int typeUser;
- try {
- if ('${msgList[index]["fromUser"]}' ==
- flustars.SpUtil.getString(Constant.userId)) {
- typeUser = 1;
- } else {
- typeUser = 0;
- }
- return ChatContentView(
- contentIndex: index,
- urlList: imagesList,
- type: typeUser,
- text: msgList[index]["msg"],
- msgType: msgList[index]["type"],
- avatar: msgList[index]["avatarUrl"],
- username: msgList[index]["name"],
- remarks: msgList[index]["remarks"],
- isNetwork: msgList[index]["avatarUrl"] != null
- ? true
- : false,
- recordingDuration: msgList[index]["dura"],
- onTap: () async {
- if (_player.isPlaying) {
- await stopPlayer();
- }
- print('play ===== ${msgList[index]['msg']}');
- play(msgList[index]['msg'], () {
- print('stopped ===== ');
- });
- });
- } catch (e) {
- print('e ==== $e');
- }
- return null;
- },
- ),
- ),
- showEmoji
- ? Container(
- padding: EdgeInsets.only(
- left: ScreenUtil().setWidth(6),
- top: ScreenUtil().setWidth(10),
- bottom: ScreenUtil().setWidth(10),
- right: ScreenUtil().setWidth(6)),
- color: Colors.white,
- height: ScreenUtil().setWidth(200),
- child: GridView.extent(
- //横轴的最大长度
- maxCrossAxisExtent: 35,
- //内边距
- padding: EdgeInsets.all(4.0),
- //垂直方向的间距
- mainAxisSpacing: 4.0,
- //水平方向的间距
- crossAxisSpacing: 4.0,
- children: emojiList.map((element) {
- return GestureDetector(
- onTap: () {
- controller.text += element;
- },
- child: Text(
- element,
- style: TextStyle(
- color: Color(0xff000000),
- fontSize: ScreenUtil().setSp(22)),
- textAlign: TextAlign.center,
- ),
- );
- }).toList(),
- ))
- : Container(
- child: null,
- ),
- Container(
- padding: EdgeInsets.only(
- top: ScreenUtil().setHeight(2.0),
- bottom: ScreenUtil().setHeight(2.0),
- left: 0,
- right: 0),
- color: Color(0xffFBFBFB),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- IconButton(
- icon: voiceModeOn
- ? Icon(
- Iconfont.b040jianpan,
- size: 21,
- )
- : Icon(
- Iconfont.yuyin2,
- size: 21,
- ),
- onPressed: () {
- setState(() {
- voiceModeOn = !voiceModeOn;
- });
- }),
- if (voiceModeOn)
- Expanded(
- child: GestureDetector(
- onLongPressStart: (details) {
- setState(() {
- voiceRecordingOn = true;
- });
- if (_player.isPlaying) {
- stopPlayer();
- }
- record();
- startCountingRecording();
- },
- onLongPressEnd: (details) async {
- setState(() {
- voiceRecordingOn = false;
- });
- // print('localPosition ===== ${details.localPosition}');
- if (recordingTimer != null) {
- recordingTimer.cancel();
- }
- await stopRecorder();
- if (details.localPosition.dy > -50) {
- await uploadAudioFile(_audioFilePath);
- } else {
- toasts("已取消");
- }
- },
- child: Text(
- voiceRecordingOn ? '松开结束' : '按住说话',
- textAlign: TextAlign.center,
- ),
- ),
- ),
- if (!voiceModeOn)
- Expanded(
- child: Container(
- // height: 40,
- alignment: Alignment.center,
- decoration: BoxDecoration(
- borderRadius:
- BorderRadius.all(Radius.circular(5.0)),
- color: Colors.white),
- child: TextFormField(
- controller: controller,
- // decoration: InputDecoration.collapsed(hintText: null),
- decoration: InputDecoration(
- border: InputBorder.none,
- ),
- maxLines: 5,
- minLines: 1,
- autocorrect: true,
- autofocus: true,
- focusNode: _focusNode,
- textAlign: TextAlign.start,
- textInputAction: TextInputAction.send,
- style: TextStyle(color: Colors.black),
- cursorColor: Colors.green,
- onFieldSubmitted: (term) {
- isDownKeyboard = false;
- // 这里进行事件处理
- // print('点击点击点击');
- if (controller.text == null ||
- controller.text == '') {
- toasts("请输入内容");
- } else {
- _handleSubmitted(context, controller.text);
- }
- },
- onChanged: (text) {
- setState(() {
- hasText = text.length > 0 ? true : false;
- });
- // print('change=================== $text');
- },
- // onSubmitted:_handleSubmitted,
- enabled: true, //bu禁用
- ),
- )),
- Container(
- width: ScreenUtil().setWidth(30.0),
- child: IconButton(
- icon: Icon(
- Iconfont.biaoqing,
- size: 21,
- ),
- //发送按钮图标
- onPressed: () {
- print('打开表情面板');
- setState(() {
- showEmoji = !showEmoji;
- voiceModeOn = false;
- });
- }),
- ),
- Container(
- width: ScreenUtil().setWidth(30.0),
- margin: EdgeInsets.only(
- right: ScreenUtil().setWidth(10.0)),
- child: IconButton(
- icon: Icon(Iconfont.tianjia, size: 24),
- onPressed: () {
- selectPicker();
- FocusScope.of(context)
- .requestFocus(FocusNode());
- }),
- ),
- Container(
- width: ScreenUtil().setWidth(30.0),
- margin: EdgeInsets.only(
- right: ScreenUtil().setWidth(10.0)),
- child: IconButton(
- //发送按钮或者+按钮
- icon: Icon(Iconfont.fasong,
- size: 18, color: Color(0xff3978F7)),
- onPressed: () {
- // isDownKeyboard = false;
- if (controller.text == null ||
- controller.text == '') {
- toasts("请输入内容");
- } else {
- _handleSubmitted(context, controller.text);
- }
- }),
- )
- ],
- ),
- )
- ],
- ),
- ),
- ),
- if (voiceRecordingOn)
- Container(
- width: 140,
- height: 140,
- decoration: BoxDecoration(
- color: Color.fromRGBO(0, 0, 0, 0.5),
- borderRadius: BorderRadius.all(Radius.circular(8)),
- ),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Icon(
- Iconfont.yuyin,
- size: 40,
- color: Colors.white,
- ),
- SignalBars(),
- ],
- ),
- Text(
- '手指上划,取消发送',
- style: TextStyle(color: Colors.white, fontSize: 12),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- // ),
- );
- }
- processMsgList(var list) {
- return list.map((e) {
- if (e is String) {
- return JsonDecoder().convert(e);
- }
- return e;
- }).toList();
- }
- Future _loadMore() async {
- isAutoJump = false;
- if (hasMore) {
- //pge++;
- await NewApiService().getMsgHistoryList({
- "pageNum": _page,
- "pageSize": 10,
- // "pageSize":pageSize,
- "sessionid": widget.id,
- }, onSuccess: (res) {
- _page++;
- print(JsonEncoder().convert(res));
- if (res != null && res.length > 0) {
- res = processMsgList(res);
- print("初始化数据");
- print('res ==== $res');
- var ass = [];
- ass.addAll(res);
- if (assList.length == 0) {
- ass.addAll(msgList);
- assList = ass;
- } else {
- ass.addAll(assList);
- }
- setState(() {
- msgList = ass;
- print('_loadMore msgList ===== $msgList');
- hasMore = true;
- // _jumpBottom();
- });
- } else {
- setState(() {
- hasMore = false;
- });
- }
- }, onError: (code, msg) {
- toasts(msg);
- });
- } else {
- toasts("无更多历史记录!");
- }
- print("加载更多了");
- }
- Future<void> openTheRecorder() async {
- final permissions = await PermissionHandler()
- .requestPermissions([PermissionGroup.microphone]);
- if (permissions[PermissionGroup.microphone] != PermissionStatus.granted) {
- toasts('需要麦克风权限!');
- }
- var tempDir = await getTemporaryDirectory();
- _audioFilePath = '${tempDir.path}/audio_message.aac';
- var outputFile = File(_audioFilePath);
- if (outputFile.existsSync()) {
- await outputFile.delete();
- }
- await _recorder.openAudioSession();
- _recorderIsInited = true;
- }
- Future<void> record() async {
- assert(_recorderIsInited && _player.isStopped);
- await _recorder.startRecorder(
- toFile: _audioFilePath,
- codec: Codec.aacADTS,
- );
- setState(() {});
- }
- Future<void> stopRecorder() async {
- await _recorder.stopRecorder();
- _playbackReady = true;
- }
- void play(String fileUrl, Function onFinished) async {
- assert(_playerIsInited &&
- _playbackReady &&
- _recorder.isStopped &&
- _player.isStopped);
- await _player.startPlayer(
- fromURI: fileUrl,
- codec: Codec.aacADTS,
- whenFinished: () {
- setState(() {});
- if (onFinished != null) {
- onFinished();
- }
- });
- setState(() {});
- }
- Future<void> stopPlayer() async {
- await _player.stopPlayer();
- }
- uploadAudioFile(String filePath) async {
- String uploadName = OssUtil.instance.getImageUploadName(filePath);
- await NewApiService.uploadImage(context, uploadName, filePath).then((data) {
- if (data.statusCode == 200) {
- String str = NewApiUrl.URL_UPLOAD_IMAGE_OSS + "/" + uploadName;
- print("str:" + str);
- if (str != null) {
- Map obj = {"uploadName": uploadName, "success": true};
- FastNotification.push("percent", obj);
- }
- Provider.of<WebSocketProvide>(context, listen: false).sendMessage(
- context,
- msg: str,
- id: widget.id,
- msgType: 4,
- dataTable: dataTable,
- toUserId: widget.toUserId,
- recordingDuration: recordingDuration);
- } else {
- Map obj = {"uploadName": uploadName, "success": false};
- FastNotification.push("percent", obj);
- }
- }).catchError((data) {
- Map obj = {"uploadName": uploadName, "success": false};
- FastNotification.push("percent", obj);
- });
- }
- void startCountingRecording() {
- recordingDuration = 0;
- if (recordingTimer != null) {
- recordingTimer.cancel();
- }
- recordingTimer = Timer.periodic(Duration(seconds: 1), (timer) async {
- if (recordingDuration >= 30) {
- recordingTimer.cancel();
- await stopRecorder();
- // await uploadAudioFile(_audioFilePath);
- setState(() {
- voiceRecordingOn = false;
- });
- } else {
- recordingDuration++;
- }
- });
- }
- }
- class SignalBars extends StatefulWidget {
- @override
- State<SignalBars> createState() => SignalBarsState();
- }
- class SignalBarsState extends State<SignalBars> {
- double bar1Opactity = 0;
- double bar2Opactity = 0;
- double bar3Opactity = 0;
- int loopCount = 0;
- Timer _timer;
- @override
- void initState() {
- _timer = Timer.periodic(Duration(milliseconds: 400), (timer) {
- setState(() {
- if (loopCount == 1) {
- bar1Opactity = 1;
- } else if (loopCount == 2) {
- bar2Opactity = 1;
- } else if (loopCount == 3) {
- bar3Opactity = 1;
- } else {
- bar1Opactity = 0;
- bar2Opactity = 0;
- bar3Opactity = 0;
- loopCount = 0;
- }
- loopCount++;
- });
- });
- super.initState();
- }
- @override
- void dispose() {
- if (_timer != null) {
- _timer.cancel();
- }
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- height: 35,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Container(
- height: 4,
- width: 18,
- color: Color.fromRGBO(255, 255, 255, bar3Opactity)),
- Container(
- height: 4,
- width: 14,
- color: Color.fromRGBO(255, 255, 255, bar2Opactity)),
- Container(
- height: 4,
- width: 10,
- color: Color.fromRGBO(255, 255, 255, bar1Opactity)),
- Container(
- height: 4, width: 6, color: Color.fromRGBO(255, 255, 255, 1)),
- ],
- ),
- );
- }
- }
|