import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import '../../generated/l10n.dart'; import '../../retrofit/business_api.dart'; import '../../retrofit/data/base_data.dart'; import '../../retrofit/data/refund_reason_list.dart'; import '../../utils/business_instance.dart'; import '../../utils/flutter_utils.dart'; import '../../view_widget/border_text.dart'; import '../../view_widget/round_button.dart'; import '../../view_widget/settlement_tips_dialog.dart'; class RequestRefund extends StatefulWidget { final Map arguments; RequestRefund({this.arguments}); @override State createState() { return _RequestRefund(); } } class _RequestRefund extends State { final RefreshController refreshController = RefreshController(); final TextEditingController modifyAmountController = TextEditingController(); FocusNode _focusNode = FocusNode(); bool isKeyBoardShow = false; int modifyAmountState = 0; int refundState = 0; RefundReasonList reasonReasonList; BusinessApiService businessService; String refundAmount; String selectRefundReason; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { setState(() { print("object: ${MediaQuery.of(context).viewInsets.bottom}"); if (MediaQuery.of(context).viewInsets.bottom == 0) { if (isKeyBoardShow) { isKeyBoardShow = false; //关闭键盘 软键盘关闭了, 清除输入控件的焦点, 否则重新进入页面会导致软键盘再弹出问题 FocusScope.of(context).requestFocus(FocusNode()); } } else { isKeyBoardShow = true; } }); }); refundAmount = widget.arguments["refundAmount"]; queryRefundReason(); } ///离开页面记着销毁和清除 @override void dispose() { _focusNode.unfocus(); super.dispose(); } ///退款原因 queryRefundReason() async { if (businessService == null) { businessService = BusinessApiService(Dio(), context: context, token: BusinessInstance.instance.businessToken, tenant: BusinessInstance.instance.businessTenant, storeId: widget.arguments["storeId"]); } BaseData baseData = await businessService.refundReason({ "current": 1, "model": {"type": 3}, "size": 20 }).catchError((error) { SmartDialog.showToast(AppUtils.dioErrorTypeToString(error.type), alignment: Alignment.center); }); if (baseData != null && baseData.isSuccess) { reasonReasonList = baseData.data; } } ///申请退款 orderRefund() async { EasyLoading.show( status: S.current.zhengzaijiazai, maskType: EasyLoadingMaskType.black); try { if (businessService == null) { businessService = BusinessApiService(Dio(), context: context, token: BusinessInstance.instance.businessToken, tenant: BusinessInstance.instance.businessTenant, storeId: widget.arguments["storeId"]); } BaseData baseData = await businessService.refundOrder({ "tenant_code": BusinessInstance.instance.businessTenant, "orderId": widget.arguments["orderId"], "mid": widget.arguments["mid"], "reason": selectRefundReason, "backMoney": modifyAmountController.text == "" ? refundAmount : modifyAmountController.text, }).catchError((error) {}); if (baseData != null && baseData.isSuccess) { SmartDialog.show( widget: SettlementTips( () {}, text: baseData?.data ?? "", color: Color(0xFF30415B), )); Navigator.of(context).pop(refundState); } else { SmartDialog.showToast(baseData?.msg ?? "", alignment: Alignment.center); } } finally { EasyLoading.dismiss(); } } @override Widget build(BuildContext context) { return GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { FocusScope.of(context).requestFocus(FocusNode()); }, child: Scaffold( resizeToAvoidBottomInset: false, backgroundColor: Color(0xFFF8F8FA), appBar: MyAppBar( title: S.of(context).shenqingtuikuan, titleColor: Colors.black, background: Colors.white, leadingColor: Colors.black, brightness: Brightness.dark, ), body: Container( margin: EdgeInsets.only(top: 24.h, left: 16.w, right: 16.w), child: Column( children: [ Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8.w), boxShadow: [ BoxShadow( color: Color(0x0F06152E).withAlpha(12), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ), ], ), padding: EdgeInsets.only( top: 16.h, left: 16.w, right: 16.w, ), child: Column( children: [ Row( children: [ Container( width: 2.w, height: 16.h, color: Color(0xFF30415B), margin: EdgeInsets.only(right: 6.w), ), Text( "申请金额", style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 16.sp, fontWeight: MyFontWeight.semi_bold, ), ), ], ), SizedBox( height: modifyAmountState == 0 ? 32.h : 16.h, ), Row( children: [ if (modifyAmountState == 0) Expanded( child: Text.rich( TextSpan( children: [ TextSpan( text: "¥", style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 24.sp, fontWeight: MyFontWeight.semi_bold, ), ), TextSpan( text: refundAmount ?? "", style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 24.sp, fontWeight: MyFontWeight.medium, ), ), ], ), )), if (modifyAmountState == 1) Text( "¥", style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 24.sp, fontWeight: MyFontWeight.semi_bold, ), ), if (modifyAmountState == 1) Expanded( child: TextField( controller: modifyAmountController, decoration: InputDecoration( hintText: "请输入修改金额", hintStyle: TextStyle( color: Color(0xFF0D0D0D), fontSize: 14.sp, fontWeight: MyFontWeight.regular), border: InputBorder.none, ), keyboardType: TextInputType.phone, style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 24.sp, fontWeight: MyFontWeight.medium), ), ), GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { // showModifyAmountDialog(); setState(() { modifyAmountState = 1; }); }, child: Row( children: [ Image.asset( "assets/image/bs_edit.webp", fit: BoxFit.cover, width: 16, height: 16, ), SizedBox( width: 4.w, ), Text( "修改金额", style: TextStyle( color: Color(0xFF7A797F), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ), ], ), ) ], ), Container( height: 1.h, width: double.infinity, color: Color(0xFFEBECEF), margin: EdgeInsets.only(bottom: 24.h), ), Padding( padding: EdgeInsets.only(bottom: 25.h), child: GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { showRefundReason(); }, child: Row( children: [ Expanded( child: Text( "退款原因", style: TextStyle( color: Color(0xFF7A797F), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), )), Text( selectRefundReason ?? "请选择退款原因", style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 14.sp, fontWeight: MyFontWeight.regular, ), ), Padding( padding: EdgeInsets.only(left: 8.w), child: Image.asset( "assets/image/bs_right.webp", width: 8.w, height: 12.h, )), ], ), ), ) ], ), ), Spacer(), GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { if (refundState == 1) { orderRefund(); } else { if (selectRefundReason == null) { SmartDialog.showToast("请选择退款原因", alignment: Alignment.center); } else showModifyAmountDialog(); } }, child: Container( width: double.infinity, alignment: Alignment.center, margin: EdgeInsets.only(bottom: 55.h, top: 125.h), padding: EdgeInsets.symmetric(vertical: 16.h), decoration: BoxDecoration( borderRadius: BorderRadius.circular(27), color: Color(0xFF30415B)), child: Text( "确定退款", style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: 16.sp, color: Colors.white, ), ), ), ) ], ), ), ), ); } ///修改金额提示 showModifyAmountDialog() { showDialog( context: context, builder: (context) { return AlertDialog( content: Container( width: MediaQuery.of(context).size.width - 84.w, child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( "申请退款", style: TextStyle( color: Color(0xFF0D0D0D), fontSize: 16.sp, fontWeight: MyFontWeight.regular, ), ), SizedBox( height: 15.h, ), Text( "此操作将进行退款申请,是否继续?", style: TextStyle( color: Color(0xFFF4524D), fontSize: 16.sp, fontWeight: MyFontWeight.regular, ), ), SizedBox( height: 35.h, ), Row( children: [ Expanded( child: InkWell( child: BorderText( text: S.of(context).quxiao, textColor: Color(0xFF30415B), fontSize: 16.sp, fontWeight: FontWeight.bold, borderColor: Color(0xFF30415B), radius: 4, padding: EdgeInsets.all(12), borderWidth: 1, ), onTap: () { Navigator.of(context).pop(); }, ), flex: 1, ), SizedBox( width: 16.w, ), Expanded( child: InkWell( child: RoundButton( text: S.of(context).queren, textColor: Colors.white, radius: 4, padding: EdgeInsets.all(12), backgroup: Color(0xFF30415B), fontSize: 16.sp, fontWeight: FontWeight.bold, ), onTap: () { setState(() { refundState = 1; Navigator.of(context).pop(); }); }, ), flex: 1, ), ], ) ], ), ), ); }, ); } ///退款原因 showRefundReason() { showModalBottomSheet( context: context, backgroundColor: Colors.transparent, builder: (context) { return Container( width: double.infinity, height: 309.h, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( topLeft: Radius.circular(8), topRight: Radius.circular(8), ), ), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( child: Container( alignment: Alignment.center, margin: EdgeInsets.only(top: 12.h,bottom: 12.h,left: 41.w), child: Text( "退款原因", style: TextStyle( fontWeight: MyFontWeight.bold, fontSize: 17.sp, color: Color(0xFF1A1A1A), ), ), )), GestureDetector( onTap: () { setState(() { Navigator.of(context).pop(); }); }, child: Padding( padding: EdgeInsets.only(right: 16.w), child: Image.asset( "assets/image/cancel.webp", width: 25.h, height: 25.h, ), ), ), ], ), Padding( padding: EdgeInsets.symmetric(horizontal: 16.w), child: ListView.builder( padding: EdgeInsets.zero, itemCount: reasonReasonList?.records?.length ?? 0, scrollDirection: Axis.vertical, shrinkWrap: true, physics: BouncingScrollPhysics(), itemBuilder: (context, position) { return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { setState(() { selectRefundReason = reasonReasonList.records[position].reason; }); Navigator.of(context).pop(); }, child: refundReasonItem( reasonReasonList.records[position]), ); }, ), ) ], ), ); }); } refundReasonItem(Records records) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: EdgeInsets.symmetric(vertical: 16.h), child: Text( records?.reason ?? "", style: TextStyle( fontWeight: MyFontWeight.bold, fontSize: 14.sp, color: Color(0xFF0D0D0D), ), ), ), Container( width: double.infinity, height: 1.h, color: Color(0xFFEBEBEB), ) ], ); } }