From a84c63ecf6a8f1bc25c0de67cbc738e4f1103edd Mon Sep 17 00:00:00 2001 From: w-R <953969641@qq.com> Date: Tue, 29 Mar 2022 22:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=9F=8E=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shopping/shopping_home/search_page.dart | 450 +++++++++--------- 1 file changed, 235 insertions(+), 215 deletions(-) diff --git a/lib/store/shopping/shopping_home/search_page.dart b/lib/store/shopping/shopping_home/search_page.dart index 23895e3e..fc96612f 100644 --- a/lib/store/shopping/shopping_home/search_page.dart +++ b/lib/store/shopping/shopping_home/search_page.dart @@ -21,13 +21,15 @@ class SearchPage extends StatefulWidget { final Map arguments; SearchPage({this.arguments}); + @override State createState() { return _SearchPage(); } } -class _SearchPage extends State with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { +class _SearchPage extends State + with AutomaticKeepAliveClientMixin, WidgetsBindingObserver { ApiService apiService; MinApiService minService; final TextEditingController editingController = TextEditingController(); @@ -35,7 +37,7 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, int optionIndex = 0; bool isKeyBoardShow = false; List productListBeans = []; - List hotSearch =[]; + List hotSearch = []; List historySearch = []; FocusNode _focusNode = FocusNode(); bool hasFocus = true; @@ -71,17 +73,17 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, void initState() { super.initState(); setState(() { - if(widget.arguments["hotSearch"] != null) + if (widget.arguments["hotSearch"] != null) hotSearch.addAll(widget.arguments["hotSearch"]); }); - _focusNode.addListener((){ + _focusNode.addListener(() { setState(() { hasFocus = _focusNode.hasFocus; }); }); - if(widget.arguments["searchKey"] != null){ + if (widget.arguments["searchKey"] != null) { setState(() { - editingController.text =widget.arguments["searchKey"]; + editingController.text = widget.arguments["searchKey"]; }); appletGoods(widget.arguments["searchKey"]); } @@ -89,11 +91,10 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, } ///获取保存列表 - getHistorySearch()async{ + getHistorySearch() async { SharedPreferences value = await SharedPreferences.getInstance(); - var str = value.getString("historySearch"); - if(str == null ) - return; + var str = value.getString("historySearch"); + if (str == null) return; var strArr = str.split("&"); strArr.removeAt(strArr.length - 1); setState(() { @@ -103,17 +104,15 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, } ///保存列表 - setHistorySearch(String text)async{ - if(text == null || text.trim() == "") - return; + setHistorySearch(String text) async { + if (text == null || text.trim() == "") return; SharedPreferences value = await SharedPreferences.getInstance(); - var str = value.getString("historySearch"); - if(str == null ) - str = ""; + var str = value.getString("historySearch"); + if (str == null) str = ""; var strArr = str.split("&"); - if(strArr.length >30){ - str = strArr.getRange(0, 29).join("&"); - } else{ + if (strArr.length > 60) { + str = strArr.getRange(0, 59).join("&"); + } else { str = strArr.join("&"); } str += text + "&"; @@ -122,43 +121,52 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, } ///删除历史列表 - delHistorySearch()async{ + delHistorySearch() async { SharedPreferences value = await SharedPreferences.getInstance(); - value.setString("historySearch", ""); - getHistorySearch(); + value.setString("historySearch", ""); + getHistorySearch(); } ///小程序查询分组及商品列表 appletGoods(String text) async { - if(editingController.text.trim() == ""){ + if (editingController.text.trim() == "") { return; } - if(minService == null) - minService = MinApiService( - Dio(), - context: context, - token: widget.arguments["minToken"], - tenant:widget.arguments["tenant"], - storeId: widget.arguments["storeId"], - ); + if (minService == null) + minService = MinApiService( + Dio(), + context: context, + token: widget.arguments["minToken"], + tenant: widget.arguments["tenant"], + storeId: widget.arguments["storeId"], + ); - BaseData> baseData = - await minService.findMiNiGroupList({ - "id":widget.arguments["storeId"], - "searchKey" :text, - "sortType":optionIndex == 0 ? "" : (optionIndex == 1) ? productListBeans[0].sellCount : productListBeans[0].price, - "asc":optionIndex == 0 ? "" :(priceOrder == 2) ? true:"" - }).catchError((error) { - }).catchError((error) { - refreshController.refreshFailed(); - }); + BaseData> baseData = await minService + .findMiNiGroupList({ + "id": widget.arguments["storeId"], + "searchKey": text, + "sortType": optionIndex == 0 + ? "" + : (optionIndex == 1) + ? productListBeans[0].sellCount + : productListBeans[0].price, + "asc": optionIndex == 0 + ? "" + : (priceOrder == 2) + ? true + : "" + }) + .catchError((error) {}) + .catchError((error) { + refreshController.refreshFailed(); + }); if (baseData != null && baseData.isSuccess) { refreshController.refreshCompleted(); setState(() { - productListBeans.clear(); - if(baseData.data.length > 0) - productListBeans.addAll(baseData.data[0].productList); + productListBeans.clear(); + if (baseData.data.length > 0) + productListBeans.addAll(baseData.data[0].productList); }); } else { refreshController.refreshFailed(); @@ -171,7 +179,7 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, }); } - _onRefresh(){ + _onRefresh() { appletGoods(editingController.text); refreshController.refreshCompleted(); } @@ -226,67 +234,66 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ), ), Expanded( - child: - Container( - height: 36.h, - margin: EdgeInsets.fromLTRB(6.w, 0, 14.w, 0), - padding: EdgeInsets.fromLTRB(0, 6.h, 0, 6.h), - decoration: BoxDecoration( - color: Colors.white, - borderRadius: BorderRadius.circular(4), - boxShadow: [ - BoxShadow( - color: Colors.black.withAlpha(12), - offset: Offset(0, 3), - blurRadius: 14, - spreadRadius: 0, - ), - ], + child: Container( + height: 36.h, + margin: EdgeInsets.fromLTRB(6.w, 0, 14.w, 0), + padding: EdgeInsets.fromLTRB(0, 6.h, 0, 6.h), + decoration: BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.circular(4), + boxShadow: [ + BoxShadow( + color: Colors.black.withAlpha(12), + offset: Offset(0, 3), + blurRadius: 14, + spreadRadius: 0, ), - child: TextField( - focusNode:_focusNode, - textInputAction: TextInputAction.search, - style: TextStyle( - fontSize: 12.sp, - fontWeight: MyFontWeight.regular, - color: Color(0xFF292929), - ), - onEditingComplete: () { - // startLocation(); + ], + ), + child: TextField( + focusNode: _focusNode, + textInputAction: TextInputAction.search, + style: TextStyle( + fontSize: 12.sp, + fontWeight: MyFontWeight.regular, + color: Color(0xFF292929), + ), + onEditingComplete: () { + // startLocation(); + }, + controller: editingController, + // cursorHeight: 30.h, + decoration: InputDecoration( + contentPadding: EdgeInsets.symmetric( + vertical: 12.h, + ), + prefixIcon: Icon( + Icons.search, + size: 24, + color: Colors.black, + ), + suffixIcon: InkWell( + onTap: () { + editingController.clear(); }, - controller: editingController, - // cursorHeight: 30.h, - decoration: InputDecoration( - contentPadding: EdgeInsets.symmetric( - vertical: 12.h, - ), - prefixIcon: Icon( - Icons.search, - size: 24, - color: Colors.black, - ), - suffixIcon: InkWell( - onTap: () { - editingController.clear(); - }, - child: Icon( - Icons.close, - size: 19, - color: Colors.grey, - ), - ), - border: InputBorder.none, + child: Icon( + Icons.close, + size: 19, + color: Colors.grey, ), ), - ) - ), + border: InputBorder.none, + ), + ), + )), Container( alignment: Alignment.center, child: GestureDetector( onTap: () { setState(() { - if(editingController.text.trim() == ""){ - SmartDialog.showToast("请输入搜索内容", alignment: Alignment.center); + if (editingController.text.trim() == "") { + SmartDialog.showToast("请输入搜索内容", + alignment: Alignment.center); return; } appletGoods(editingController.text); @@ -326,26 +333,24 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, header: MyHeader(), physics: BouncingScrollPhysics(), footer: CustomFooter( - builder: (context, mode) { - return MyFooter(mode); + builder: (context, mode) { + return MyFooter(mode); + }, + ), + onRefresh: () { + setState(() { + _onRefresh(); + }); }, - ),onRefresh: () { - setState(() { - _onRefresh(); - }); - }, child: SingleChildScrollView( physics: NeverScrollableScrollPhysics(), child: FutureBuilder( builder: (context, snapshot) { return Column( children: [ - if(hasFocus) - hotSearchWords(), - if(hasFocus) - historySearchWords(), - if(!hasFocus) - searchList() + if (hasFocus) hotSearchWords(), + if (hasFocus) historySearchWords(), + if (!hasFocus) searchList() ], ); }, @@ -365,7 +370,6 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ///热门搜索 Widget hotSearchWords() { return Container( - width: double.infinity, child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, @@ -380,13 +384,13 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ), SizedBox(height: 16.h), GridView.builder( - itemCount:hotSearch?.length ?? 0, + itemCount: hotSearch?.length ?? 0, shrinkWrap: true, padding: EdgeInsets.zero, physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( //一行的Widget数量 - crossAxisCount:4, + crossAxisCount: 4, //水平子Widget之间间距 crossAxisSpacing: 6.w, //垂直子Widget之间间距 @@ -398,8 +402,8 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, return GestureDetector( onTap: () { setState(() { - if(hotSearch[index] != null){ - editingController.text =hotSearch[index]; + if (hotSearch[index] != null) { + editingController.text = hotSearch[index]; appletGoods(hotSearch[index]); } }); @@ -415,6 +419,7 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, Widget hotSearchItem(String hotSearch) { return Container( + width: double.infinity, child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.start, @@ -422,7 +427,7 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, RoundButton( padding: EdgeInsets.only(left: 4, right: 4), height: 29.h, - text:hotSearch, + text: hotSearch, textColor: Color(0xFF181818), fontWeight: MyFontWeight.regular, radius: 4, @@ -437,7 +442,7 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ///历史搜索 Widget historySearchWords() { return Container( - width: double.infinity, + // width: double.infinity, child: Column( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, @@ -459,12 +464,12 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, height: 20.h, ), GestureDetector( - onTap: (){ + onTap: () { setState(() { delHistorySearch(); }); }, - child:Text( + child: Text( "删除", style: TextStyle( fontSize: 15.sp, @@ -477,13 +482,13 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ), SizedBox(height: 16.h), GridView.builder( - itemCount:historySearch?.length ?? 0, + itemCount: historySearch?.length ?? 0, shrinkWrap: true, padding: EdgeInsets.zero, physics: NeverScrollableScrollPhysics(), gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( //一行的Widget数量 - crossAxisCount:4, + crossAxisCount: 4, //水平子Widget之间间距 crossAxisSpacing: 6.w, //垂直子Widget之间间距 @@ -495,13 +500,13 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, return GestureDetector( onTap: () { setState(() { - if(historySearch[index] != null){ - editingController.text =historySearch[index]; + if (historySearch[index] != null) { + editingController.text = historySearch[index]; appletGoods(historySearch[index]); } }); }, - child:historySearchItem(historySearch[index]), + child: historySearchItem(historySearch[index]), ); }, ), @@ -516,18 +521,25 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.start, children: [ - Expanded(child: - RoundButton( - padding: EdgeInsets.only(left: 4, right: 4), - height: 29.h, - text:historySearch, - textColor: Color(0xFF181818), - fontWeight: MyFontWeight.regular, - radius: 4, - backgroup: Color(0xFFF5F5F5), - fontSize: 12.sp, - ),) - + Expanded( + child: Container( + padding: EdgeInsets.only(left: 4, right: 4), + alignment: Alignment.center, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(4), + color: Color(0xFFF5F5F5)), + child: Text( + historySearch, + overflow: TextOverflow.ellipsis, + maxLines: 1, + style: TextStyle( + fontSize: 12.sp, + fontWeight: MyFontWeight.regular, + color: Color(0xFF181818), + ), + ), + ), + ) ], ), ); @@ -589,8 +601,8 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, onTap: () { setState(() { optionIndex = 2; - priceOrder = 1; - appletGoods(widget.arguments["searchKey"]); + // priceOrder = 1; + // appletGoods(widget.arguments["searchKey"]); }); }, child: Row( @@ -610,24 +622,28 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, Column( children: [ GestureDetector( - onTap: (){ + onTap: () { priceOrder = 1; appletGoods(widget.arguments["searchKey"]); }, - child:Icon( + child: Icon( Icons.arrow_drop_up, - color: (optionIndex == 2 && priceOrder == 1) ? Color(0xFF33A061) : Color(0xFF4D4D4D), + color: (optionIndex == 2 && priceOrder == 1) + ? Color(0xFF33A061) + : Color(0xFF4D4D4D), size: 16, ), ), GestureDetector( - onTap: (){ + onTap: () { priceOrder = 2; appletGoods(widget.arguments["searchKey"]); }, - child:Icon( + child: Icon( Icons.arrow_drop_down, - color: (optionIndex == 2 && priceOrder == 2) ? Color(0xFF33A061) : Color(0xFF4D4D4D), + color: (optionIndex == 2 && priceOrder == 2) + ? Color(0xFF33A061) + : Color(0xFF4D4D4D), size: 16, ), ) @@ -663,45 +679,45 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ], ), SizedBox(height: 16.h), - (productListBeans == null || productListBeans?.length == 0) + (productListBeans == null || productListBeans?.length == 0) ? NoDataView( - src:"assets/image/ding_dan.png", - isShowBtn: false, - text:"该关键词未搜索到相关商品~", - fontSize: 16.sp, - margin: EdgeInsets.only(top: 120.h,left: 60.w,right: 60.w), - ): - GridView.builder( - itemCount:productListBeans?.length ?? 0, - shrinkWrap: true, - physics: NeverScrollableScrollPhysics(), - gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( - //一行的Widget数量 - crossAxisCount: 2, - //水平子Widget之间间距 - crossAxisSpacing: 11.w, - //垂直子Widget之间间距 - mainAxisSpacing: 16.w, - //子Widget宽高比例 0.59 - childAspectRatio: - 185 / (281 / 2 + (281 / 2) * AppUtils.textScale(context)), - ), - itemBuilder: (context, index) { - return GestureDetector( - onTap: () { - Navigator.of(context).pushNamed( - '/router/shopping_goods_details', - arguments: { - "id":productListBeans[index].id, - "storeId":widget.arguments["storeId"], - "tenant":widget.arguments["tenant"], - }, - ); - }, - child: searchListItem(productListBeans[index]), - ); - }, - ) + src: "assets/image/ding_dan.png", + isShowBtn: false, + text: "该关键词未搜索到相关商品~", + fontSize: 16.sp, + margin: EdgeInsets.only(top: 120.h, left: 60.w, right: 60.w), + ) + : GridView.builder( + itemCount: productListBeans?.length ?? 0, + shrinkWrap: true, + physics: NeverScrollableScrollPhysics(), + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( + //一行的Widget数量 + crossAxisCount: 2, + //水平子Widget之间间距 + crossAxisSpacing: 11.w, + //垂直子Widget之间间距 + mainAxisSpacing: 16.w, + //子Widget宽高比例 0.59 + childAspectRatio: 185 / + (281 / 2 + (281 / 2) * AppUtils.textScale(context)), + ), + itemBuilder: (context, index) { + return GestureDetector( + onTap: () { + Navigator.of(context).pushNamed( + '/router/shopping_goods_details', + arguments: { + "id": productListBeans[index].id, + "storeId": widget.arguments["storeId"], + "tenant": widget.arguments["tenant"], + }, + ); + }, + child: searchListItem(productListBeans[index]), + ); + }, + ) ], ), ); @@ -739,15 +755,14 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ) ], ), - child: - MImage( - productListBeans?.imgPath ??"", - width: double.infinity, - height: 166, - fit: BoxFit.cover, - errorSrc: "assets/image/default_1.png", - fadeSrc: "assets/image/default_1.png", - ), + child: MImage( + productListBeans?.imgPath ?? "", + width: double.infinity, + height: 166, + fit: BoxFit.cover, + errorSrc: "assets/image/default_1.png", + fadeSrc: "assets/image/default_1.png", + ), ), Expanded( child: Container( @@ -760,35 +775,40 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.end, children: [ - Expanded(child:Container( - margin: EdgeInsets.only(top: 8, right: 8), - padding: EdgeInsets.only(left: 2, right: 2), - height: 17.h, - alignment: Alignment.center, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(2), - color: Color(0xFF5A5A5A), + Expanded( + child: Container( + margin: EdgeInsets.only(top: 8, right: 8), + padding: EdgeInsets.only(left: 2, right: 2), + height: 17.h, + alignment: Alignment.center, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(2), + color: Color(0xFF5A5A5A), + ), + child: Text( + productListBeans.supplierName, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle( + fontSize: 10.sp, + fontWeight: MyFontWeight.regular, + color: Colors.white, + ), + ), ), + ), + Expanded( + child: Container( child: Text( - productListBeans.supplierName, - maxLines: 1, - overflow: TextOverflow.ellipsis, + "已售${productListBeans?.sellCount ?? 0}件", style: TextStyle( - fontSize: 10.sp, + fontSize: 12.sp, fontWeight: MyFontWeight.regular, - color: Colors.white, + color: Color(0xFF5D5D5D), ), ), - ),), - Expanded(child: - Container(child: Text( - "已售${productListBeans?.sellCount ?? 0}件", - style: TextStyle( - fontSize: 12.sp, - fontWeight: MyFontWeight.regular, - color: Color(0xFF5D5D5D), - ), - ),alignment: Alignment.topRight,)), + alignment: Alignment.topRight, + )), ], ), Text( @@ -817,7 +837,7 @@ class _SearchPage extends State with AutomaticKeepAliveClientMixin, ), ), TextSpan( - text:productListBeans?.price ?? "", + text: productListBeans?.price ?? "", style: TextStyle( fontSize: 18.sp, fontWeight: MyFontWeight.semi_bold,