Hibok
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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