class LoadMoreState extends State<LoadMorePage> {
var lastFileID = '0';
var rowNumber = 0;
var cid = '29338';
var newsListBean = null;
List<ListBean> dataItems = <ListBean>[];
@override
void initState() {
super.initState();
loadData();
}
// 请求首页数据
Future<Null> loadData() {
this.isLoading = true;
final Completer<Null> completer = new Completer<Null>();
getNewsData();
completer.complete(null);
return completer.future;
}
getNewsData() async {
await http
.get(
'https://XXX.....&lastFileID=${lastFileID}&rowNumber=${rowNumber}')
.then((response) {
if (response.statusCode == 200) {
var jsonRes = json.decode(response.body);
newsListBean = NewsListBean(jsonRes);
setState(() {
if (newsListBean != null && newsListBean.list != null && newsListBean.list.length > 0) {
for (int i = 0; i < newsListBean.list.length; i++) {
dataItems.add(newsListBean.list[i]);
}
lastFileID = newsListBean.list[newsListBean.list.length - 1].fileID.toString();
rowNumber += newsListBean.list.length;
} else {}
});
}
});
}
bool dataNotification(ScrollNotification notification) {
if (notification is ScrollEndNotification) {
//下滑到最底部
if (notification.metrics.extentAfter == 0.0) {
print('======下滑到最底部======');
loadData();
}
//滑动到最顶部
if (notification.metrics.extentBefore == 0.0) {
print('======滑动到最顶部======');
lastFileID = '0';
rowNumber = 0;
dataItems.clear();
loadData();
}
}
return true;
}
// 处理列表中是否有数据,对应展示相应页面
Widget childWidget() {
Widget childWidget;
if (newsListBean != null &&
(newsListBean.success != null && !newsListBean.success)) {
childWidget = new Stack(
children: <Widget>[
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 100.0),
child: new Center(
child: Image.asset( 'images/icon_wrong.jpg', width: 120.0, height: 120.0, ),
),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 100.0, 0.0, 0.0),
child: new Center(
child: new Text( '抱歉!暂无内容哦~', style: new TextStyle(fontSize: 18.0, color: Colors.blue), ),
),
),
],
);
} else if (dataItems != null && dataItems.length != 0) {
childWidget = new Padding(
padding: EdgeInsets.all(2.0),
child: new ListView.builder(
controller: _scrollController,
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(6.0),
itemCount: dataItems == null ? 0 : dataItems.length,
itemBuilder: (context, item) {
return buildListData(context, dataItems[item]);
}),);
} else {
childWidget = new Center(
child: new Card(
child: new Stack(
children: <Widget>[
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 35.0),
child: new Center( child: SpinKitFadingCircle( color: Colors.blueAccent, size: 30.0, ), ),
),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 35.0, 0.0, 0.0),
child: new Center( child: new Text('正在加载中,莫着急哦~'), ),
),
])),);
}
return childWidget;
}
}
评论