coupon_page.dart 8.26 KB
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/pages/coupon/coupon_item.dart';
import 'package:one_key/provider/coupon_provider.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/event_bus.dart';
import 'package:one_key/route/navigator_util.dart';
import 'package:one_key/widgets/page_exception.dart';
import 'package:one_key/widgets/round_button.dart';
import 'package:one_key/widgets/system_app_bar.dart';
import 'package:one_key/widgets/v_empty_view.dart';
import 'package:provider/provider.dart';

/**
 * 优惠券列表
 */

class CouponPage extends StatefulWidget {
  final bool needRefrsh;
  CouponPage({Key key, this.needRefrsh = false}) : super(key: key);

  @override
  _CouponPageState createState() => _CouponPageState();
}

class _CouponPageState extends State<CouponPage> {
  EasyRefreshController _controller = EasyRefreshController();
  CouponProvider couponProvider;

  ///获取优惠券列表数据
  _couponNetworkData({bool isShowLoading = true}) async {
    Map<String, String> params = {
      "uid": Provider.of<UserModel>(context, listen: false).user.openid,
      // 'is_time': '0'
    };
    await couponProvider.getCustomerActivity(context, params,
        isShowLoading: isShowLoading);
  }

  ///修改活动信息
  _setCustomerActivity(int index) async {
    Map<String, String> params = {
      "id": couponProvider.couponList[index].id,
      "status": couponProvider.couponList[index].isOpenBool ? '1' : '0'
    };
    bool isSuccess = await couponProvider.setCustomerActivity(context, params,
        isShowLoading: false);
    print(isSuccess);
    _couponNetworkData(isShowLoading: false);
    // if (isSuccess) {
    //   couponProvider.notifyListeners();
    // } else {
    //   couponProvider.couponList[index].isOpenBool =
    //       !couponProvider.couponList[index].isOpenBool;
    //   couponProvider.notifyListeners();
    // }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _couponNetworkData();
    });
    // bus.on("refresh_coupon_list", (arg) {
    //   _couponNetworkData();
    // });
  }

  @override
  void dispose() {
    bus.off('changeStatusColor');
    // bus.off("refresh_coupon_list");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    couponProvider = Provider.of<CouponProvider>(context, listen: true);
    return Scaffold(
        backgroundColor: MyColours.color_fefefe,
        appBar: SystemAppBar('线上券', rightImgName: '历史券', onTap: () {
          if (widget.needRefrsh) {
            EventBus().emit('refresh_coupon_invitation');
          } else {
            EventBus().emit('changeStatusColor');
          }

          Navigator.of(context).pop();
        }, onTapRight: () {
          NavigatorUtil.goCouponHistoryPage(context);
        }),
        body: WillPopScope(
            onWillPop: () {
              if (widget.needRefrsh) {
                EventBus().emit('refresh_coupon_invitation');
              } else {
                EventBus().emit('changeStatusColor');
              }
              Navigator.of(context).pop();
            },
            child: Column(
              children: [
                Expanded(child: Consumer<CouponProvider>(
                  builder: (context, model, _) {
                    if (model.pageState != PageState.Success) {
                      return ExceptionPage(
                        pageState: model.pageState,
                        callback: () {
                          // offset = 0;
                          // getMessage(showLoading: true);
                          _couponNetworkData();
                        },
                        emptyMsg: "暂时没有列表信息哦",
                        emptyImg: "empty_search",
                        showMsg: "加载中,请稍后...",
                      );
                    } else {
                      return EasyRefresh.custom(
                        controller: _controller,
                        shrinkWrap: true,
                        slivers: [
                          SliverList(
                            delegate: SliverChildBuilderDelegate(
                                (BuildContext context, int index) {
                              return Container(
                                  margin:
                                      EdgeInsets.all(ScreenUtil().setWidth(40)),
                                  padding:
                                      EdgeInsets.all(ScreenUtil().setWidth(30)),
                                  color: MyColours.color_F0F0F0,
                                  child: Text(
                                    '使用说明:需在结账前出示优惠券二维码,店内工作人员微信扫码核销。超过有效期的券,会自动作废并移出列表。优惠券不可找零、兑现。',
                                    style: TextStyle(
                                      color: MyColours.color_666666,
                                      fontSize: ScreenUtil().setSp(24),
                                    ),
                                    strutStyle: StrutStyle(
                                        forceStrutHeight: true,
                                        height: 1,
                                        leading: 0.3),
                                  ));
                            }, childCount: 1),
                          ),
                          SliverList(
                            delegate: SliverChildBuilderDelegate(
                                (BuildContext context, int index) {
                              return CouponItem(
                                from: 'quan',
                                couponSonModel:
                                    couponProvider.couponList[index],
                                showBtn: true,
                                onPressedSwich: () {
                                  couponProvider.couponList[index].isOpenBool =
                                      !couponProvider
                                          .couponList[index].isOpenBool;
                                  _setCustomerActivity(index);
                                },
                                onPressed: () {
                                  NavigatorUtil.goCouponDetailPage(context,
                                      couponProvider.couponList[index]);
                                },
                              );
                            }, childCount: couponProvider.couponList.length),
                          )
                        ],
                        // onRefresh: () async {
                        //   // offset = 0;
                        //   // getMessage(showLoading: false);
                        // },
                        // onLoad: () async {
                        //   // offset++;
                        //   // getMessage(showLoading: false);
                        // },
                      );
                    }
                  },
                )),
                VEmptyView(20),
                Padding(
                    padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(40), 0,
                        ScreenUtil().setWidth(40), ScreenUtil().setWidth(40)),
                    child: RoundButton(
                      onPressed: () async {
                        bool needRefresh =
                            await NavigatorUtil.goCouponEditPage(context);
                        print(needRefresh);
                        if (needRefresh != null && needRefresh) {
                          //刷新页面数据
                          // _controller.callRefresh();
                          _couponNetworkData();
                        }
                      },
                      text: '创建新券',
                      fontSize: 34,
                      isBold: false,
                      height: ScreenUtil().setWidth(80),
                      width: double.infinity,
                      radius: ScreenUtil().setWidth(10),
                    ))
              ],
            )));
  }
}