import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/coupon.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/item_title.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:huixiang/view_widget/my_footer.dart'; import 'package:huixiang/view_widget/new_coupon_widget.dart'; import 'package:huixiang/view_widget/receive_success.dart'; import 'package:huixiang/view_widget/selector_store_dialog.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class RollCenterPage extends StatefulWidget { @override State createState() { return _RollCenterPage(); } } class _RollCenterPage extends State { RefreshController _refreshController; ApiService apiService; @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService(Dio(), context: context, token: value.getString('token')), queryCoupon(), }); _refreshController = RefreshController(initialRefresh: false); } int pageNum = 1; List coupons = []; queryCoupon() async { BaseData baseData = await apiService.queryCoupon({ "centre": true, "pageNum": pageNum, "pageSize": 10, "searchKey": "", "state": 0 }).catchError((onError) { _refreshController.refreshFailed(); _refreshController.loadFailed(); }); if (pageNum == 1) coupons.clear(); if (baseData != null && baseData.isSuccess) { PageInfo pageInfo = PageInfo.fromJson(baseData.data); coupons.addAll(pageInfo.list.map((e) => Coupon.fromJson(e)).toList()); _refreshController.refreshCompleted(); _refreshController.loadComplete(); if (pageInfo.pageNum == pageInfo.pages) { _refreshController.loadNoData(); } else { pageNum += 1; } setState(() {}); } else { _refreshController.refreshFailed(); _refreshController.loadFailed(); } } refreshCoupon() { pageNum = 1; queryCoupon(); } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( title: S.of(context).lingquanzhongxin, titleColor: Colors.black, background: Color(0xFFF7F7F7), leadingColor: Colors.black, ), body: Container( child: Column( children: [ ItemTitle( text: S.of(context).kelingqudeyouhuiquan, imgPath: "assets/image/icon_card_coupon.png", moreText: S.of(context).chakanwodekaquan, onTap: () { Navigator.of(context).pushNamed('/router/mine_card'); }, ), Expanded( child: SmartRefresher( enablePullDown: true, enablePullUp: true, header: MyHeader(), footer: CustomFooter( builder: (BuildContext context, LoadStatus mode) { return MyFooter(mode); }, ), controller: _refreshController, onRefresh: refreshCoupon, onLoading: queryCoupon, physics: BouncingScrollPhysics(), child: ListView.builder( itemBuilder: (context, position) { return GestureDetector( child: NewCouponWidget(coupons[position], (type) { if (type == 1) { receiveCoupon(coupons[position].id); } else { if (coupons[position].bizType == 5) { Navigator.of(context).pushNamed('/router/write_off_page',arguments: { "couponId":coupons[position].id, "coupon":coupons[position].toJson() }); } else { showStoreSelector(coupons[position].storeList); } } }, () { setState(() { coupons[position].isEx = !coupons[position].isEx; }); }, type: 1,), ); }, itemCount: coupons != null ? coupons.length : 0, ), ), ) ], ), ), ); } receiveCoupon(couponId) async { BaseData baseData = await apiService.receiveCoupon(couponId); if (baseData != null && baseData.isSuccess) { refreshCoupon(); showAlertDialog(); } } showStoreSelector(storeList) { showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (context) { return SelectorStoreWidget(storeList); }); } showAlertDialog() { //显示对话框 showDialog( context: context, builder: (BuildContext context) { return ReceiveSuccess(); }, ); } }