SelectRoom.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart' as prefix0;
  4. import '../styles/totast.dart';
  5. import '../net/HttpManager.dart';
  6. import '../net/Result.dart';
  7. import 'package:flutter_redux/flutter_redux.dart';
  8. import '../redux/AppState.dart';
  9. import '../model/BannerInfo.dart';
  10. import '../widget/ITextInput.dart';
  11. import '../widget/HouseItem.dart';
  12. import '../model/HouseInfo.dart';
  13. import 'package:flutter_picker/flutter_picker.dart';
  14. import '../model/GameInfo.dart';
  15. import 'dart:async';
  16. import 'dart:convert';
  17. import 'dart:ui';
  18. import 'dart:math';
  19. class SelectRoom extends StatefulWidget {
  20. @override
  21. _SelectRoomState createState() => _SelectRoomState();
  22. }
  23. class _SelectRoomState extends State<SelectRoom> with WidgetsBindingObserver {
  24. List<HouseInfo> houseList = [];
  25. int currentPage = 0;
  26. bool showBadge = false;
  27. int gameId = 0;
  28. String searchKey = '';
  29. bool scrollFlag = false;
  30. List<BannerInfo> bannerList = [];
  31. ScrollController _controller;
  32. List<GameInfo> gameList = [];
  33. String gameName = '全部游戏';
  34. bool isFirst = true;
  35. FocusNode _focusNode = new FocusNode();
  36. void showPicker(BuildContext context) {
  37. List<String> _list = [];
  38. for (var item in gameList) {
  39. _list.add(item.gameName);
  40. }
  41. String PickerData = json.encode(_list);
  42. Picker(
  43. confirmText: '确定',
  44. cancelText: '取消',
  45. adapter: PickerDataAdapter<String>(pickerdata: JsonDecoder().convert(PickerData)),
  46. changeToFirst: true,
  47. textAlign: TextAlign.left,
  48. columnPadding: const EdgeInsets.all(8.0),
  49. onConfirm: (Picker picker, List value) {
  50. setState(() {
  51. gameId = gameList[value[0]].id;
  52. gameName = gameList[value[0]].gameName;
  53. });
  54. }).showModal(this.context);
  55. }
  56. Future<void> getGame() async {
  57. // Toast.show(context, '加载中', -1, 'loading');
  58. Result res = await HttpManager.get('gameInfo/all');
  59. // Toast.hide();
  60. if (res.success) {
  61. List<GameInfo> _list = [];
  62. for (var item in res.data) {
  63. _list.add(GameInfo.fromJson(item));
  64. }
  65. setState(() {
  66. gameList = _list;
  67. });
  68. } else {}
  69. }
  70. Future<void> getRoomInfo() async {
  71. print('获取房间');
  72. Map<String, dynamic> data = {'currentPage': currentPage, 'pageNumber': 20, 'myUserId': StoreProvider.of<AppState>(context).state.userInfo.id};
  73. data['advancedQuery'] = '';
  74. if (gameId != 0) {
  75. data['gameId'] = gameId;
  76. }
  77. data['searchKey'] = searchKey;
  78. List<HouseInfo> _allList = houseList;
  79. if (currentPage == 1) {
  80. _allList = [];
  81. }
  82. if (StoreProvider.of<AppState>(context).state.userInfo.recommender != null) {
  83. data['recommender'] = StoreProvider.of<AppState>(context).state.userInfo.recommender;
  84. }
  85. Result res = await HttpManager.get('houseInfo/page', data: data);
  86. if (res.success && res.data['pp'] != null) {
  87. for (var item in res.data['pp']) {
  88. HouseInfo _temHouse = HouseInfo.fromJson(item);
  89. _allList.add(_temHouse);
  90. }
  91. if (res.data['page']['currentPage'] < res.data['page']['totalPage']) {
  92. scrollFlag = true;
  93. } else {
  94. scrollFlag = false;
  95. }
  96. } else {}
  97. setState(() {
  98. houseList = _allList;
  99. });
  100. }
  101. Future<void> search(String text) async {
  102. if (text != searchKey) {
  103. currentPage = 1;
  104. setState(() {
  105. searchKey = text;
  106. });
  107. if (searchKey == '') {
  108. houseList = [];
  109. } else {
  110. getRoomInfo();
  111. }
  112. }
  113. }
  114. @override
  115. void initState() {
  116. super.initState();
  117. bannerList = [];
  118. _controller = ScrollController();
  119. // gameList.add(GameInfo.fromJson({"id": 0, "gameName": '全部游戏'}));
  120. _controller.addListener(() {
  121. if (_controller.position.pixels == _controller.position.maxScrollExtent) {
  122. if (scrollFlag) {
  123. setState(() {
  124. currentPage++;
  125. });
  126. getRoomInfo();
  127. }
  128. }
  129. print('aaaaaaa');
  130. setState(() {
  131. _focusNode.unfocus();
  132. });
  133. });
  134. Future.delayed(Duration.zero, () {
  135. getGame();
  136. // getRoomInfo();
  137. });
  138. }
  139. @override
  140. void dispose() {
  141. super.dispose();
  142. }
  143. @override
  144. Widget build(BuildContext context) {
  145. return GestureDetector(
  146. behavior: HitTestBehavior.translucent,
  147. child: Scaffold(
  148. backgroundColor: Color(0xFF23253C),
  149. body: SafeArea(
  150. child: Container(
  151. color: Color(0xFF2E3049),
  152. height: double.infinity,
  153. width: double.infinity,
  154. child: RefreshIndicator(
  155. color: Theme.of(context).primaryColor,
  156. backgroundColor: Colors.white,
  157. displacement: 10,
  158. onRefresh: () async {
  159. await Future.delayed(const Duration(seconds: 1));
  160. gameId = 0;
  161. currentPage = 1;
  162. searchKey = '';
  163. getGame();
  164. },
  165. child: CustomScrollView(
  166. physics: AlwaysScrollableScrollPhysics(),
  167. controller: _controller,
  168. slivers: <Widget>[
  169. SliverPersistentHeader(
  170. pinned: true, //是否固定在顶部
  171. floating: true,
  172. delegate: _SliverAppBarDelegate(
  173. minHeight: 100, //收起的高度
  174. maxHeight: 100, //展开的最大高度
  175. child: Container(
  176. color: Color(0xFF23253C),
  177. height: 60,
  178. padding: EdgeInsets.fromLTRB(15, 5, 0, 0),
  179. margin: EdgeInsets.fromLTRB(0, 0, 0, 15),
  180. child: Column(
  181. // decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(4))),
  182. children: [
  183. ITextField(
  184. hintText: '房间标题/房间号/房主ID号',
  185. inputBorder: InputBorder.none,
  186. hintStyle: TextStyle(fontSize: 13, color: Color(0xFFB1B5C0), height: 1.2),
  187. textStyle: TextStyle(color: Colors.black),
  188. contentPadding: EdgeInsets.symmetric(vertical: 4),
  189. inputText: searchKey,
  190. fieldCallBack: (content) {
  191. search(content);
  192. // _focusNode.unfocus();
  193. },
  194. focusNode: _focusNode,
  195. counterStyle: TextStyle(color: Color(0xFF9BA0AE), fontSize: 0),
  196. textInputAction: TextInputAction.search,
  197. fileOnTap: () {
  198. // print('选择input');
  199. // if (_controller.position.pixels < 200) {
  200. // _controller.animateTo(200, duration: Duration(milliseconds: 300), curve: Curves.ease);
  201. // }
  202. },
  203. hasCancel: true),
  204. Container(
  205. width: double.infinity,
  206. height: 40,
  207. padding: EdgeInsets.symmetric(horizontal: 20),
  208. child: InkWell(
  209. onTap: () {
  210. showPicker(context);
  211. },
  212. child: Row(
  213. mainAxisAlignment: MainAxisAlignment.center,
  214. children: <Widget>[
  215. Text(
  216. '筛选游戏',
  217. style: TextStyle(color: Colors.white54, fontSize: 12),
  218. ),
  219. Expanded(
  220. flex: 1,
  221. child: Container(),
  222. ),
  223. Container(
  224. padding: EdgeInsets.only(left: 5),
  225. alignment: Alignment.center,
  226. child: Text(
  227. gameName,
  228. style: TextStyle(color: Colors.white, fontSize: 12),
  229. overflow: TextOverflow.ellipsis,
  230. textAlign: TextAlign.right,
  231. ),
  232. ),
  233. Image.asset('images/icon_zhankai_huise.png'),
  234. Container(
  235. width: 5,
  236. )
  237. ],
  238. )))
  239. ],
  240. ),
  241. )),
  242. ),
  243. SliverFixedExtentList(
  244. itemExtent: houseList.isEmpty ? 300 : 90,
  245. delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
  246. if (houseList.isEmpty) {
  247. if (searchKey != '') {
  248. return InkWell(
  249. child: Container(
  250. padding: EdgeInsets.only(top: 105),
  251. child: Column(
  252. children: <Widget>[
  253. Image.asset('images/icon_kong.png'),
  254. Text('暂时没有此房间哦~', style: TextStyle(fontSize: 14, color: Theme.of(context).primaryColor.withOpacity(0.3)))
  255. ],
  256. ),
  257. ),
  258. onTap: () {
  259. _focusNode.unfocus();
  260. },
  261. );
  262. } else {
  263. return Container();
  264. }
  265. } else if (index == houseList.length) {
  266. return Text('更多房间敬请期待...', style: TextStyle(color: Colors.grey, fontSize: 13, height: 3), textAlign: TextAlign.center);
  267. }
  268. return HouseItem(
  269. houseList[index],
  270. houseList[index].gameInfo,
  271. isNext: !_focusNode.hasFocus,
  272. onTapHomeMenu: () {
  273. setState(() {
  274. _focusNode.unfocus();
  275. });
  276. },
  277. );
  278. }, childCount: houseList.length + 1),
  279. )
  280. ],
  281. ),
  282. ),
  283. ),
  284. )),
  285. onTap: () {
  286. _focusNode.unfocus();
  287. },
  288. );
  289. }
  290. List<Widget> _houseList(houselist) {
  291. List<Widget> list = [];
  292. for (var i = 0; i < houselist.length; i++) {
  293. list.add(HouseItem(houselist[i], houselist[i].gameInfo));
  294. }
  295. return list;
  296. }
  297. }
  298. class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  299. _SliverAppBarDelegate({
  300. @required this.minHeight,
  301. @required this.maxHeight,
  302. @required this.child,
  303. });
  304. final double minHeight;
  305. final double maxHeight;
  306. final Widget child;
  307. @override
  308. double get minExtent => minHeight;
  309. @override
  310. double get maxExtent => max(maxHeight, minHeight);
  311. @override
  312. Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
  313. return new SizedBox.expand(child: child);
  314. }
  315. @override
  316. bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
  317. return maxHeight != oldDelegate.maxHeight || minHeight != oldDelegate.minHeight || child != oldDelegate.child;
  318. }
  319. }