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/retrofit/data/findMiNiGroupList.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/classic_header.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:shimmer/shimmer.dart'; import '../../generated/l10n.dart'; import '../../retrofit/business_api.dart'; import '../../retrofit/data/base_data.dart'; import '../../retrofit/data/business_goods.dart'; import '../../utils/business_instance.dart'; import '../../utils/flutter_utils.dart'; import '../../view_widget/custom_image.dart'; import '../../view_widget/my_appbar.dart'; import '../../view_widget/my_footer.dart'; import '../../view_widget/no_data_view.dart'; class GoodsSearchPage extends StatefulWidget { final Map arguments; GoodsSearchPage({this.arguments}); @override State createState() { return _GoodsSearchPage(); } } class _GoodsSearchPage extends State with WidgetsBindingObserver { final TextEditingController editingController = TextEditingController(); final RefreshController refreshController = RefreshController(); final ScrollController scrollController = ScrollController(); int optionIndex = 0; bool isKeyBoardShow = false; List productListBeans = []; List hotSearch = []; List historySearch = []; FocusNode _focusNode = FocusNode(); bool hasFocus = true; int priceOrder = 0; int _pageIndex = 1; BusinessApiService businessService; List adminProductVoList = []; String networkError = ""; int networkStatus = 0; @override void didChangeMetrics() { super.didChangeMetrics(); 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; } }); }); } ///离开页面记着销毁和清除 @override void dispose() { _focusNode.unfocus(); super.dispose(); } @override void initState() { super.initState(); _focusNode.addListener(() { setState(() { hasFocus = _focusNode.hasFocus; }); }); _onRefresh(); } _onRefresh({bool isShowLoad = true}) async { if (isShowLoad) EasyLoading.show( status: S.current.zhengzaijiazai, maskType: EasyLoadingMaskType.black); await queryGoodsList(); EasyLoading.dismiss(); if (!mounted) return; if (refreshController.isRefresh) refreshController.refreshCompleted(); setState(() {}); } ///商品列表 queryGoodsList() 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.findAdminProductLis({ "groupId": "", "hasStock": "", "keyword": editingController?.text ?? "", "pageIndex": _pageIndex, "pageSize": 10, "status": widget.arguments["onSaleStatus"], }).catchError((error) { networkError = AppUtils.dioErrorTypeToString(error.type); networkStatus = -1; setState(() {}); refreshController.refreshFailed(); refreshController.loadFailed(); }); if (!mounted) return; if (baseData != null && baseData.isSuccess) { adminProductVoList.addAll(baseData?.data?.adminProductVOList ?? []); if ((baseData?.data?.adminProductVOList ?? []).isEmpty || adminProductVoList.length == baseData.data.total) refreshController.loadNoData(); else refreshController.loadComplete(); networkStatus = 1; } else { SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } ///商品下架 queryGoodsUpdate(productId) async { try { EasyLoading.show( status: S.current.zhengzaijiazai, maskType: EasyLoadingMaskType.black); if (businessService == null) { businessService = BusinessApiService(Dio(), context: context, token: BusinessInstance.instance.businessToken, tenant: BusinessInstance.instance.businessTenant, storeId: widget.arguments["storeId"]); } BaseData baseData = await businessService.goodsUpdate({ "productId": productId, "sell": widget.arguments["onSaleStatus"] == "1" ? false : true, }).catchError((error) {}); if (baseData != null && baseData.isSuccess) { _pageIndex = 1; adminProductVoList.clear(); await _onRefresh(isShowLoad: false); SmartDialog.showToast( widget.arguments["onSaleStatus"] == "1" ? "商品下架成功" : "商品上架成功", alignment: Alignment.center); } 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( appBar: MyAppBar( title: "搜索", titleColor: Colors.black, leadingColor: Colors.black, background: Colors.white, ), body: Column( children: [ Container( color: Colors.white, margin: EdgeInsets.only(bottom: 16.h), child: Container( height: 40.h, margin: EdgeInsets.fromLTRB(18.w, 17.h, 18.w, 10.h), alignment: Alignment.center, decoration: BoxDecoration( color: Color(0xFFF7F8FA), borderRadius: BorderRadius.circular(2), ), child: TextField( textInputAction: TextInputAction.search, onEditingComplete: () { FocusScope.of(context).requestFocus(FocusNode()); _pageIndex = 1; adminProductVoList.clear(); _onRefresh(); }, controller: editingController, style: TextStyle( fontSize: 14.sp, ), decoration: InputDecoration( hintText: "请输入搜索内容", hintStyle: TextStyle( fontSize: 15.sp, color: Color(0xFF808080), fontWeight: MyFontWeight.regular), isCollapsed: true, prefixIcon: Padding( padding: EdgeInsets.only(left: 5.w, right: 5.w), child: Image.asset( "assets/image/icon_search.webp", width: 20.h, height: 20.h, ), ), prefixIconConstraints: BoxConstraints(), border: InputBorder.none, ), ), ), ), Expanded( child: networkStatus == -1 ? noNetwork() : Container( child: SmartRefresher( controller: refreshController, enablePullDown: true, enablePullUp: true, physics: BouncingScrollPhysics(), header: MyHeader( color: Color(0xFF30415B), ), footer: CustomFooter( builder: (context, mode) { return MyFooter(mode); }, ), onRefresh: () { _pageIndex = 1; adminProductVoList.clear(); _onRefresh(isShowLoad: false); }, onLoading: () { _pageIndex++; _onRefresh(isShowLoad: false); }, child: networkStatus == 0 ? ListView.builder( padding: EdgeInsets.zero, itemCount: 10, scrollDirection: Axis.vertical, shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemBuilder: (context, position) { return searchGoodsItemSm(); }, ) : ((adminProductVoList == null || adminProductVoList.length == 0) ? NoDataView( src: "assets/image/bs_no data_logo.webp", isShowBtn: false, text: "未搜索到相关数据", fontSize: 16.sp, margin: EdgeInsets.all(20.h), ) : Container( margin: EdgeInsets.only(bottom: 20.h), child: ListView.builder( padding: EdgeInsets.zero, itemCount: adminProductVoList?.length ?? 0, scrollDirection: Axis.vertical, shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemBuilder: (context, position) { return searchGoodsItem( adminProductVoList[position]); }, ), )), ), ), ) ], ), ), ); } Widget searchGoodsItem(AdminProductVoList adminProductVoList) { return Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8), boxShadow: [ BoxShadow( color: Color(0x0F06152E).withAlpha(12), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], ), margin: EdgeInsets.only(bottom: 12.h, left: 16.w, right: 16.w), padding: EdgeInsets.only(left: 12.w, top: 12.h, bottom: 12.h, right: 24.w), child: Row( children: [ MImage( adminProductVoList?.productImg ?? "", width: 70.h, height: 70.h, fit: BoxFit.cover, radius: BorderRadius.circular(4), ), SizedBox( width: 12.w, ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: EdgeInsets.only(bottom: 11.h, top: 2.h), child: Text( adminProductVoList?.productName ?? "", maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 14.sp, fontWeight: MyFontWeight.medium, color: Color(0xFF000000), ), ), ), Row( children: [ Text( "库存${adminProductVoList?.stock ?? 0}", style: TextStyle( fontSize: 10.sp, fontWeight: MyFontWeight.regular, color: Color(0xFFA29E9E), ), ), SizedBox( width: 8.w, ), Text( "销量${adminProductVoList?.sellCount ?? 0}", style: TextStyle( fontSize: 10.sp, fontWeight: MyFontWeight.regular, color: Color(0xFF999999), ), ), ], ), SizedBox( height: 7.h, ), Row( children: [ Expanded( child: Text.rich( TextSpan( children: [ TextSpan( text: "¥", style: TextStyle( fontSize: 12.sp, fontWeight: MyFontWeight.medium, color: Color(0xFFF4524D), ), ), TextSpan( text: adminProductVoList?.price ?? "", style: TextStyle( fontSize: 14.sp, fontWeight: MyFontWeight.medium, color: Color(0xFFF4524D), ), ), ], ), )), GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { queryGoodsUpdate(adminProductVoList.id ?? ""); }, child: Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(33), border: Border.all( color: Color(0xFF30415B), width: 1, ), ), padding: EdgeInsets.symmetric( vertical: 2.h, horizontal: 12.w), child: Text( widget.arguments["onSaleStatus"] == "1" ? "下架" : "上架", style: TextStyle( fontSize: 12.sp, fontWeight: MyFontWeight.regular, color: Color(0xFF30415B), ), ), )) ], ) ], )), ], ), ); } Widget searchGoodsItemSm() { return Container( decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(8), boxShadow: [ BoxShadow( color: Color(0x0F06152E).withAlpha(12), offset: Offset(0, 2), blurRadius: 4, spreadRadius: 0, ) ], ), margin: EdgeInsets.only(bottom: 12.h, left: 16.w, right: 16.w), padding: EdgeInsets.only(left: 12.w, top: 12.h, bottom: 12.h, right: 24.w), child: Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(4), ), width: 70.h, height: 70.h, ), ), SizedBox( width: 12.w, ), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: EdgeInsets.only(bottom: 11.h, top: 2.h), child: Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 124.w, height: 20.h, ), ), ), Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 43.w, height: 18.h, ), ), SizedBox( width: 8.w, ), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 43.w, height: 18.h, ), ), ], ), SizedBox( height: 7.h, ), Row( children: [ Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 28.w, height: 20.h, ), ), Spacer(), Shimmer.fromColors( baseColor: Color(0XFFD8D8D8), highlightColor: Color(0XFFD8D8D8), child: Container( decoration: BoxDecoration( color: Color(0XFFD8D8D8), borderRadius: BorderRadius.circular(2), ), width: 24.w, height: 18.h, ), ), ], ) ], )), ], ), ); } Widget noNetwork() { return Container( color: Colors.white, width: double.infinity, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( networkError.substring(0, 4), style: TextStyle( fontSize: 14.sp, color: Color(0xFF0D0D0D), fontWeight: MyFontWeight.bold), ), Padding( padding: EdgeInsets.symmetric(vertical: 10.h), child: Text( "请检查网络设置或稍后重试", style: TextStyle( fontSize: 12.sp, color: Color(0xFF7A797F), fontWeight: MyFontWeight.regular), ), ), GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { _onRefresh(); }, child: Container( decoration: BoxDecoration( color: Color(0xFF30415B), borderRadius: BorderRadius.circular(15), ), padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 3.h), child: Text( "重试", style: TextStyle( fontSize: 14.sp, color: Colors.white, fontWeight: MyFontWeight.regular), )), ) ], ), ); } }