Flutter 中关于FutureBuilder界面数据异步刷新和上拉下拉效果
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:zhiwuapp/entity/card_info_entity.dart';
import 'package:zhiwuapp/entity_factory.dart';
import 'package:zhiwuapp/utils/locale.dart';
import 'package:zhiwuapp/utils/zwnet.dart';
import 'me_base_info_show.dart';
import 'me_signature_page.dart';
class MyPage extends StatefulWidget {
MyPage({Key key}) : super(key: key);
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State {
CardInfoEntity cardInf; //用户卡片信息
Future future;
GlobalKey _easyRefreshKey = GlobalKey();
GlobalKey _headerKey = GlobalKey();
GlobalKey _footerKey = GlobalKey();
@override
void initState() {
super.initState();
future = fetchCardInfo(); //初始化卡片信息
}
//刷新数据
Future refresh() async {
setState(() {
future = fetchCardInfo();
});
}
//获取卡片信息
Future fetchCardInfo() async {
var res = await ZWNet().get('/users?share_id=${ZWNet.userInfo.uuid}');
setState(() {
var json = res.data['data'];
cardInf = EntityFactory.generateOBJ(json);
});
return cardInf;
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return buildFutureBuilder();
}
FutureBuilder buildFutureBuilder() {
return new FutureBuilder(
builder: (context, AsyncSnapshot async) {
if (async.connectionState == ConnectionState.active ||
async.connectionState == ConnectionState.waiting) {
return new Center(
child: new CircularProgressIndicator(),
);
}
if (async.connectionState == ConnectionState.done) {
if (async.hasError) {
return new Center(
child: new Text("ERROR"),
);
} else if (async.hasData) {
CardInfoEntity _cardInfo = async.data;
return new RefreshIndicator(
child: buildListView(context, _cardInfo), onRefresh: refresh);
}
}
},
future: future,
);
}
buildListView(BuildContext context, CardInfoEntity card) {
return EasyRefresh(
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
children: [
//用户头像
Padding(
padding: EdgeInsets.only(top: 12),
child: AspectRatio(
aspectRatio: 351 / 200,
child: card.avatar != null
? Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8)),
image: DecorationImage(
image: NetworkImage("http:${card.avatar}"),
fit: BoxFit.cover),
),
)
: Container(),
),
),
//基本信息展示
Padding(
padding: EdgeInsets.only(top: 12),
child:
cardInf != null ? MeBaseInfoShow(cardInf: card) : Container(),
),
//我的签名
Padding(
padding: EdgeInsets.only(top: 12),
child: cardInf != null ? MeSignature(cardInf: card) : Container(),
),
],
),
key: _easyRefreshKey,
behavior: ScrollOverBehavior(),
refreshHeader: ClassicsHeader(
refreshText:
ZWLocale.name(context, 'toast.refreshText'), //提示刷新文字Pull to refresh
refreshingText: ZWLocale.name(
context, 'toast.refreshingText'), //正在刷新文字... Refreshing...
refreshReadyText: ZWLocale.name(
context, 'toast.refreshReadyText'), //准备刷新文字Release to refresh
refreshedText: ZWLocale.name(
context, 'toast.refreshedText'), // 刷新完成文字Refresh finished
key: _headerKey,
bgColor: Colors.transparent,
textColor: Colors.black87,
moreInfoColor: Colors.black54,
showMore: true,
),
refreshFooter: ClassicsFooter(
loadText:
ZWLocale.name(context, 'toast.loadText'), //提示加载文字 Push to load
loadReadyText: ZWLocale.name(
context, 'toast.loadReadyText'), //准备加载文字Release to load
loadingText:
ZWLocale.name(context, 'toast.loadingText'), //正在加载文字Loading...
loadedText:
ZWLocale.name(context, 'toast.loadedText'), //刷新完成文字 Load finished
noMoreText: ZWLocale.name(context, 'toast.noMoreText'), //没有更多文字
key: _footerKey,
bgColor: Colors.transparent,
textColor: Colors.black87,
moreInfoColor: Colors.black54,
showMore: true,
),
onRefresh: () async {
//下拉刷新
await fetchCardInfo();
},
loadMore: () async {
//上拉加载
await fetchCardInfo();
},
);
}
}