import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyrefresh/easy_refresh.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:ysairplane2/code/YSNewDetail.dart'; import 'package:ysairplane2/tools/YSNetWorking.dart'; import 'package:ysairplane2/tools/YSTools.dart'; const APPBAR_SCROLL_OFFSET = 100; class YSNews extends StatefulWidget { @override _YSNewsState createState() => _YSNewsState(); } class _YSNewsState extends State { ScrollController _scroll = ScrollController(); SwiperController _swiperController = SwiperController(); double appBarAlpha = 0; double start = 0; double number = 0; List _titles = []; List _lists = []; List _heads = []; int _selected = 0; int _page = 1; _onScroll(offset) { double alpha = offset / APPBAR_SCROLL_OFFSET; if (alpha < 0) { alpha = 0; } else if (alpha > 1) { alpha = 1; } setState(() { appBarAlpha = alpha; }); } @override void initState() { Future.delayed(Duration(seconds: 0)).then((value){ _getTitleArrayData(); _getHeadArrayData(); }); super.initState(); } @override void dispose() { _swiperController.dispose(); _scroll.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: SingleChildScrollView( child:Stack( children: [ Container( height: MediaQuery.of(context).size.height, child: Listener( onPointerDown: (PointerDownEvent pointerDownEvent) { start = pointerDownEvent.position.dy; }, onPointerMove: (PointerMoveEvent pointerMoveEvent) { setState(() { number = (pointerMoveEvent.position.dy-start)/5; }); }, onPointerUp: (PointerUpEvent upEvent) { setState(() { number = 0; }); }, child: NotificationListener( onNotification: (scrollNotification) { if (scrollNotification is ScrollUpdateNotification && scrollNotification.depth == 0) { //滚动且是列表滚动的时候 _onScroll(scrollNotification.metrics.pixels); return true; }else{ return false; } }, child: EasyRefresh( onRefresh: _refreshData, onLoad: _loadMore, header: MaterialHeader( ), footer: BezierBounceFooter( backgroundColor: Colors.transparent, color: Colors.transparent, ), child: SingleChildScrollView( controller: _scroll, padding: EdgeInsets.all(0), child: Column( children: [ if(_heads.length>0)Container( height: hsp(360)+number, child: Swiper( itemBuilder: (BuildContext context, int index) { return Container( child: ysImageLoad( imageUrl: '${_heads[index]['cover']}', fit: BoxFit.fill, height: hsp(360)+number, width: MediaQuery.of(context).size.width ) ); }, itemCount: _heads.length, scrollDirection: Axis.horizontal, loop: true, duration: 300, autoplay: true, onTap: (index) { Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return YSNewDetail(newId: _heads[index]['id'],); } ) ); }, pagination: SwiperPagination( alignment: Alignment.bottomCenter, builder: DotSwiperPaginationBuilder( color: Colors.white, activeColor: Colors.white, size: 5.0, activeSize: 10.0 ), ), autoplayDisableOnInteraction : true, controller: _swiperController, ), ), Container( height: 50, color: Colors.white, padding: EdgeInsets.only(left: 15,right: 15,top: 20), child: ListView.separated( scrollDirection: Axis.horizontal, itemBuilder: (context,index){ return GestureDetector( onTap: (){ setState(() { _selected = index; }); _refreshData(); }, child: Container( height: 30, child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('${_titles[index]['typeName']}',style: TextStyle(fontSize: 16,color: index==_selected?Color(0xFF007AFF):Color(0xFFCCCCCC),decoration: TextDecoration.none, fontWeight: index==_selected?FontWeight.bold:FontWeight.normal),), Container(height: 2,width: 20,color: index==_selected?Color(0xFF007AFF):Colors.transparent,) ], ), ), ); }, separatorBuilder: (context,index){ return Container(width: 20,); }, itemCount: _titles.length ), ), Container( child: ListView.separated( shrinkWrap: true, itemBuilder: (context,index){ return GestureDetector( onTap: (){ Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return YSNewDetail(newId: _lists[index]['id'],); } ) ); }, child: Container( color: Colors.white, padding: EdgeInsets.all(wsp(30)), child: _lists[index]['coverCount']==1?Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: [ Column( children: [ Container( width: MediaQuery.of(context).size.width-wsp(350), margin: EdgeInsets.only(bottom: hsp(40)), child: Text('${_lists[index]['title']}',style: TextStyle(color: Color(0xFF333333),fontSize: zsp(36))), ), Row( children: [ Container( height: hsp(45), padding: EdgeInsets.only(left: wsp(9),right: wsp(9)), child: Text('${_lists[index]['labelName']}',style: TextStyle(color: Color(0xFF0081EC),fontSize: zsp(24)),), color: Color(0xFFC5E6FE), alignment: Alignment.center, margin: EdgeInsets.only(right: wsp(15)), ), Text('${_lists[index]['createtime']}',style: TextStyle(color: Color(0xFF999999),fontSize: zsp(24)),), ], ) ], crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.spaceBetween, ), Container( height: hsp(200), width: wsp(220), child: ysImageLoad( imageUrl: '${_lists[index]['cover']}', fit: BoxFit.fill, height: hsp(200), width: wsp(220), ), ) ], ):_lists[index]['coverCount']==0?Column( children: [ Container( width: MediaQuery.of(context).size.width-wsp(60), margin: EdgeInsets.only(bottom: hsp(40)), child: Text('${_lists[index]['title']}',style: TextStyle(color: Color(0xFF333333),fontSize: zsp(36)),maxLines: 2,), ), Row( children: [ Container( height: hsp(45), padding: EdgeInsets.only(left: wsp(9),right: wsp(9)), child: Text('${_lists[index]['labelName']}',style: TextStyle(color: Color(0xFF0081EC),fontSize: zsp(24)),), color: Color(0xFFC5E6FE), alignment: Alignment.center, margin: EdgeInsets.only(right: wsp(15)), ), Text('${_lists[index]['createtime']}',style: TextStyle(color: Color(0xFF999999),fontSize: zsp(24)),), ], ) ], crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.spaceBetween, ):Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: MediaQuery.of(context).size.width-wsp(60), child: Text('${_lists[index]['title']}',style: TextStyle(color: Color(0xFF333333),fontSize: zsp(36)),maxLines: 2,), ), Container( height: hsp(200), margin: EdgeInsets.only(top: hsp(30),bottom: hsp(30)), child: ListView.separated( itemBuilder: (context,indexSub){ return Container( height: 200, width: (MediaQuery.of(context).size.width-wsp(100))/3, child: ysImageLoad( imageUrl: '${((_lists[index]['cover'].toString()).split(','))[indexSub]}', fit: BoxFit.fill, height: 200, width: (MediaQuery.of(context).size.width-wsp(100))/3, ), ); }, separatorBuilder: (context,indexSub){ return Container( width: wsp(20), color: Colors.white, ); }, itemCount: (_lists[index]['cover'].toString()).split(',').length, padding: EdgeInsets.all(0), physics: NeverScrollableScrollPhysics(), scrollDirection: Axis.horizontal, ), width: MediaQuery.of(context).size.width-wsp(60), ), Row( children: [ Container( height: hsp(45), padding: EdgeInsets.only(left: wsp(9),right: wsp(9)), child: Text('${_lists[index]['labelName']}',style: TextStyle(color: Color(0xFF0081EC),fontSize: zsp(24)),), color: Color(0xFFC5E6FE), alignment: Alignment.center, margin: EdgeInsets.only(right: wsp(15)), ), Text('${_lists[index]['createtime']}',style: TextStyle(color: Color(0xFF999999),fontSize: zsp(24)),), ], ) ], ), ), ); }, separatorBuilder: (context,index){ return Divider(indent: wsp(32),endIndent: wsp(32),height: 0.5,thickness: 0.5,color: Color(0xFFEEEEEE),); }, itemCount: _lists.length, physics: NeverScrollableScrollPhysics(), padding: EdgeInsets.only(top: 0.5), ), decoration: BoxDecoration( border: Border(top: BorderSide(width: 0.5,color: Color(0xFFEEEEEE))) ), ) ], ), ), ), ), ), ), Container( height: MediaQuery.of(context).padding.top+44, padding: EdgeInsets.only(left: 15,right: 15,top: MediaQuery.of(context).padding.top), color: Colors.white.withOpacity(appBarAlpha), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ GestureDetector( onTap: (){ Navigator.pop(context); }, child: Container( alignment: Alignment.centerLeft, child: Icon(Icons.arrow_back_ios,size: 20,color: appBarAlpha>0.5?Color(0xFF000000):Colors.white,), width: (MediaQuery.of(context).size.width-30)/3, ) ), if(appBarAlpha>0.5)Container( child: Text('通航头条',style: TextStyle(fontSize: 18,color: Color(0xFF000000),decoration: TextDecoration.none),), width: (MediaQuery.of(context).size.width-30)/3, ), Container( width: (MediaQuery.of(context).size.width-30)/3, ) ], ), ) ], ), ), ); } _getHeadArrayData() async{ Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/headline/Carousel/list',parameter: {}); setState(() { _heads = dict['data']; }); } _getTitleArrayData() async{ Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/headline/type/list',parameter: {}); if(dict!=null){ setState(() { _titles = dict['data']['list']; }); _refreshData(); } } Future _refreshData() async{ _page = 1; Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/headline/list',parameter: {'pageNo':_page,'pageSize':10,'headlineTypeId':_titles[_selected]['id']}); if(dict!=null){ setState(() { _lists = dict['data']['resultList']; }); } } Future _loadMore() async{ _page++; Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/headline/list',parameter: {'pageNo':_page,'pageSize':10,'headlineTypeId':_titles[_selected]['id']}); if(dict!=null){ setState(() { _lists.addAll(dict['data']['resultList']); }); } } }