SelectRoom.dart 12 KB

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