import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/data/base_data.dart'; import 'package:huixiang/data/invitation.dart'; import 'package:huixiang/data/page.dart'; import 'package:huixiang/data/user_info.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/constant.dart'; import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/classic_header.dart'; import 'package:huixiang/view_widget/my_footer.dart'; import 'package:huixiang/view_widget/no_data_view.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sharesdk_plugin/sharesdk_defines.dart'; import 'package:sharesdk_plugin/sharesdk_interface.dart'; import 'package:sharesdk_plugin/sharesdk_map.dart'; class InviteFriends extends StatefulWidget { @override State createState() { return _InviteFriends(); } } class _InviteFriends extends State { ApiService? apiService; final RefreshController refreshController = RefreshController(); final ScrollController scrollController = ScrollController(); List invitationList = []; UserInfo? userInfo; int pageNum = 1; String? phone = ""; @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) { String? user = value.getString('user'); if (user?.isNotEmpty ?? false) { phone = UserInfo.fromJson(jsonDecode(user!)).inviteCode; setState(() {}); } if (apiService != null) apiService = ApiService(Dio(), context: context, token: value.getString('token'), showLoading: false); queryUserInfo(); queryInviteMember(); }); } ///邀请记录 queryInviteMember() async { BaseData>? baseData = await apiService?.inviteMemberList({ "searchKey": "", "pageNum": 1, "pageSize": 100, }).catchError((error) { refreshController.refreshFailed(); return Future.value(null); }); if (baseData?.isSuccess ?? false) { if (baseData!.data != null && (baseData.data!.list?.isNotEmpty ?? false)) { if (pageNum == 1) { invitationList.clear(); } invitationList.addAll(baseData.data!.list!); } setState(() { refreshController.refreshCompleted(); }); } else { refreshController.refreshFailed(); } } ///查询用户信息 queryUserInfo() async { BaseData? baseDate = await apiService?.queryInfo().catchError((onError) { refreshController.refreshFailed(); return Future.value(null); }); if (baseDate?.isSuccess ?? false) { setState(() { userInfo = baseDate!.data; }); SharedPreferences.getInstance().then( (value) => { value.setString('user', jsonEncode(baseDate!.data)), }, ); refreshController.refreshCompleted(); } else { refreshController.refreshFailed(); } SmartDialog.dismiss(); } _onRefresh() { queryUserInfo(); queryInviteMember(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( shadowColor: Colors.white, backgroundColor: Colors.white, leading: GestureDetector( child: Icon( Icons.arrow_back_ios_new_rounded, color: Colors.black, ), onTap: () { Navigator.of(context).pop(); }, ), title: Text( S.of(context).yaoqinghaoyou, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 17.sp, color: Color(0xFF000000), ), ), centerTitle: true, elevation: 0.0, //滑动时颜色不变 scrolledUnderElevation: 0.0, ), body: SmartRefresher( controller: refreshController, enablePullDown: true, enablePullUp: false, header: MyHeader(), footer: CustomFooter( builder: (context, mode) { return MyFooter(mode); }, ), onRefresh: () { setState(() { _onRefresh(); }); }, physics: BouncingScrollPhysics(), scrollController: scrollController, child: Container( child: SingleChildScrollView( physics: BouncingScrollPhysics(), child: Column( children: [ invite(), activityRule(), achievement(), mineInvite(), ], ), ), ), ), ); } ///邀请图 Widget invite() { return Container( width: double.infinity, margin: EdgeInsets.only( bottom: 16.h, ), child: Column( children: [ Container( child: Image.asset( "assets/image/invite_friends.webp", width: double.infinity, fit: BoxFit.fitWidth, alignment: Alignment.topCenter, ), ), 26.d, GestureDetector( onTap: () { widgetToUrl(); }, child: Container( width: double.infinity, height: 52.h, decoration: BoxDecoration( borderRadius: BorderRadius.circular(26.h), color: Color(0xFF32A060), ), margin: EdgeInsets.symmetric(horizontal: 14.w), child: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Image.asset( "assets/image/wx.webp", height: 20, width: 20, fit: BoxFit.fill, ), SizedBox( width: 4.w, ), Text( "邀请微信好友", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 16.sp, color: Color(0xFFFFFFFF), ), ), ], ), ), ), ], ), ); } ///活动规则 Widget activityRule() { return Container( width: double.infinity, decoration: BoxDecoration( borderRadius: BorderRadius.circular(6), color: Color(0xFFFFFFFF), ), margin: EdgeInsets.only( bottom: 12.h, left: 14.h, right: 14.h, ), padding: EdgeInsets.all(6), child: Column( // mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( decoration: BoxDecoration( image: DecorationImage( fit: BoxFit.cover, image: AssetImage("assets/image/invite_bj.webp"), ), ), height: 54.h, width: double.infinity, alignment: Alignment.center, child: Text( S.of(context).huodongguize, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 16.sp, color: Color(0xFF32A060), ), ), ), SizedBox( height: 12.h, ), Row( children: [ Spacer(), Image.asset( "assets/image/invite_wx.webp", width: 42.h, height: 42.h, fit: BoxFit.cover, ), Expanded( child: dashedLine(), ), Image.asset( "assets/image/yq_zt.webp", width: 42.h, height: 42.h, fit: BoxFit.cover, ), Expanded( child: dashedLine(), ), Image.asset( "assets/image/zt_m.webp", width: 42.h, height: 42.h, fit: BoxFit.cover, ), Expanded( child: dashedLine(), ), Image.asset( "assets/image/invite_q.webp", width: 42.h, height: 42.h, fit: BoxFit.cover, ), Spacer(), ], ), Row( children: [ Spacer(), Container( alignment: Alignment.center, width: 42.h, child: Image.asset( "assets/image/invite_1.webp", width: 24.h, height: 24.h, fit: BoxFit.cover, ), ), Spacer(), Container( alignment: Alignment.center, width: 42.h, child: Image.asset( "assets/image/invite_2.webp", width: 24.h, height: 24.h, fit: BoxFit.cover, ), ), Spacer(), Container( alignment: Alignment.center, width: 42.h, child: Image.asset( "assets/image/invite_3.webp", width: 24.h, height: 24.h, fit: BoxFit.cover, ), ), Spacer(), Container( alignment: Alignment.center, width: 42.h, child: Image.asset( "assets/image/invite_4.webp", width: 24.h, height: 24.h, fit: BoxFit.cover, ), ), Spacer(), ], ), SizedBox(height: 8.h), Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Spacer(), Expanded( flex: 3, child: Text( "分享链接给好友", style: TextStyle( fontSize: 12.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFF181818), ), ), ), Spacer(), Expanded( flex: 3, child: Text( "好友注册并获得10元优惠券", style: TextStyle( fontSize: 12.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFF181818), ), ), ), Spacer(), Expanded( flex: 3, child: Text( "好友完成首单", style: TextStyle( fontSize: 12.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFF181818), ), ), ), Spacer(), Expanded( flex: 3, child: Text( "邀请达成获得奖励", textAlign: TextAlign.center, style: TextStyle( fontSize: 12.sp, height: 1.3.h, fontWeight: MyFontWeight.regular, color: Color(0xFF181818), ), ), ), Spacer(), ], ), ], ), ); } dashedLine() { return LayoutBuilder( builder: (ctx, c) { double width = c.constrainWidth(); return Flex( children: List.generate( width ~/ 4, (_) { return Container( width: 2, margin: EdgeInsets.symmetric(horizontal: 1), height: 1, child: DecoratedBox( decoration: BoxDecoration(color: Color(0xFF32A060)), ), ); }, ), mainAxisAlignment: MainAxisAlignment.spaceBetween, direction: Axis.horizontal, ); }, ); } ///我的成就 Widget achievement() { return Container( width: double.infinity, height: 150.h, decoration: BoxDecoration( borderRadius: BorderRadius.circular(6), color: Color(0xFFFFFFFF), ), margin: EdgeInsets.only( bottom: 12.h, left: 14.h, right: 14.h, ), padding: EdgeInsets.only( top: 6.h, left: 6, right: 6, bottom: 16.h, ), child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( decoration: BoxDecoration( image: DecorationImage( fit: BoxFit.cover, image: AssetImage("assets/image/invite_bj.webp"), ), ), height: 54.h, width: double.infinity, alignment: Alignment.center, child: Text( S.of(context).wodechengjiu, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 16.sp, color: Color(0xFF32A060), ), )), Row( children: [ Expanded( child: Column( children: [ Text( "${userInfo?.todayInviteNumber ?? 0}", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 25.sp, color: Color(0xFF000000), ), ), SizedBox( height: 5.h, ), Text( "今日邀请", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Color(0xFF808080), ), ) ], )), Expanded( child: Column( children: [ Text( "${userInfo?.inviteNumber ?? 0}", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 25.sp, color: Color(0xFF000000), ), ), SizedBox( height: 5.h, ), Text( "累积邀请", style: TextStyle( fontWeight: MyFontWeight.regular, fontSize: 12.sp, color: Color(0xFF808080), ), ) ], )) ], ), ], ), ); } ///我的邀请 Widget mineInvite() { return Container( width: double.infinity, decoration: BoxDecoration( borderRadius: BorderRadius.circular(6), color: Color(0xFFFFFFFF), ), margin: EdgeInsets.only(bottom: 12.h, left: 14.h, right: 14.h), padding: EdgeInsets.only(top: 6.h, left: 6, right: 6), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( decoration: BoxDecoration( image: DecorationImage( fit: BoxFit.cover, image: AssetImage("assets/image/invite_bj.webp"), ), ), height: 54.h, width: double.infinity, alignment: Alignment.center, child: Text( S.of(context).wodeyaoqing, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 16.sp, color: Color(0xFF32A060), ), )), SizedBox( height: 12.h, ), Padding( padding: EdgeInsets.symmetric(horizontal: 6.w), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( S.of(context).shoujihao, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 14.sp, color: Color(0xFF000000), ), ), Text( "注册时间", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 14.sp, color: Color(0xFF000000), ), ), ], ), ), SizedBox( height: 12.h, ), (invitationList.length == 0) ? NoDataView( src: "assets/image/ding_dan.webp", isShowBtn: false, text: "还没有邀请记录哦~", fontSize: 16.sp, margin: EdgeInsets.only(top: 120.h), ) : ListView.builder( padding: EdgeInsets.zero, itemCount: invitationList.length, scrollDirection: Axis.vertical, shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemBuilder: (context, position) { return GestureDetector( onTap: () {}, child: mineInviteItem(invitationList[position]), ); }, ), ], ), ); } Widget mineInviteItem(Invitation invitationList) { return Container( child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( AppUtils.phoneEncode(invitationList.phone ?? ""), style: TextStyle( color: Color(0xFF000000), fontSize: 12.sp, fontWeight: MyFontWeight.regular, ), ), Text( invitationList.createTime ?? "", style: TextStyle( color: Color(0xFF000000), fontSize: 12.sp, fontWeight: MyFontWeight.regular, ), ), ], ), Container( width: double.infinity, height: 1.h, color: Color(0xFFF7F7F7), margin: EdgeInsets.symmetric(vertical: 12.h), ), ], ), ); } GlobalKey globalKey = GlobalKey(); widgetToUrl() async { SSDKMap params = SSDKMap() ..setGeneral( "邀请好友", "", [ "", ], "", "", buildShareUrl(), "", "", "", "", SSDKContentTypes.webpage, ); SharesdkPlugin.share(ShareSDKPlatforms.wechatSession, params, (state, userData, contentEntity, error) { print("share!$state"); }); } String buildShareUrl() { return "https://hx.lotus-wallet.com/invite.html?invite=${phone}"; } }