import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/article.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/page.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/view_widget/classic_header.dart'; import 'package:huixiang/view_widget/hot_item.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; class HotArticlePage extends StatefulWidget { @override State createState() { return _HotArticlePage(); } } class _HotArticlePage extends State { ApiService apiService; @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService(Dio(), token: value.getString("token")), queryArticle(), }); } int pageNum = 1; RefreshController refreshController = RefreshController(); _onRefresh() { pageNum = 1; queryArticle(); } List
articles = []; queryArticle() async { BaseData baseData = await apiService.queryArticle({ "pageNum": pageNum, "pageSize": 10, "searchKey": "", "state": 0, "type": 2 }).catchError((onError){ refreshController.refreshFailed(); refreshController.loadFailed(); }); if (baseData != null && baseData.isSuccess) { PageInfo pageInfo = PageInfo.fromJson(baseData.data); refreshController.refreshCompleted(); refreshController.loadComplete(); if(pageNum == 1) { articles.clear(); } articles.addAll(pageInfo.list.map((e) => Article.fromJson(e)).toList()); if (pageInfo.pageNum == pageInfo.pages) { refreshController.loadNoData(); } else { pageNum += 1; } setState(() {}); } else { refreshController.refreshFailed(); refreshController.loadFailed(); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Color(0xFFF7F7F7), elevation: 0, title: Text( S.of(context).remenwenzhangshipinliebiao, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, ), ), centerTitle: false, leading: GestureDetector( onTap: () { Navigator.of(context).pop(); }, child: Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(left: 10), padding: EdgeInsets.all(6), child: Icon( Icons.arrow_back_ios, color: Colors.black, size: 24, ), ), ), titleSpacing: 2, leadingWidth: 56, ), body: Container( child: SmartRefresher( controller: refreshController, enablePullDown: true, enablePullUp: true, physics: BouncingScrollPhysics(), header: MyHeader(), footer: CustomFooter( builder: (context, mode) { Widget body; if (mode == LoadStatus.idle) { body = Text("pull up load"); } else if (mode == LoadStatus.loading) { body = CupertinoActivityIndicator(); } else if (mode == LoadStatus.failed) { body = Text("Load Failed!Click retry!"); } else if (mode == LoadStatus.canLoading) { body = Text("release to load more"); } else { body = Text(S.of(context).meiyougengduoyouhuiquan); } return Container( height: 55.0, child: Center(child: body), ); }, ), onRefresh: _onRefresh, onLoading: queryArticle, child: ListView.builder( itemCount: articles.length, physics: NeverScrollableScrollPhysics(), scrollDirection: Axis.vertical, itemBuilder: (context, position) { return AspectRatio( aspectRatio: 2.47, child: Container( height: 130, margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16), child: HotArticleItem(articl: articles[position]), ), ); }, ), ), ), ); } }