Flutter 中关于FutureBuilder界面数据异步刷新和上拉下拉效果

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<MyPage> {
  CardInfoEntity cardInf; //用户卡片信息
  Future future;

  GlobalKey<EasyRefreshState> _easyRefreshKey = GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> _headerKey = GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey = GlobalKey<RefreshFooterState>();

  @override
  void initState() {
    super.initState();
    future = fetchCardInfo(); //初始化卡片信息
  }

  //刷新数据
  Future refresh() async {
    setState(() {
      future = fetchCardInfo();
    });
  }

//获取卡片信息
  Future<CardInfoEntity> 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<CardInfoEntity> buildFutureBuilder() {
    return new FutureBuilder<CardInfoEntity>(
      builder: (context, AsyncSnapshot<CardInfoEntity> 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: <Widget>[
          //用户头像
          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();
      },
    );
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值