Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

769 lines
27 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. fixedText(userName, fontSize: 20, color: Colors.white70),
  239. fixedText('$age', fontSize: 20, color: Colors.white70)
  240. ],
  241. ),
  242. SizedBox(height: 5),
  243. Text(
  244. '$local',
  245. style: TextStyle(fontSize: 16, color: Colors.white70),
  246. textScaleFactor: 1.0,
  247. textAlign: TextAlign.center,
  248. ),
  249. SizedBox(height: 5),
  250. Text(
  251. '$profession',
  252. style: TextStyle(fontSize: 16, color: Colors.white70),
  253. textScaleFactor: 1.0,
  254. textAlign: TextAlign.center,
  255. ),
  256. SizedBox(height: 20),
  257. Text(
  258. connectTip,
  259. maxLines: 1,
  260. style: TextStyle(fontSize: 20, color: Colors.white70),
  261. textScaleFactor: 1.0,
  262. textAlign: TextAlign.center,
  263. ),
  264. ],
  265. ),
  266. );
  267. }
  268. _replayToolBar() {
  269. return Container(
  270. child: Row(
  271. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  272. children: <Widget>[
  273. //接听按钮
  274. RawMaterialButton(
  275. onPressed: () {
  276. MsgHandler.stopAudioRing();
  277. int myId = UserData().basicInfo.userId;
  278. MsgHandler.sendReplyAudioChatReq(friendId, true);
  279. _createRendererView(myId);
  280. },
  281. child: Icon(
  282. Icons.call,
  283. color: Colors.white,
  284. size: 35.0,
  285. ),
  286. shape: CircleBorder(),
  287. elevation: 2.0,
  288. fillColor: Colors.greenAccent,
  289. padding: const EdgeInsets.all(15.0),
  290. ),
  291. //挂断按钮
  292. RawMaterialButton(
  293. onPressed: () {
  294. _onExit(context);
  295. MsgHandler.stopAudioRing();
  296. MsgHandler.sendReplyAudioChatReq(friendId, false);
  297. },
  298. child: Icon(
  299. Icons.call_end,
  300. color: Colors.white,
  301. size: 35.0,
  302. ),
  303. shape: CircleBorder(),
  304. elevation: 2.0,
  305. fillColor: Colors.redAccent,
  306. padding: const EdgeInsets.all(15.0),
  307. )
  308. ]));
  309. }
  310. _bottomToolBar() {
  311. List<Widget> showWidgets = [
  312. Text(I18n.of(context).voicing,
  313. style: TextStyle(fontSize: 11, color: Colors.white24),
  314. textAlign: TextAlign.center),
  315. SizedBox(height: 10),
  316. Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[
  317. Offstage(
  318. offstage: !isChating,
  319. child: UserData().giftSwitch > 0
  320. ? RawMaterialButton(
  321. onPressed: _showGiftSheet,
  322. child: Icon(
  323. IconData(0xe64e, fontFamily: Constants.IconFontFamily),
  324. color: Colors.blueAccent,
  325. size: 20.0,
  326. ),
  327. shape: CircleBorder(),
  328. elevation: 2.0,
  329. fillColor: Colors.white,
  330. padding: const EdgeInsets.all(12.0),
  331. )
  332. : SizedBox(width: 36, height: 36)),
  333. //挂断按钮
  334. RawMaterialButton(
  335. onPressed: () {
  336. MsgHandler.sendReplyAudioChatReq(friendId, false);
  337. _onExit(context);
  338. },
  339. child: Icon(
  340. Icons.call_end,
  341. color: Colors.white,
  342. size: 35.0,
  343. ),
  344. shape: CircleBorder(),
  345. elevation: 2.0,
  346. fillColor: Colors.redAccent,
  347. padding: const EdgeInsets.all(15.0),
  348. ),
  349. //是否外放
  350. Offstage(
  351. offstage: !isChating,
  352. child: RawMaterialButton(
  353. onPressed: _isSpeakPhone,
  354. child: new Icon(
  355. speakPhone ? Icons.volume_up : Icons.volume_down,
  356. color: Colors.blueAccent,
  357. size: 20.0,
  358. ),
  359. shape: new CircleBorder(),
  360. elevation: 2.0,
  361. fillColor: Colors.white,
  362. padding: const EdgeInsets.all(12.0),
  363. ))
  364. ])
  365. ];
  366. if (isChating) {
  367. showWidgets.insert(
  368. 0,
  369. Container(
  370. alignment: Alignment.center,
  371. height: 80,
  372. width: 200,
  373. child: CounterOverlay(),
  374. ));
  375. }
  376. return Container(
  377. child: Column(children: showWidgets),
  378. );
  379. }
  380. //创建渲染视图
  381. void _createRendererView(int uId) {
  382. //增加音频会话对象 为了音频布局需要(通过uid和容器信息)
  383. //加入频道 第一个参数是 token 第二个是频道id 第三个参数 频道信息 一般为空 第四个 用户id
  384. int myId = UserData().basicInfo.userId;
  385. setState(() {
  386. print('加入聊天房间');
  387. var channelName = UserData().getSessionId(friendId).toString();
  388. AgoraRtcEngine.joinChannel(null, channelName, null, myId);
  389. });
  390. }
  391. //设置事件监听
  392. void setAgoreEventListener() {
  393. //成功加入房间
  394. AgoraRtcEngine.onJoinChannelSuccess =
  395. (String channel, int uid, int elapsed) {
  396. print("加入成功id为:$uid elapsed:$elapsed");
  397. if (isReply) {
  398. //回应
  399. setState(() {
  400. isReply = false;
  401. isChating = true;
  402. });
  403. } else {
  404. MsgHandler.sendAudioChatReq(widget.userInfo.userId);
  405. callingTimer = Timer.periodic(Duration(milliseconds: 2200), (timer) {
  406. SoundUtils().play(
  407. 'http://testcyhd.chengyouhd.com/Upload/Audio/even_wheat_sound.mp3',
  408. isLocal: false);
  409. });
  410. //30没人接就自动挂断
  411. offTimer = Timer(Duration(seconds: 30), () {
  412. MsgHandler.sendReplyAudioChatReq(friendId, false);
  413. _onExit(context);
  414. });
  415. }
  416. };
  417. //监听是否有新用户加入
  418. AgoraRtcEngine.onUserJoined = (int uid, int elapsed) {
  419. print("新用户所加入的id为:$uid elapsed:$elapsed");
  420. setState(() {
  421. //更新UI布局
  422. SoundUtils().stop();
  423. callingTimer?.cancel();
  424. isChating = true;
  425. offTimer?.cancel();
  426. });
  427. };
  428. //监听用户是否离开这个房间
  429. AgoraRtcEngine.onUserOffline = (int uid, int reason) {
  430. print("用户离开的id为:$uid");
  431. _onExit(context);
  432. };
  433. AgoraRtcEngine.onError = (dynamic errCode) {
  434. print('通话异常');
  435. // _onExit(context);
  436. };
  437. //监听用户是否离开这个频道
  438. AgoraRtcEngine.onLeaveChannel = () {
  439. print("用户离开");
  440. };
  441. }
  442. void _showGiftSheet() {
  443. showModalBottomSheet(
  444. context: context,
  445. elevation: 2.0,
  446. shape: RoundedRectangleBorder(
  447. borderRadius: BorderRadius.only(
  448. topLeft: Radius.circular(20), topRight: Radius.circular(20))),
  449. backgroundColor: Colors.transparent,
  450. builder: (BuildContext context) {
  451. return StatefulBuilder(
  452. builder: (BuildContext context, setBottomSheetState) {
  453. return Container(
  454. height: 400,
  455. width: Screen.width,
  456. decoration: BoxDecoration(
  457. color: Colors.white,
  458. ),
  459. child: giftList == null
  460. ? Center(child: Text(I18n.of(context).no_gift))
  461. : Column(
  462. children: <Widget>[
  463. Container(
  464. // decoration: BoxDecoration(
  465. // color: Colors.orangeAccent,
  466. // borderRadius: borderRadius
  467. // ),
  468. padding: EdgeInsets.symmetric(
  469. vertical: 10, horizontal: 20),
  470. child: Row(
  471. children: <Widget>[
  472. Text(I18n.of(context).sent_gift,
  473. textScaleFactor: 1.0,
  474. style: TextStyle(
  475. fontSize: 16,
  476. fontWeight: FontWeight.bold,
  477. color: Constants.BlackTextColor)),
  478. Expanded(
  479. child: SizedBox(),
  480. ),
  481. Image.asset(
  482. R.assetsImagesCoin,
  483. scale: 2,
  484. ),
  485. SizedBox(
  486. width: 5,
  487. ),
  488. fixedText(
  489. I18n.of(context)
  490. .available_balance
  491. .replaceFirst('/s1', ''),
  492. color: Constants.GreyTextColor),
  493. Consumer<MoneyChangeProvider>(
  494. builder: (context,
  495. MoneyChangeProvider counter,
  496. child) =>
  497. fixedText(counter.money.toString(),
  498. color: Color(0xFFDB305D)),
  499. ),
  500. fixedText(I18n.of(context).mask_coin,
  501. color: Color(0xFFDB305D)),
  502. ],
  503. ),
  504. ),
  505. Container(
  506. alignment: Alignment.topCenter,
  507. padding: EdgeInsets.all(10),
  508. height: 230,
  509. child: GiftPage(giftList, 0),
  510. ),
  511. Expanded(child: SizedBox()),
  512. Padding(
  513. padding: EdgeInsets.symmetric(
  514. vertical: 5, horizontal: 20),
  515. child: Row(
  516. children: <Widget>[
  517. Expanded(
  518. child: SizedBox(
  519. height: 36,
  520. child: RaisedButton(
  521. padding:
  522. EdgeInsets.symmetric(horizontal: 5),
  523. child: Text(
  524. I18n.of(context).recharge,
  525. textScaleFactor: 1.0,
  526. maxLines: 1,
  527. style: TextStyle(
  528. fontSize: 16,
  529. fontWeight: FontWeight.bold),
  530. ),
  531. elevation: 2.0,
  532. color: Colors.orangeAccent,
  533. textColor: Colors.white,
  534. onPressed: () {
  535. ChargeMoney.showChargeSheet(context,
  536. () {
  537. setState(() {});
  538. });
  539. },
  540. shape: RoundedRectangleBorder(
  541. borderRadius: BorderRadius.all(
  542. Radius.circular(20)),
  543. ),
  544. ),
  545. )),
  546. SizedBox(width: 20),
  547. Expanded(
  548. flex: 2,
  549. child: Container(
  550. height: 36,
  551. decoration: BoxDecoration(
  552. boxShadow: [
  553. BoxShadow(
  554. blurRadius: 2.0,
  555. color: Colors.white24)
  556. ],
  557. borderRadius: BorderRadius.all(
  558. Radius.circular(20)),
  559. border: Border.all(
  560. color: Colors.red)),
  561. child: Row(
  562. children: <Widget>[
  563. Expanded(
  564. child: DropdownButton(
  565. isExpanded: true,
  566. underline: Container(),
  567. value: curGiftValue,
  568. style: TextStyle(fontSize: 16),
  569. iconEnabledColor:
  570. Colors.redAccent,
  571. onChanged: (newValue) {
  572. setBottomSheetState(() {
  573. curGiftValue = newValue;
  574. });
  575. },
  576. items: <int>[1, 6, 8, 66]
  577. .map<DropdownMenuItem>((v) => DropdownMenuItem(
  578. value: v,
  579. child: Container(
  580. padding:
  581. EdgeInsets.only(
  582. left: 20),
  583. child: fixedText(
  584. v.toString(),
  585. color: v == curGiftValue
  586. ? Colors
  587. .redAccent
  588. : Colors
  589. .black))))
  590. .toList(),
  591. )),
  592. Expanded(
  593. child: InkWell(
  594. child: Container(
  595. decoration: BoxDecoration(
  596. color: Color(0xFFFD6E8F),
  597. borderRadius:
  598. BorderRadius.only(
  599. topRight: Radius
  600. .circular(20),
  601. bottomRight:
  602. Radius.circular(
  603. 20)),
  604. ),
  605. height: 36,
  606. alignment: Alignment.center,
  607. child: Text(
  608. I18n.of(context).give,
  609. textScaleFactor: 1.0,
  610. style: TextStyle(
  611. color: Colors.white,
  612. fontSize: 16,
  613. fontWeight:
  614. FontWeight.bold),
  615. ),
  616. ),
  617. onTap: () {
  618. _sendGift(context);
  619. },
  620. ))
  621. ],
  622. )))
  623. ],
  624. ),
  625. )
  626. ],
  627. ));
  628. },
  629. );
  630. });
  631. }
  632. _sendGift(BuildContext context) async {
  633. int curSelectIndex =
  634. Provider.of<GiftSelectProvider>(context).curSelectIndex;
  635. var curGift = giftList[curSelectIndex];
  636. int price = curGift.price;
  637. int giftId = curGift.id;
  638. int total = curGiftValue * price;
  639. if (total > Provider.of<MoneyChangeProvider>(context).money) {
  640. showToast(I18n.of(context).not_enough);
  641. return;
  642. }
  643. int buyCount = 0;
  644. if (curGiftValue > curGift.amount) {
  645. buyCount = curGiftValue - curGift.amount;
  646. }
  647. Map data = {
  648. "userId": UserData().basicInfo.userId,
  649. "rUserId": friendId,
  650. "id": giftId,
  651. "price": price,
  652. "amount": curGiftValue,
  653. "totalPrice": total,
  654. "payNum": buyCount
  655. };
  656. data['sign'] = TokenMgr().getSign(data);
  657. Response res = await HttpUtil().post('reward/gift', data: data);
  658. if (res == null) {
  659. return;
  660. }
  661. Map resData = res.data;
  662. //showToast(resData['msg']);
  663. if (resData['code'] == 0) {
  664. //赠送成功
  665. print('赠送金额:${resData['data']}');
  666. int receiveAmount = resData['data'];
  667. Navigator.pop(context);
  668. Provider.of<MoneyChangeProvider>(context, listen: false).subMoney(total);
  669. AnimEffect.showEffect(context, giftId);
  670. AnimEffect.showDashangEffect(context, true, curGiftValue, curGift.name);
  671. MsgHandler.sendGiftTo(friendId, giftId, curGiftValue, receiveAmount);
  672. setState(() {});
  673. }
  674. }
  675. //是否开启扬声器
  676. void _isSpeakPhone() {
  677. setState(() {
  678. speakPhone = !speakPhone;
  679. });
  680. AgoraRtcEngine.setEnableSpeakerphone(speakPhone);
  681. }
  682. //退出频道 退出本页面
  683. void _onExit(BuildContext context) {
  684. if (!isQuit) {
  685. if (Navigator.canPop(context)) {
  686. isQuit = true;
  687. Navigator.of(context).pop();
  688. SoundUtils().stop();
  689. callingTimer?.cancel();
  690. }
  691. }
  692. }
  693. }