import 'dart:ui'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../retrofit/data/base_data.dart'; import '../retrofit/data/im_user.dart'; import '../retrofit/retrofit_api.dart'; import '../utils/font_weight.dart'; import '../view_widget/my_appbar.dart'; import '../view_widget/settlement_tips_dialog.dart'; class ImSearch extends StatefulWidget { @override State createState() { return _ImSearch(); } } class _ImSearch extends State { ApiService apiService; final TextEditingController editingController = TextEditingController(); FocusNode _focusNode = FocusNode(); List searchUser = []; int searchState = 0; int textType = 0; int searchUserIndex = 0; @override void initState() { super.initState(); } ///离开页面记着销毁和清除 @override void dispose() { _focusNode.unfocus(); super.dispose(); } ///搜索列表 queryImSearch(keyword) async { if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService( Dio(), context: context, token: value.getString("token"), showLoading:false ); } BaseData> baseData = await apiService.memberSearch(keyword).catchError((onError) {}); if (baseData != null && baseData.isSuccess) { searchUser.clear(); baseData.data.forEach((element) { if(element.phone != "" && element.nickname != ""){ searchUser.add(element); } }); searchState = 1; if(baseData.data.length == 0){ searchState = 2; } setState(() {}); } } @override Widget build(BuildContext context) { return GestureDetector( onTap:(){ FocusScope.of(context).requestFocus(FocusNode()); }, child: Scaffold( backgroundColor: Color(0xFFFFFFFF), resizeToAvoidBottomInset: false, appBar: MyAppBar( title: "", leading: true, leadingColor: Colors.black, background: Color(0xFFFFFFFF), ), body: Container( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ imSearch(), searchState == 2 ? Center( child: Text( "未找到该用户", style: TextStyle( fontSize: 14.sp, color: Color(0xFFA29E9E), ), ), ): Expanded(child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ if(searchState ==1) Padding(padding:EdgeInsets.only(left:16.w), child: Text( "搜索用户:", textAlign: TextAlign.center, style: TextStyle( color: Color(0xFF060606), fontSize:16.sp, fontWeight: MyFontWeight.medium, ), ),), if(searchState ==1) Expanded( child: ListView.builder( itemCount: searchUser?.length ?? 0, physics: BouncingScrollPhysics(), shrinkWrap: true, itemBuilder: (context, position) { return GestureDetector( behavior: HitTestBehavior.opaque, onTap:(){ setState(() { searchUserIndex = position; }); Navigator.of(context).pushNamed('/router/personal_page', arguments: { "memberId":searchUser[searchUserIndex].mid ?? "", }); }, child: imSearchItem(searchUser[position]), ); }, )) ], )), ], ), ), ), ); } ///搜索 Widget imSearch() { return Container( margin: EdgeInsets.fromLTRB(16.w, 8.h, 16.w,29.h), padding: EdgeInsets.symmetric(vertical: 13.h), decoration: BoxDecoration( color: Color(0xFFFDFCFC), borderRadius: BorderRadius.circular(4), ), child: TextField( textInputAction: TextInputAction.search, onChanged: (value){ setState(() { searchState =3; }); if (isNumeric(value)) { textType = 1; } else { textType = 2; } if(editingController.text == null || editingController.text == ""){ return; }else{ queryImSearch(editingController.text ?? ""); } }, onEditingComplete: () { FocusScope.of(context).requestFocus(FocusNode()); if(editingController.text == null || editingController.text == ""){ SmartDialog.show( widget: SettlementTips( () {}, text: "请输入姓名或手机号搜索", )); }else{ queryImSearch(editingController.text ?? ""); } }, controller: editingController, style: TextStyle( fontSize: 14.sp, ), decoration: InputDecoration( hintText: "输入姓名或手机号搜索", hintStyle: TextStyle( fontSize: 14.sp, color: Color(0xFFA29E9E), ), isCollapsed: true, prefixIcon: Padding( padding: EdgeInsets.only(left: 15.w, right: 5.w), child: Image.asset( "assets/image/icon_search.webp", width: 14.h, height: 14.h, color: Color(0xFFB3B3B3), ), ), prefixIconConstraints: BoxConstraints(), border: InputBorder.none, ), ), ); } ///搜索列表 Widget imSearchItem(ImUser searchUser) { return Container( padding: EdgeInsets.only(left:10.w,right:16.w,bottom:15.h), child: Text( textType == 1 ?(searchUser?.phone?? "") : (searchUser?.nickname ?? ""), style: TextStyle( fontSize: 16.sp, color: Color(0xFF32A060), fontWeight:MyFontWeight.regular), ), ); } /// 判断给的字符串是否全部由数字组成 bool isNumeric(String str) { RegExp regExp = RegExp(r'^\d+$'); return regExp.hasMatch(str); } }