TRTCCallingContact.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import 'package:ctjt_flutter/common/trtc/GenerateTestUserSig.dart';
  2. import 'package:ctjt_flutter/common/trtc/ProfileManager_Mock.dart';
  3. import 'package:ctjt_flutter/common/trtc/TxUtils.dart';
  4. import 'package:ctjt_flutter/common/trtc/calling/model/TRTCCalling.dart';
  5. import 'package:ctjt_flutter/common/trtc/calling/model/TRTCCallingDelegate.dart';
  6. import 'package:flutter/cupertino.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_screenutil/flutter_screenutil.dart';
  9. import 'package:fluttertoast/fluttertoast.dart';
  10. import 'base/CallTypes.dart';
  11. import 'base/CallingScenes.dart';
  12. class TRTCCallingContact extends StatefulWidget {
  13. TRTCCallingContact(this.callingScenes, {Key? key}) : super(key: key);
  14. final CallingScenes callingScenes;
  15. @override
  16. _TRTCCallingContactState createState() => _TRTCCallingContactState();
  17. }
  18. class _TRTCCallingContactState extends State<TRTCCallingContact> {
  19. String searchText = '';
  20. String myLoginInfoId = '';
  21. List<UserModel> userList = [];
  22. late ProfileManager _profileManager;
  23. late TRTCCalling sInstance;
  24. goIndex() {
  25. Navigator.pushReplacementNamed(
  26. context,
  27. "/index",
  28. );
  29. return true;
  30. }
  31. goLoginPage() {
  32. Navigator.pushReplacementNamed(
  33. context,
  34. "/login",
  35. );
  36. return true;
  37. }
  38. //搜索
  39. onSearchClick() async {
  40. List<UserModel> ls =
  41. await ProfileManager.getInstance().queryUserInfo(searchText);
  42. setState(() {
  43. userList = ls;
  44. });
  45. }
  46. //发起通话
  47. onCallClick(UserModel userInfo) async {
  48. if (userInfo.userId == myLoginInfoId) {
  49. TxUtils.showErrorToast('不能呼叫自己', context);
  50. return;
  51. }
  52. Navigator.pushReplacementNamed(
  53. context,
  54. "/calling/callingView",
  55. arguments: {
  56. "remoteUserInfo": userInfo,
  57. "callType": CallTypes.Type_Call_Someone,
  58. "callingScenes": widget.callingScenes
  59. },
  60. );
  61. }
  62. // 提示浮层
  63. showToast(text) {
  64. Fluttertoast.showToast(
  65. msg: text,
  66. toastLength: Toast.LENGTH_SHORT,
  67. gravity: ToastGravity.CENTER,
  68. );
  69. }
  70. initUserInfo() async {
  71. _profileManager = await ProfileManager.getInstance();
  72. sInstance = await TRTCCalling.sharedInstance();
  73. String loginId = await TxUtils.getLoginUserId();
  74. await sInstance.login(GenerateTestUserSig.sdkAppId, loginId,
  75. await GenerateTestUserSig.genTestSig(loginId));
  76. sInstance.unRegisterListener(onTrtcListener);
  77. sInstance.registerListener(onTrtcListener);
  78. if (loginId == '') {
  79. TxUtils.showErrorToast("请先登录。", context);
  80. goLoginPage();
  81. } else {
  82. setState(() {
  83. myLoginInfoId = loginId;
  84. });
  85. }
  86. }
  87. onTrtcListener(type, params) async {
  88. switch (type) {
  89. case TRTCCallingDelegate.onInvited:
  90. {
  91. UserModel userInfo = await _profileManager
  92. .querySingleUserInfo(params["sponsor"].toString());
  93. Navigator.pushReplacementNamed(
  94. context,
  95. "/calling/callingView",
  96. arguments: {
  97. "remoteUserInfo": userInfo,
  98. "callType": CallTypes.Type_Being_Called,
  99. "callingScenes": params['type'] == TRTCCalling.typeVideoCall
  100. ? CallingScenes.VideoOneVOne
  101. : CallingScenes.AudioOneVOne
  102. },
  103. );
  104. }
  105. break;
  106. }
  107. }
  108. @override
  109. void initState() {
  110. super.initState();
  111. initUserInfo();
  112. }
  113. @override
  114. void dispose() {
  115. super.dispose();
  116. sInstance.unRegisterListener(onTrtcListener);
  117. }
  118. getGuideSearchWidget() {
  119. return Column(
  120. mainAxisAlignment: MainAxisAlignment.center,
  121. mainAxisSize: MainAxisSize.max,
  122. children: [
  123. Center(
  124. child: Image.asset(
  125. 'assets/images/callingDemo/search.png',
  126. height: 97,
  127. ),
  128. ),
  129. Center(
  130. child: Text('搜索添加已注册用户'),
  131. ),
  132. Center(
  133. child: Text('以发起通话'),
  134. ),
  135. ],
  136. );
  137. }
  138. getSearchResult() {
  139. return CustomScrollView(
  140. slivers: [
  141. SliverFixedExtentList(
  142. itemExtent: 55.0.h,
  143. delegate: SliverChildBuilderDelegate(
  144. (BuildContext context, int index) {
  145. var userInfo = userList[index];
  146. return Container(
  147. alignment: Alignment.centerLeft,
  148. margin: EdgeInsets.only(left: 20.w, right: 20.w),
  149. child: Row(
  150. children: [
  151. Container(
  152. child: ClipRRect(
  153. borderRadius: BorderRadius.circular(44.r),
  154. child: Image.network(
  155. userInfo.avatar,
  156. height: 44.h,
  157. fit: BoxFit.fitHeight,
  158. ),
  159. ),
  160. ),
  161. Expanded(
  162. flex: 1,
  163. child: Padding(
  164. padding: EdgeInsets.fromLTRB(15.w, 0, 0, 0),
  165. child: Text(
  166. userInfo.name,
  167. style: TextStyle(
  168. color: Colors.black,
  169. fontSize: 16.sp,
  170. ),
  171. ),
  172. ),
  173. ),
  174. Container(
  175. // ignore: deprecated_member_use
  176. child: RaisedButton(
  177. color: Colors.green,
  178. onPressed: () {
  179. onCallClick(userInfo);
  180. },
  181. child: Text(
  182. '呼叫',
  183. style: TextStyle(fontSize: 16.0.sp, color: Colors.white),
  184. ),
  185. shape: RoundedRectangleBorder(
  186. side: BorderSide.none,
  187. borderRadius: BorderRadius.all(Radius.circular(20.r)),
  188. ),
  189. ),
  190. )
  191. ],
  192. ),
  193. );
  194. },
  195. childCount: userList.length,
  196. ),
  197. ),
  198. ],
  199. );
  200. }
  201. @override
  202. Widget build(BuildContext context) {
  203. var searchBtn = Row(
  204. children: [
  205. Expanded(
  206. flex: 1,
  207. child: Container(
  208. padding: EdgeInsets.only(left: 20.w, right: 20.w, bottom: 10.h),
  209. decoration: BoxDecoration(
  210. borderRadius: BorderRadius.all(
  211. Radius.circular(19.0.r),
  212. ),
  213. color: Color.fromRGBO(244, 245, 249, 1.000),
  214. ),
  215. child: TextField(
  216. style: TextStyle(color: Colors.black),
  217. autofocus: true,
  218. decoration: InputDecoration(
  219. hintText: "搜索用户ID",
  220. hintStyle:
  221. TextStyle(color: Color.fromRGBO(187, 187, 187, 1.000)),
  222. enabledBorder: UnderlineInputBorder(
  223. borderSide: BorderSide(color: Colors.white),
  224. ),
  225. ),
  226. keyboardType: TextInputType.number,
  227. onChanged: (value) => this.searchText = value),
  228. ),
  229. ),
  230. Container(
  231. margin: EdgeInsets.only(right: 20.w),
  232. // ignore: deprecated_member_use
  233. child: RaisedButton(
  234. color: Color.fromRGBO(0, 110, 255, 1.000),
  235. shape: RoundedRectangleBorder(
  236. side: BorderSide.none,
  237. borderRadius: BorderRadius.all(Radius.circular(20.r)),
  238. ),
  239. onPressed: () {
  240. onSearchClick();
  241. },
  242. child: Text(
  243. '搜索',
  244. style: TextStyle(color: Colors.white),
  245. ),
  246. ),
  247. ),
  248. ],
  249. );
  250. var myInfo = Row(
  251. children: [
  252. Container(
  253. constraints: BoxConstraints(minHeight: 12.h, minWidth: 3.w),
  254. margin: EdgeInsets.only(left: 20.w, right: 10.w),
  255. color: Color.fromRGBO(153, 153, 153, 1.000),
  256. ),
  257. Text('您的用户ID是 $myLoginInfoId'),
  258. ],
  259. );
  260. return Scaffold(
  261. appBar: AppBar(
  262. title: widget.callingScenes == CallingScenes.VideoOneVOne
  263. ? Text('视频通话')
  264. : Text('语音通话'),
  265. leading: IconButton(
  266. icon: Icon(Icons.arrow_back_ios), //color: Colors.black
  267. onPressed: () async {
  268. goIndex();
  269. },
  270. ),
  271. centerTitle: true,
  272. elevation: 0,
  273. ),
  274. body: WillPopScope(
  275. onWillPop: () async {
  276. return goIndex();
  277. },
  278. child: Column(
  279. children: [
  280. searchBtn,
  281. myInfo,
  282. Expanded(
  283. flex: 1,
  284. child: getSearchResult(), //getGuideSearchWidget(),
  285. ),
  286. ],
  287. ),
  288. ),
  289. );
  290. }
  291. }