Hibok
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

778 rader
28 KiB

  1. import 'dart:async';
  2. import 'dart:ui';
  3. import 'package:agora_rtc_engine/agora_rtc_engine.dart';
  4. import 'package:cached_network_image/cached_network_image.dart';
  5. import 'package:chat/chat/gift_page.dart';
  6. import 'package:chat/data/UserData.dart';
  7. import 'package:chat/data/WebData.dart';
  8. import 'package:chat/data/constants.dart';
  9. import 'package:chat/generated/i18n.dart';
  10. import 'package:chat/models/UserInfo.dart';
  11. import 'package:chat/models/gift_item_model.dart';
  12. import 'package:chat/models/gift_select_provider.dart';
  13. import 'package:chat/models/money_change.dart';
  14. import 'package:chat/models/ref_name_provider.dart';
  15. import 'package:chat/r.dart';
  16. import 'package:chat/utils/ChargeMoney.dart';
  17. import 'package:chat/utils/CustomUI.dart';
  18. import 'package:chat/utils/HttpUtil.dart';
  19. import 'package:chat/utils/MessageMgr.dart';
  20. import 'package:chat/utils/TokenMgr.dart';
  21. import 'package:chat/utils/anim_effect_overlay.dart';
  22. import 'package:chat/utils/counter_overlay.dart';
  23. import 'package:chat/utils/msgHandler.dart';
  24. import 'package:chat/utils/screen.dart';
  25. import 'package:chat/utils/sound_util.dart';
  26. import 'package:chat/utils/sp_utils.dart';
  27. import 'package:dio/dio.dart';
  28. import 'package:flutter/material.dart';
  29. import 'package:oktoast/oktoast.dart';
  30. import 'package:provider/provider.dart';
  31. class AudioChatPage extends StatefulWidget {
  32. final UserInfo userInfo;
  33. final bool isReplay;
  34. AudioChatPage({this.userInfo, this.isReplay = false});
  35. @override
  36. _AudioChatPageState createState() => _AudioChatPageState();
  37. }
  38. class _AudioChatPageState extends State<AudioChatPage> {
  39. //是否启用扬声器
  40. bool speakPhone = false;
  41. int friendId;
  42. bool isReply; //是否应答模式
  43. bool isChating = false; //是否通话中
  44. //超时挂断
  45. Timer offTimer;
  46. Timer callingTimer;
  47. String channelName;
  48. //打赏礼物信息
  49. List<GiftItemModel> giftList;
  50. final _controller = new PageController();
  51. //礼物数量
  52. int curGiftValue = 1;
  53. bool isQuit = false;
  54. @override
  55. void initState() {
  56. super.initState();
  57. friendId = widget.userInfo.userId;
  58. isReply = widget.isReplay;
  59. MsgHandler.isAudioConnect = true;
  60. getGiftList();
  61. initAgoreSdk();
  62. getDefaultSetting();
  63. //事件监听回调
  64. setAgoreEventListener();
  65. //礼物打赏
  66. MessageMgr().on('Receive Gift', receiveGift);
  67. MessageMgr().on('AudioChat Failed', closeChat);
  68. MessageMgr().on('AudioChat State', refuseAnswer);
  69. }
  70. void getDefaultSetting() async {
  71. bool soundPlayMode =
  72. (await SPUtils.getBool(Constants.SOUND_PLAY_MODE)) ?? false;
  73. setState(() {
  74. speakPhone = soundPlayMode;
  75. });
  76. AgoraRtcEngine.setEnableSpeakerphone(speakPhone);
  77. }
  78. closeChat(args) {
  79. showToast(I18n.of(context).not_online);
  80. _onExit(context);
  81. }
  82. @override
  83. void didChangeDependencies() {
  84. super.didChangeDependencies();
  85. print('test didChangeDependencies');
  86. }
  87. refuseAnswer(args) {
  88. var fdId = args['fdId'];
  89. var isAnswer = args['isAnswer'];
  90. if (fdId == friendId && !isAnswer) {
  91. // showToast('对方暂时无法接听');
  92. _onExit(context);
  93. }
  94. }
  95. receiveGift(gift) {
  96. int giftId = gift.giftId;
  97. int amount = gift.giftAmount;
  98. int total = gift.money;
  99. print(DateTime.now());
  100. var giftModel = giftList[giftId - 1];
  101. print('收到礼物金额:$total');
  102. UserData().incomeMoney += total;
  103. //Provider.of<MoneyChangeProvider>(context, listen: false).addMoney(total);
  104. AnimEffect.showEffect(context, giftId);
  105. AnimEffect.showDashangEffect(context, false, amount, giftModel.name);
  106. }
  107. //本页面即将销毁
  108. @override
  109. void dispose() {
  110. _controller.dispose();
  111. MessageMgr().off('AudioChat State', refuseAnswer);
  112. MessageMgr().off('AudioChat Failed', closeChat);
  113. MessageMgr().off('Receive Gift', receiveGift);
  114. MsgHandler.isAudioConnect = false;
  115. offTimer?.cancel();
  116. callingTimer?.cancel();
  117. SoundUtils().stop();
  118. AgoraRtcEngine.leaveChannel();
  119. //sdk资源释放
  120. AgoraRtcEngine.destroy();
  121. super.dispose();
  122. }
  123. void initAgoreSdk() {
  124. //初始化引擎
  125. AgoraRtcEngine.create(Constants.Agore_appId);
  126. //设置视频为可用 启用音频模块
  127. AgoraRtcEngine.enableAudio();
  128. //每次需要原生视频都要调用_createRendererView
  129. if (!isReply) {
  130. int myId = UserData().basicInfo.userId;
  131. _createRendererView(myId);
  132. }
  133. }
  134. void getGiftList() async {
  135. Map data = {
  136. "userId": UserData().basicInfo.userId,
  137. };
  138. data['sign'] = TokenMgr().getSign(data);
  139. Response res = await HttpUtil().post('prop/get/list', data: data);
  140. if (res == null) {
  141. return;
  142. }
  143. print(res);
  144. Map resData = res.data;
  145. if (resData['code'] == 0) {
  146. //领取成功
  147. giftList = resData['data']
  148. .map<GiftItemModel>((v) => GiftItemModel.fromJson(v))
  149. .toList();
  150. print('giftList length : ${giftList.length}');
  151. }
  152. }
  153. @override
  154. Widget build(BuildContext context) {
  155. return Scaffold(
  156. body: SafeArea(
  157. child: Stack(
  158. children: <Widget>[
  159. ConstrainedBox(
  160. constraints: BoxConstraints.expand(),
  161. child: CachedNetworkImage(
  162. imageUrl: widget.userInfo.headimgurl,
  163. fit: BoxFit.fill,
  164. placeholder: CustomUI.buildImgLoding)),
  165. BackdropFilter(
  166. filter: new ImageFilter.blur(
  167. sigmaX: Screen.width / 8, sigmaY: Screen.width / 8),
  168. child: Container(
  169. color: Colors.black.withOpacity(0.5),
  170. ),
  171. ),
  172. SafeArea(
  173. child: Padding(
  174. padding: EdgeInsets.symmetric(vertical: 40),
  175. child: Column(
  176. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  177. children: <Widget>[
  178. _viewAudio(),
  179. isReply ? _replayToolBar() : _bottomToolBar(),
  180. ],
  181. )))
  182. ],
  183. )));
  184. }
  185. _viewAudio() {
  186. var url = widget.userInfo.headimgurl;
  187. print('url : $url');
  188. print(url.length);
  189. var userName = Provider.of<RefNameProvider>(context)
  190. .getRefName(widget.userInfo.userId, widget.userInfo.nickName);
  191. var local = WebData().getCity(widget.userInfo.city);
  192. var profession = WebData().getProffesionName(widget.userInfo.occupation);
  193. var birth = widget.userInfo.birthday;
  194. print('birth : $birth');
  195. var birthDay = DateTime.parse(birth);
  196. print(birthDay);
  197. var today = DateTime.now();
  198. var testAge = today.difference(birthDay);
  199. print(testAge.inDays);
  200. var age = testAge.inDays ~/ 365;
  201. var connectTip;
  202. if (isChating) {
  203. connectTip = I18n.of(context).chatting;
  204. } else {
  205. if (isReply) {
  206. connectTip = I18n.of(context).voice_msg.replaceFirst('/s1', userName);
  207. } else {
  208. connectTip = I18n.of(context).waitting_answer;
  209. }
  210. }
  211. return Container(
  212. child: Column(
  213. children: <Widget>[
  214. ClipRRect(
  215. borderRadius: BorderRadius.circular(0),
  216. child: url.length == 0
  217. ? SizedBox(
  218. height: 100,
  219. width: 100,
  220. child: Image.asset(R.assetsImagesDefaultNorAvatar))
  221. : CachedNetworkImage(
  222. width: 100,
  223. height: 100,
  224. imageUrl: url,
  225. placeholder: (context, url) => Padding(
  226. padding: EdgeInsets.all(5),
  227. child: CircularProgressIndicator(
  228. valueColor:
  229. AlwaysStoppedAnimation(Constants.BlueTextColor)),
  230. ),
  231. fit: BoxFit.cover,
  232. ),
  233. ),
  234. SizedBox(height: 5),
  235. Row(
  236. mainAxisAlignment: MainAxisAlignment.center,
  237. children: <Widget>[
  238. Container(
  239. width: Screen.width - 50,
  240. child: Text(
  241. userName,
  242. style: TextStyle(fontSize: 20, color: Colors.white70),
  243. textScaleFactor: 1.0,
  244. textAlign: TextAlign.center,
  245. )),
  246. fixedText('$age', fontSize: 20, color: Colors.white70)
  247. ],
  248. ),
  249. SizedBox(height: 5),
  250. Text(
  251. '$local',
  252. style: TextStyle(fontSize: 16, color: Colors.white70),
  253. textScaleFactor: 1.0,
  254. textAlign: TextAlign.center,
  255. ),
  256. SizedBox(height: 5),
  257. Text(
  258. '$profession',
  259. style: TextStyle(fontSize: 16, color: Colors.white70),
  260. textScaleFactor: 1.0,
  261. textAlign: TextAlign.center,
  262. ),
  263. SizedBox(height: 20),
  264. Container(
  265. width: Screen.width - 50,
  266. child: Text(
  267. connectTip,
  268. //maxLines: 2,
  269. style: TextStyle(fontSize: 20, color: Colors.white70),
  270. textScaleFactor: 1.0,
  271. textAlign: TextAlign.center,
  272. )),
  273. ],
  274. ),
  275. );
  276. }
  277. _replayToolBar() {
  278. return Container(
  279. child: Row(
  280. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  281. children: <Widget>[
  282. //接听按钮
  283. RawMaterialButton(
  284. onPressed: () {
  285. MsgHandler.stopAudioRing();
  286. int myId = UserData().basicInfo.userId;
  287. MsgHandler.sendReplyAudioChatReq(friendId, true);
  288. _createRendererView(myId);
  289. },
  290. child: Icon(
  291. Icons.call,
  292. color: Colors.white,
  293. size: 35.0,
  294. ),
  295. shape: CircleBorder(),
  296. elevation: 2.0,
  297. fillColor: Colors.greenAccent,
  298. padding: const EdgeInsets.all(15.0),
  299. ),
  300. //挂断按钮
  301. RawMaterialButton(
  302. onPressed: () {
  303. _onExit(context);
  304. MsgHandler.stopAudioRing();
  305. MsgHandler.sendReplyAudioChatReq(friendId, false);
  306. },
  307. child: Icon(
  308. Icons.call_end,
  309. color: Colors.white,
  310. size: 35.0,
  311. ),
  312. shape: CircleBorder(),
  313. elevation: 2.0,
  314. fillColor: Colors.redAccent,
  315. padding: const EdgeInsets.all(15.0),
  316. )
  317. ]));
  318. }
  319. _bottomToolBar() {
  320. List<Widget> showWidgets = [
  321. Text(I18n.of(context).voicing,
  322. style: TextStyle(fontSize: 11, color: Colors.white24),
  323. textAlign: TextAlign.center),
  324. SizedBox(height: 10),
  325. Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[
  326. Offstage(
  327. offstage: !isChating,
  328. child: UserData().giftSwitch > 0
  329. ? RawMaterialButton(
  330. onPressed: _showGiftSheet,
  331. child: Icon(
  332. IconData(0xe64e, fontFamily: Constants.IconFontFamily),
  333. color: Colors.blueAccent,
  334. size: 20.0,
  335. ),
  336. shape: CircleBorder(),
  337. elevation: 2.0,
  338. fillColor: Colors.white,
  339. padding: const EdgeInsets.all(12.0),
  340. )
  341. : SizedBox(width: 36, height: 36)),
  342. //挂断按钮
  343. RawMaterialButton(
  344. onPressed: () {
  345. MsgHandler.sendReplyAudioChatReq(friendId, false);
  346. _onExit(context);
  347. },
  348. child: Icon(
  349. Icons.call_end,
  350. color: Colors.white,
  351. size: 35.0,
  352. ),
  353. shape: CircleBorder(),
  354. elevation: 2.0,
  355. fillColor: Colors.redAccent,
  356. padding: const EdgeInsets.all(15.0),
  357. ),
  358. //是否外放
  359. Offstage(
  360. offstage: !isChating,
  361. child: RawMaterialButton(
  362. onPressed: _isSpeakPhone,
  363. child: new Icon(
  364. speakPhone ? Icons.volume_up : Icons.volume_down,
  365. color: Colors.blueAccent,
  366. size: 20.0,
  367. ),
  368. shape: new CircleBorder(),
  369. elevation: 2.0,
  370. fillColor: Colors.white,
  371. padding: const EdgeInsets.all(12.0),
  372. ))
  373. ])
  374. ];
  375. if (isChating) {
  376. showWidgets.insert(
  377. 0,
  378. Container(
  379. alignment: Alignment.center,
  380. height: 80,
  381. width: 200,
  382. child: CounterOverlay(),
  383. ));
  384. }
  385. return Container(
  386. child: Column(children: showWidgets),
  387. );
  388. }
  389. //创建渲染视图
  390. void _createRendererView(int uId) {
  391. //增加音频会话对象 为了音频布局需要(通过uid和容器信息)
  392. //加入频道 第一个参数是 token 第二个是频道id 第三个参数 频道信息 一般为空 第四个 用户id
  393. int myId = UserData().basicInfo.userId;
  394. setState(() {
  395. print('加入聊天房间');
  396. var channelName = UserData().getSessionId(friendId).toString();
  397. AgoraRtcEngine.joinChannel(null, channelName, null, myId);
  398. });
  399. }
  400. //设置事件监听
  401. void setAgoreEventListener() {
  402. //成功加入房间
  403. AgoraRtcEngine.onJoinChannelSuccess =
  404. (String channel, int uid, int elapsed) {
  405. print("加入成功id为:$uid elapsed:$elapsed");
  406. if (isReply) {
  407. //回应
  408. setState(() {
  409. isReply = false;
  410. isChating = true;
  411. });
  412. } else {
  413. MsgHandler.sendAudioChatReq(widget.userInfo.userId);
  414. callingTimer = Timer.periodic(Duration(milliseconds: 2200), (timer) {
  415. SoundUtils().play(
  416. 'http://testcyhd.chengyouhd.com/Upload/Audio/even_wheat_sound.mp3',
  417. isLocal: false);
  418. });
  419. //30没人接就自动挂断
  420. offTimer = Timer(Duration(seconds: 30), () {
  421. MsgHandler.sendReplyAudioChatReq(friendId, false);
  422. _onExit(context);
  423. });
  424. }
  425. };
  426. //监听是否有新用户加入
  427. AgoraRtcEngine.onUserJoined = (int uid, int elapsed) {
  428. print("新用户所加入的id为:$uid elapsed:$elapsed");
  429. setState(() {
  430. //更新UI布局
  431. SoundUtils().stop();
  432. callingTimer?.cancel();
  433. isChating = true;
  434. offTimer?.cancel();
  435. });
  436. };
  437. //监听用户是否离开这个房间
  438. AgoraRtcEngine.onUserOffline = (int uid, int reason) {
  439. print("用户离开的id为:$uid");
  440. _onExit(context);
  441. };
  442. AgoraRtcEngine.onError = (dynamic errCode) {
  443. print('通话异常');
  444. // _onExit(context);
  445. };
  446. //监听用户是否离开这个频道
  447. AgoraRtcEngine.onLeaveChannel = () {
  448. print("用户离开");
  449. };
  450. }
  451. void _showGiftSheet() {
  452. showModalBottomSheet(
  453. context: context,
  454. elevation: 2.0,
  455. shape: RoundedRectangleBorder(
  456. borderRadius: BorderRadius.only(
  457. topLeft: Radius.circular(20), topRight: Radius.circular(20))),
  458. backgroundColor: Colors.transparent,
  459. builder: (BuildContext context) {
  460. return StatefulBuilder(
  461. builder: (BuildContext context, setBottomSheetState) {
  462. return Container(
  463. height: 400,
  464. width: Screen.width,
  465. decoration: BoxDecoration(
  466. color: Colors.white,
  467. ),
  468. child: giftList == null
  469. ? Center(child: Text(I18n.of(context).no_gift))
  470. : Column(
  471. children: <Widget>[
  472. Container(
  473. // decoration: BoxDecoration(
  474. // color: Colors.orangeAccent,
  475. // borderRadius: borderRadius
  476. // ),
  477. padding: EdgeInsets.symmetric(
  478. vertical: 10, horizontal: 20),
  479. child: Row(
  480. children: <Widget>[
  481. Text(I18n.of(context).sent_gift,
  482. textScaleFactor: 1.0,
  483. style: TextStyle(
  484. fontSize: 16,
  485. fontWeight: FontWeight.bold,
  486. color: Constants.BlackTextColor)),
  487. Expanded(
  488. child: SizedBox(),
  489. ),
  490. Image.asset(
  491. R.assetsImagesCoin,
  492. scale: 2,
  493. ),
  494. SizedBox(
  495. width: 5,
  496. ),
  497. fixedText(
  498. I18n.of(context)
  499. .available_balance
  500. .replaceFirst('/s1', ''),
  501. color: Constants.GreyTextColor),
  502. Consumer<MoneyChangeProvider>(
  503. builder: (context,
  504. MoneyChangeProvider counter,
  505. child) =>
  506. fixedText(counter.money.toString(),
  507. color: Color(0xFFDB305D)),
  508. ),
  509. fixedText(I18n.of(context).mask_coin,
  510. color: Color(0xFFDB305D)),
  511. ],
  512. ),
  513. ),
  514. Container(
  515. alignment: Alignment.topCenter,
  516. padding: EdgeInsets.all(10),
  517. height: 230,
  518. child: GiftPage(giftList, 0),
  519. ),
  520. Expanded(child: SizedBox()),
  521. Padding(
  522. padding: EdgeInsets.symmetric(
  523. vertical: 5, horizontal: 20),
  524. child: Row(
  525. children: <Widget>[
  526. Expanded(
  527. child: SizedBox(
  528. height: 36,
  529. child: RaisedButton(
  530. padding:
  531. EdgeInsets.symmetric(horizontal: 5),
  532. child: Text(
  533. I18n.of(context).recharge,
  534. textScaleFactor: 1.0,
  535. maxLines: 1,
  536. style: TextStyle(
  537. fontSize: 16,
  538. fontWeight: FontWeight.bold),
  539. ),
  540. elevation: 2.0,
  541. color: Colors.orangeAccent,
  542. textColor: Colors.white,
  543. onPressed: () {
  544. ChargeMoney.showChargeSheet(context,
  545. () {
  546. setState(() {});
  547. });
  548. },
  549. shape: RoundedRectangleBorder(
  550. borderRadius: BorderRadius.all(
  551. Radius.circular(20)),
  552. ),
  553. ),
  554. )),
  555. SizedBox(width: 20),
  556. Expanded(
  557. flex: 2,
  558. child: Container(
  559. height: 36,
  560. decoration: BoxDecoration(
  561. boxShadow: [
  562. BoxShadow(
  563. blurRadius: 2.0,
  564. color: Colors.white24)
  565. ],
  566. borderRadius: BorderRadius.all(
  567. Radius.circular(20)),
  568. border: Border.all(
  569. color: Colors.red)),
  570. child: Row(
  571. children: <Widget>[
  572. Expanded(
  573. child: DropdownButton(
  574. isExpanded: true,
  575. underline: Container(),
  576. value: curGiftValue,
  577. style: TextStyle(fontSize: 16),
  578. iconEnabledColor:
  579. Colors.redAccent,
  580. onChanged: (newValue) {
  581. setBottomSheetState(() {
  582. curGiftValue = newValue;
  583. });
  584. },
  585. items: <int>[1, 6, 8, 66]
  586. .map<DropdownMenuItem>((v) => DropdownMenuItem(
  587. value: v,
  588. child: Container(
  589. padding:
  590. EdgeInsets.only(
  591. left: 20),
  592. child: fixedText(
  593. v.toString(),
  594. color: v == curGiftValue
  595. ? Colors
  596. .redAccent
  597. : Colors
  598. .black))))
  599. .toList(),
  600. )),
  601. Expanded(
  602. child: InkWell(
  603. child: Container(
  604. decoration: BoxDecoration(
  605. color: Color(0xFFFD6E8F),
  606. borderRadius:
  607. BorderRadius.only(
  608. topRight: Radius
  609. .circular(20),
  610. bottomRight:
  611. Radius.circular(
  612. 20)),
  613. ),
  614. height: 36,
  615. alignment: Alignment.center,
  616. child: Text(
  617. I18n.of(context).give,
  618. textScaleFactor: 1.0,
  619. style: TextStyle(
  620. color: Colors.white,
  621. fontSize: 16,
  622. fontWeight:
  623. FontWeight.bold),
  624. ),
  625. ),
  626. onTap: () {
  627. _sendGift(context);
  628. },
  629. ))
  630. ],
  631. )))
  632. ],
  633. ),
  634. )
  635. ],
  636. ));
  637. },
  638. );
  639. });
  640. }
  641. _sendGift(BuildContext context) async {
  642. int curSelectIndex =
  643. Provider.of<GiftSelectProvider>(context).curSelectIndex;
  644. var curGift = giftList[curSelectIndex];
  645. int price = curGift.price;
  646. int giftId = curGift.id;
  647. int total = curGiftValue * price;
  648. if (total > Provider.of<MoneyChangeProvider>(context).money) {
  649. showToast(I18n.of(context).not_enough);
  650. return;
  651. }
  652. int buyCount = 0;
  653. if (curGiftValue > curGift.amount) {
  654. buyCount = curGiftValue - curGift.amount;
  655. }
  656. Map data = {
  657. "userId": UserData().basicInfo.userId,
  658. "rUserId": friendId,
  659. "id": giftId,
  660. "price": price,
  661. "amount": curGiftValue,
  662. "totalPrice": total,
  663. "payNum": buyCount
  664. };
  665. data['sign'] = TokenMgr().getSign(data);
  666. Response res = await HttpUtil().post('reward/gift', data: data);
  667. if (res == null) {
  668. return;
  669. }
  670. Map resData = res.data;
  671. //showToast(resData['msg']);
  672. if (resData['code'] == 0) {
  673. //赠送成功
  674. print('赠送金额:${resData['data']}');
  675. int receiveAmount = resData['data'];
  676. Navigator.pop(context);
  677. Provider.of<MoneyChangeProvider>(context, listen: false).subMoney(total);
  678. AnimEffect.showEffect(context, giftId);
  679. AnimEffect.showDashangEffect(context, true, curGiftValue, curGift.name);
  680. MsgHandler.sendGiftTo(friendId, giftId, curGiftValue, receiveAmount);
  681. setState(() {});
  682. }
  683. }
  684. //是否开启扬声器
  685. void _isSpeakPhone() {
  686. setState(() {
  687. speakPhone = !speakPhone;
  688. });
  689. AgoraRtcEngine.setEnableSpeakerphone(speakPhone);
  690. }
  691. //退出频道 退出本页面
  692. void _onExit(BuildContext context) {
  693. if (!isQuit) {
  694. if (Navigator.canPop(context)) {
  695. isQuit = true;
  696. Navigator.of(context).pop();
  697. SoundUtils().stop();
  698. callingTimer?.cancel();
  699. }
  700. }
  701. }
  702. }