YSLiveUserView.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutterappfuyou/code/base/YSBase.dart';
  6. import 'package:pull_to_refresh/pull_to_refresh.dart';
  7. import '../../base/YSTools.dart';
  8. class YSLiveUser extends StatefulWidget {
  9. const YSLiveUser({Key key}) : super(key: key);
  10. @override
  11. _YSLiveUserState createState() => _YSLiveUserState();
  12. }
  13. class _YSLiveUserState extends State<YSLiveUser> {
  14. List _dataArray = [];
  15. static WebSocket _socket;
  16. Timer _timer;
  17. RefreshController _refreshController = RefreshController(initialRefresh: false);
  18. int _totalNum = 0;
  19. @override
  20. void initState() {
  21. _getSocket();
  22. super.initState();
  23. }
  24. _getSocket() async{
  25. WebSocket.connect('wss://v2fy.niwoshenghuo.com/websocket').then((socket) {
  26. _socket = socket;
  27. socket.listen(_onData, cancelOnError: false);
  28. _timer =Timer.periodic(Duration(seconds: 50), (timer) {
  29. _socket.add('heartbeat');
  30. });
  31. }).catchError((e){
  32. LogUtil.d("Unable to connect: $e");
  33. _getSocket(); // 连接超时,重新建立连接
  34. });
  35. }
  36. _onData(event) async{
  37. Map dict = jsonDecode(event);
  38. LogUtil.d("---onData---$dict");
  39. if(dict['type']=='connection'){
  40. Map data = dict['data'];
  41. Map message = {};
  42. message['type'] = 'bind';
  43. message['live_stream'] = User().stream;
  44. message['uid'] = data['uid'];
  45. message['user'] = {'username':User().castName??User().name,'avatar':User().castAvatar??User().avatar,'is_owner':User().isAnchor};
  46. _socket.add(jsonEncode(message));
  47. }else if(dict['type']=='bind'){
  48. _socket.add(jsonEncode({'type':'staff'}));
  49. }else if(dict['type']=='staff'){
  50. Map data = dict['data'];
  51. _totalNum = data['total']??0;
  52. if(true){//data['current']==1
  53. _dataArray.clear();
  54. _dataArray = data['users']??[];
  55. }else{
  56. _dataArray.addAll(data['users']??[]);
  57. }
  58. setState(() {});
  59. }
  60. }
  61. @override
  62. void dispose() {
  63. _socket.close();
  64. if(_timer!=null){
  65. if(_timer.isActive)_timer.cancel();
  66. }
  67. super.dispose();
  68. }
  69. @override
  70. Widget build(BuildContext context) {
  71. return YSBase(
  72. ystitle: '学员列表',
  73. ysBgColor: Color(0xFFF0F0F1),
  74. yschild: SmartRefresher(
  75. onRefresh: _refreshData,
  76. onLoading: _loadMore,
  77. enablePullDown: _dataArray.length<=_totalNum,
  78. controller: _refreshController,
  79. header: ClassicHeader(
  80. height: 45.0,
  81. releaseText: '松开手刷新',
  82. refreshingText: '刷新中',
  83. completeText: '刷新完成',
  84. failedText: '刷新失败',
  85. idleText: '下拉刷新',
  86. ),
  87. footer: ClassicFooter(
  88. height: 45.0,
  89. canLoadingText: '没有更多数据了',
  90. noDataText: ' 无数据',
  91. loadingText: '加载中 ',
  92. failedText: '加载失败',
  93. idleText: '上拉加载',
  94. ),
  95. child: SingleChildScrollView(
  96. child: ListView.separated(
  97. itemBuilder: (context,index){
  98. Map item = _dataArray[index];
  99. return Container(
  100. color: Colors.white,
  101. padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10),
  102. child: LayoutBuilder(
  103. builder: (context,conSize){
  104. return Row(
  105. children: [
  106. Container(
  107. height: 35,
  108. width: 35,
  109. decoration: BoxDecoration(
  110. borderRadius: BorderRadius.all(Radius.circular(50)),
  111. color: Colors.pinkAccent,
  112. image: DecorationImage(image: NetworkImage(item['avatar']),fit: BoxFit.cover)
  113. ),
  114. ),
  115. Container(
  116. width: conSize.maxWidth-35,
  117. padding: EdgeInsets.only(left: 10,right: 10),
  118. child: Text(item['username']??''),
  119. )
  120. ],
  121. );
  122. },
  123. ),
  124. );
  125. },
  126. separatorBuilder: (context,index){
  127. return Divider(height: 0.2,thickness: 0.2,color: Color(0xFFF0F0F1),);
  128. },
  129. itemCount: _dataArray.length,
  130. shrinkWrap: true,
  131. physics: NeverScrollableScrollPhysics(),
  132. padding: EdgeInsets.all(0),
  133. ),
  134. ),
  135. ),
  136. );
  137. }
  138. Future<void> _refreshData() async{
  139. // _page = 1;
  140. // _socket.add(jsonEncode({'type':'staff'}));
  141. // _refreshController.refreshCompleted();
  142. }
  143. Future<void> _loadMore() async{
  144. // _page++;
  145. // _socket.add(jsonEncode({'type':'staff'}));
  146. // _refreshController.loadComplete();
  147. }
  148. }
  149. class YSUsersAlertView extends StatelessWidget {
  150. final List users;
  151. const YSUsersAlertView({Key key, this.users}) : super(key: key);
  152. @override
  153. Widget build(BuildContext context) {
  154. return Container(
  155. height: ysHeight(context)*0.6,
  156. width: ysWidth(context),
  157. decoration: BoxDecoration(
  158. color: Colors.white,
  159. borderRadius: BorderRadius.only(topRight: Radius.circular(5),topLeft: Radius.circular(5))
  160. ),
  161. padding: EdgeInsets.only(left: 10,right: 10,bottom: 10),
  162. child: Column(
  163. children: [
  164. Container(
  165. height: 50,
  166. child: Row(
  167. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  168. children: [
  169. Container(),
  170. Text('学员列表',style: TextStyle(fontSize: 16,color: Colors.black),),
  171. GestureDetector(
  172. onTap: (){
  173. Navigator.pop(context);
  174. },
  175. child: Icon(Icons.close,color: Colors.blue,),
  176. )
  177. ],
  178. ),
  179. ),
  180. Container(
  181. height: ysHeight(context)*0.6-60,
  182. child: ListView.separated(
  183. itemBuilder: (context,index){
  184. Map item = users[index];
  185. return Container(
  186. color: Colors.white,
  187. padding: EdgeInsets.only(top: 10,bottom: 10),
  188. child: LayoutBuilder(
  189. builder: (context,conSize){
  190. return Row(
  191. children: [
  192. Container(
  193. height: 35,
  194. width: 35,
  195. decoration: BoxDecoration(
  196. borderRadius: BorderRadius.all(Radius.circular(50)),
  197. color: Colors.pinkAccent,
  198. image: DecorationImage(image: NetworkImage(item['avatar']),fit: BoxFit.cover)
  199. ),
  200. ),
  201. Container(
  202. width: conSize.maxWidth-35,
  203. padding: EdgeInsets.only(left: 10,right: 10),
  204. child: Text(item['username']??''),
  205. )
  206. ],
  207. );
  208. },
  209. ),
  210. );
  211. },
  212. separatorBuilder: (context,index){
  213. return Divider(height: 0.2,thickness: 0.2,color: Color(0xFFF0F0F1),);
  214. },
  215. padding: EdgeInsets.all(0),
  216. itemCount: users.length,
  217. ),
  218. )
  219. ],
  220. ),
  221. );
  222. }
  223. }