import 'package:flutter/material.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/cupertino.dart'; import '../styles/totast.dart'; import '../net/HttpManager.dart'; import '../net/Result.dart'; import 'TipList.dart'; import 'package:flutter_redux/flutter_redux.dart'; import '../redux/AppState.dart'; import '../widget/LinearButton.dart'; import '../model/BannerInfo.dart'; import '../widget/ITextInput.dart'; import '../widget/HouseItem.dart'; import '../model/HouseInfo.dart'; import 'OpenRoom.dart'; import 'package:flutter_picker/flutter_picker.dart'; import '../model/GameInfo.dart'; import 'dart:async'; import 'dart:convert'; import 'dart:ui'; import 'dart:math'; class MatchPage extends StatefulWidget { @override _MatchPageState createState() => _MatchPageState(); } class _MatchPageState extends State with WidgetsBindingObserver { List houseList = []; int currentPage = 0; bool showBadge = false; int gameId = 0; String searchKey = ''; bool scrollFlag = false; List bannerList = []; ScrollController _controller; List gameList = []; String gameName = '全部游戏'; bool isFirst = true; FocusNode _focusNode = new FocusNode(); void showPicker(BuildContext context) { List _list = []; for (var item in gameList) { _list.add(item.gameName); } String PickerData = json.encode(_list); Picker( confirmText: '确定', cancelText: '取消', adapter: PickerDataAdapter(pickerdata: JsonDecoder().convert(PickerData)), changeToFirst: true, textAlign: TextAlign.left, columnPadding: const EdgeInsets.all(8.0), onConfirm: (Picker picker, List value) { setState(() { gameId = gameList[value[0]].id; gameName = gameList[value[0]].gameName; }); }).showModal(this.context); } Future getGame() async { // Toast.show(context, '加载中', -1, 'loading'); Result res = await HttpManager.get('gameInfo/all'); // Toast.hide(); if (res.success) { List _list = []; for (var item in res.data) { _list.add(GameInfo.fromJson(item)); } setState(() { gameList = _list; }); } else {} } Future getUnreadMsg() async { Result res = await HttpManager.get('systemNotice/unread', data: {'userId': StoreProvider.of(context).state.userInfo.id, 'statusFlag': 0}); if (res.success && res.data != null) { if (res.data > 0) { setState(() { showBadge = true; }); } else { setState(() { showBadge = false; }); } } } Future getRoomInfo() async { print('获取房间'); Map data = {'currentPage': currentPage, 'pageNumber': 20, 'myUserId': StoreProvider.of(context).state.userInfo.id}; data['advancedQuery'] = ''; if (gameId != 0) { data['gameId'] = gameId; } data['searchKey'] = searchKey; List _allList = houseList; if (currentPage == 1) { _allList = []; } if (StoreProvider.of(context).state.userInfo.recommender != null) { data['recommender'] = StoreProvider.of(context).state.userInfo.recommender; } Result res = await HttpManager.get('houseInfo/page', data: data); if (res.success && res.data['pp'] != null) { for (var item in res.data['pp']) { HouseInfo _temHouse = HouseInfo.fromJson(item); _allList.add(_temHouse); } if (res.data['page']['currentPage'] < res.data['page']['totalPage']) { scrollFlag = true; } else { scrollFlag = false; } } else {} setState(() { houseList = _allList; }); } Future search(String text) async { if (text != searchKey) { currentPage = 1; searchKey = text; getRoomInfo(); } } Future getBannerInfo() async { List _bannerList = []; Toast.show(context, '加载中', -1, 'loading'); Result res = await HttpManager.get('bannerInfo/all'); Toast.hide(); if (res.success && res.data != null) { for (var item in res.data) { BannerInfo banner = BannerInfo.fromJson(item); _bannerList.add(banner); } setState(() { bannerList = _bannerList; }); } } @override void initState() { super.initState(); bannerList = []; _controller = ScrollController(); // gameList.add(GameInfo.fromJson({"id": 0, "gameName": '全部游戏'})); _controller.addListener(() { if (_controller.position.pixels == _controller.position.maxScrollExtent) { if (scrollFlag) { setState(() { currentPage++; }); getRoomInfo(); } } }); Future.delayed(Duration.zero, () { getBannerInfo(); getGame(); getRoomInfo(); }); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return WillPopScope( child: GestureDetector( behavior: HitTestBehavior.translucent, child: Scaffold( appBar: AppBar( title: Text('赛事'), centerTitle: true, leading: Material( color: Colors.transparent, child: Builder( builder: (context) => InkWell( onTap: () async { bool result = await Navigator.push(context, CupertinoPageRoute(builder: (context) => TipList())); if (result) { getUnreadMsg(); } }, child: Image.asset('images/home_icon_xiaoxi.png'), ), ), ), actions: [ Container( width: 102, padding: EdgeInsets.all(15), child: LinearButton( btntext: '发起比赛', textColor: Color(0xFFA2423E), btnHeight: 26.0, colorList: [Color(0xFFFFE77C), Color(0xFFFFE77C)], textSize: 12.0, onTapHomeMenu: () => {Navigator.push(context, CupertinoPageRoute(builder: (context) => OpenRoom(roomFlag: '0')))}, ), ) ], ), body: Container( color: Color(0xFF2E3049), height: double.infinity, width: double.infinity, child: RefreshIndicator( color: Theme.of(context).primaryColor, backgroundColor: Colors.white, displacement: 10, onRefresh: () async { await Future.delayed(const Duration(seconds: 1)); gameId = 0; currentPage = 1; searchKey = ''; getRoomInfo(); // getBannerInfo(); getGame(); }, child: CustomScrollView( physics: AlwaysScrollableScrollPhysics(), controller: _controller, slivers: [ SliverToBoxAdapter( child: Container( width: double.infinity, height: 200, child: new Swiper( itemBuilder: (BuildContext context, int index) { return Image.network( bannerList[index].imageUrl, fit: BoxFit.fill, ); }, itemCount: bannerList.length, loop: false, autoplay: true, autoplayDelay: 5000, pagination: SwiperCustomPagination(builder: (BuildContext context, SwiperPluginConfig config) { Color activeColor = Color(0xFFD4504B); Color color = Colors.white; List list = []; int itemCount = config.itemCount; int activeIndex = config.activeIndex; for (int i = 0; i < itemCount; ++i) { bool active = i == activeIndex; list.add( Container( key: Key("pagination_$i"), margin:EdgeInsets.symmetric(vertical: 3,horizontal: 2), child: Container( width: active ? 8 : 3, height: 3, decoration: BoxDecoration(color: active ? activeColor : color, borderRadius: BorderRadius.all(Radius.circular(3))), )), ); } return Container( margin: EdgeInsets.only(top: 186), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: list, ), ); })), ), ), SliverPersistentHeader( pinned: true, //是否固定在顶部 floating: true, delegate: _SliverAppBarDelegate( minHeight: 60, //收起的高度 maxHeight: 60, //展开的最大高度 child: Container( color: Color(0xFF2E3049), height: 60, padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15), child: Container( decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(4))), child: Row( children: [ Container( width: 10, ), ImageIcon( AssetImage('images/home_icon_sousuo.png'), color: Color(0xFF727785), ), Expanded( flex: 1, child: Container( height: 34, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(100)), ), child: ITextField( hintText: '搜索竞赛房间', inputBorder: InputBorder.none, hintStyle: TextStyle(fontSize: 13, color: Color(0xFFB1B5C0), height: 1.2), textStyle: TextStyle(color: Colors.black), contentPadding: EdgeInsets.symmetric(vertical: 4, horizontal: 4), fieldCallBack: (content) { search(content); _focusNode.unfocus(); }, focusNode: _focusNode, counterStyle: TextStyle(color: Color(0xFF9BA0AE), fontSize: 0), textInputAction: TextInputAction.search, fileOnTap: () { print('选择input'); if (_controller.position.pixels < 200) { _controller.animateTo(200, duration: Duration(milliseconds: 300), curve: Curves.ease); } }), ), ), Container( width: 1, height: 10, color: Color(0xFFB1B5C0), ), Container( width: 99, child: InkWell( onTap: () { showPicker(context); }, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Expanded( flex: 1, child: Container( padding: EdgeInsets.only(left: 5), alignment: Alignment.center, child: Text( gameName, style: TextStyle(color: Color(0xFFB1B5C0)), overflow: TextOverflow.ellipsis, ), ), ), Image.asset('images/icon_zhankai_huise.png'), Container( width: 5, ) ], ))) ], ), ), )), ), SliverFixedExtentList( itemExtent: houseList.isEmpty ? 300 : 90, delegate: SliverChildBuilderDelegate((BuildContext context, int index) { if (houseList.isEmpty) { return Container( padding: EdgeInsets.only(top: 105), child: Column( children: [ Image.asset('images/icon_kong.png'), Text('暂时没有此房间哦~', style: TextStyle(fontSize: 14, color: Theme.of(context).primaryColor.withOpacity(0.3))) ], ), ); } else if (index == houseList.length) { return Text('更多房间敬请期待...', style: TextStyle(color: Colors.grey, fontSize: 13, height: 3), textAlign: TextAlign.center); } return HouseItem(houseList[index], houseList[index].gameInfo); }, childCount: houseList.length + 1), ) ], ), ), )), onTap: () { _focusNode.unfocus(); }, ), onWillPop: () { if (isFirst) { isFirst = false; Toast.show(context, '再次点击退出程序', 1500, 'info'); Timer(Duration(seconds: 2), () { isFirst = true; }); return Future.value(false); } else { return Future.value(true); } }, ); } List _houseList(houselist) { List list = []; for (var i = 0; i < houselist.length; i++) { list.add(HouseItem(houselist[i], houselist[i].gameInfo)); } return list; } } class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { _SliverAppBarDelegate({ @required this.minHeight, @required this.maxHeight, @required this.child, }); final double minHeight; final double maxHeight; final Widget child; @override double get minExtent => minHeight; @override double get maxExtent => max(maxHeight, minHeight); @override Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) { return new SizedBox.expand(child: child); } @override bool shouldRebuild(_SliverAppBarDelegate oldDelegate) { return maxHeight != oldDelegate.maxHeight || minHeight != oldDelegate.minHeight || child != oldDelegate.child; } }