Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

292 lines
11 KiB

  1. import 'package:chat/data/UserData.dart';
  2. import 'package:chat/data/group_data_mgr.dart';
  3. import 'package:chat/models/friends_info.dart';
  4. import 'package:chat/models/group_info_model.dart';
  5. import 'package:chat/utils/HttpUtil.dart';
  6. import 'package:chat/utils/conversation_table.dart';
  7. import 'package:chat/utils/friend_list_mgr.dart';
  8. import 'package:chat/utils/screen.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:chat/data/constants.dart';
  11. import 'package:chat/generated/i18n.dart';
  12. import 'package:chat/utils/CustomUI.dart';
  13. import 'package:flutter/services.dart';
  14. import 'ProfilePage.dart';
  15. import 'group_item_widget.dart';
  16. import 'package:chat/models/ref_name_provider.dart';
  17. import 'package:provider/provider.dart';
  18. class GlobalSearchPage extends StatefulWidget {
  19. final int type;
  20. GlobalSearchPage({Key key, @required this.type}) : super(key: key);
  21. @override
  22. _GlobalSearchPageState createState() => _GlobalSearchPageState();
  23. }
  24. class _GlobalSearchPageState extends State<GlobalSearchPage> {
  25. ScrollController _scrollController = ScrollController();
  26. TextEditingController _txtCtrl = new TextEditingController();
  27. bool _hasdeleteIcon = false;
  28. FocusNode focusNode;
  29. List searchList = [];
  30. String searchStr = '';
  31. @override
  32. void initState() {
  33. super.initState();
  34. _scrollController = new ScrollController();
  35. focusNode = new FocusNode();
  36. }
  37. @override
  38. void dispose() {
  39. _scrollController.dispose();
  40. focusNode.dispose();
  41. super.dispose();
  42. }
  43. //群搜索结果
  44. Widget _buildSearchGroupBody() {
  45. return ListView.builder(
  46. controller: _scrollController,
  47. itemBuilder: (BuildContext context, int index) {
  48. GroupInfoModel _contact = searchList[index];
  49. return GroupItem(Key('Item${_contact.sessionId}'),
  50. groupInfoModel: _contact);
  51. },
  52. itemCount: searchList.length,
  53. );
  54. }
  55. //好友搜索结果
  56. Widget _buildSearchMyfriendsBody() {
  57. return ListView.builder(
  58. controller: _scrollController,
  59. itemBuilder: (BuildContext context, int index) {
  60. FriendModel _contact = searchList[index];
  61. return FriendsInfo(
  62. userId: _contact.friendId,
  63. avatar: _contact.avatar,
  64. title: Provider.of<RefNameProvider>(context)
  65. .getRefName(_contact.friendId, _contact.name),
  66. isShowDivder: true,
  67. groupTitle: null);
  68. },
  69. itemCount: searchList.length,
  70. );
  71. }
  72. //通过手机号搜索新用户结果
  73. Widget _buildSearchNewfriendsBody() {
  74. if (searchStr == '') return Container();
  75. return InkWell(
  76. onTap: () {
  77. HttpUtil().searchNewFrindsByPhone(searchStr, (int userId) {
  78. if (userId == null || userId == 0) {
  79. CustomUI.buildOneConfirm(context, I18n.of(context).not_have_user,
  80. I18n.of(context).determine, () {
  81. Navigator.of(context).pop();
  82. });
  83. return;
  84. }
  85. if (userId == UserData().basicInfo.userId) {
  86. CustomUI.buildOneConfirm(context, I18n.of(context).not_add_Myself,
  87. I18n.of(context).determine, () {
  88. Navigator.of(context).pop();
  89. });
  90. return;
  91. }
  92. Navigator.of(context).push(
  93. new MaterialPageRoute(
  94. builder: (context) {
  95. return ProfilePage(userId: userId, addMode: 1);
  96. },
  97. ),
  98. );
  99. });
  100. },
  101. child: Container(
  102. margin: EdgeInsets.only(top: 9),
  103. height: 55.1,
  104. width: Screen.width,
  105. child: Row(
  106. mainAxisAlignment: MainAxisAlignment.center,
  107. children: <Widget>[
  108. Container(
  109. margin: EdgeInsets.only(right: 13),
  110. width: Constants.ContactAvatarSize,
  111. height: Constants.ContactAvatarSize,
  112. decoration: BoxDecoration(
  113. gradient: LinearGradient(
  114. begin: Alignment.topCenter,
  115. end: Alignment.bottomCenter,
  116. colors: <Color>[
  117. const Color(0xFF58B7F5),
  118. const Color(0xFF1874C9),
  119. ]),
  120. borderRadius: BorderRadius.circular(6)),
  121. child: Icon(
  122. IconData(0xe662, fontFamily: Constants.IconFontFamily),
  123. color: Colors.white,
  124. ),
  125. ),
  126. Text('${I18n.of(context).search_phone}: ',
  127. style: TextStyle(fontSize: 16.23)),
  128. Text(searchStr,
  129. style: TextStyle(fontSize: 16.23, color: Color(0xFF008AFF)))
  130. ],
  131. ),
  132. color: Colors.white));
  133. }
  134. Widget buildBody() {
  135. Widget result = Container();
  136. switch (widget.type) {
  137. case GlobalSearchPageType.SearchGroup:
  138. result = _buildSearchGroupBody();
  139. break;
  140. case GlobalSearchPageType.SearchMyFriends:
  141. result = _buildSearchMyfriendsBody();
  142. break;
  143. case GlobalSearchPageType.SearchNewFriends:
  144. result = _buildSearchNewfriendsBody();
  145. break;
  146. default:
  147. }
  148. return result;
  149. }
  150. //开始搜索群
  151. _beginSearchGroup(String str) {
  152. Map refMap = Provider.of<RefNameProvider>(context).refMap;
  153. List<GroupInfoModel> groupList = GroupInfoMgr().groupInfoList;
  154. searchList = CustomUI()
  155. .getGroupSearchResult(str, groupList == null ? [] : groupList, refMap);
  156. }
  157. //开始搜索自己好友
  158. _beginSearchMyFriends(String str) async {
  159. List<FriendModel> friendList = [];
  160. friendList = await FriendListMgr().getFriendList();
  161. searchList =
  162. CustomUI().getSearchResult(str, friendList == null ? [] : friendList);
  163. }
  164. beginSearch(String str) {
  165. switch (widget.type) {
  166. case GlobalSearchPageType.SearchGroup:
  167. _beginSearchGroup(str);
  168. break;
  169. case GlobalSearchPageType.SearchMyFriends:
  170. _beginSearchMyFriends(str);
  171. break;
  172. default:
  173. }
  174. }
  175. @override
  176. Widget build(BuildContext context) {
  177. final List<Widget> _body = [];
  178. _body.add(buildBody());
  179. return Scaffold(
  180. resizeToAvoidBottomPadding: false,
  181. appBar: AppBar(
  182. backgroundColor: AppColors.NewAppbarBgColor,
  183. title: Text(I18n.of(context).search,
  184. textScaleFactor: 1.0, style: Constants.MainTitleStyle),
  185. centerTitle: true,
  186. elevation: 1,
  187. leading: CustomUI.buildCustomLeading(context),
  188. bottom: PreferredSize(
  189. preferredSize: Size.fromHeight(49),
  190. child: Container(
  191. alignment: Alignment.center,
  192. margin: EdgeInsets.only(bottom: 14, left: 12.5, right: 12.5),
  193. height: 35,
  194. decoration: BoxDecoration(
  195. color: const Color(0xFFEEEEEE),
  196. borderRadius: BorderRadius.all(Radius.circular(8))),
  197. child: TextField(
  198. keyboardType: TextInputType.text,
  199. cursorColor: Constants.BlueTextColor,
  200. textInputAction: TextInputAction.search,
  201. controller: _txtCtrl,
  202. maxLines: 1,
  203. focusNode: focusNode,
  204. style: TextStyle(
  205. textBaseline: TextBaseline.alphabetic, fontSize: 14.5),
  206. autofocus: true,
  207. inputFormatters:
  208. widget.type == GlobalSearchPageType.SearchNewFriends
  209. ? [
  210. WhitelistingTextInputFormatter.digitsOnly,
  211. LengthLimitingTextInputFormatter(15),
  212. ]
  213. : [
  214. LengthLimitingTextInputFormatter(50),
  215. ],
  216. decoration: InputDecoration(
  217. hintText: I18n.of(context).search,
  218. hintStyle: TextStyle(fontSize: 14.5),
  219. contentPadding: EdgeInsets.only(
  220. left: 20,
  221. top: (UserData().language == LanguageType.English ||
  222. UserData().language ==
  223. LanguageType.Vietnamese)
  224. ? 3
  225. : 10.5,
  226. bottom: 10.5),
  227. prefixIcon: Icon(
  228. IconData(
  229. 0xe664,
  230. fontFamily: Constants.IconFontFamily,
  231. ),
  232. color: const Color(0xFFA0A0A0),
  233. size: 18,
  234. ),
  235. suffixIcon: Padding(
  236. padding: EdgeInsetsDirectional.only(
  237. start: 2.0, end: _hasdeleteIcon ? 20.0 : 0),
  238. child: _hasdeleteIcon
  239. ? new InkWell(
  240. onTap: (() {
  241. setState(() {
  242. WidgetsBinding.instance
  243. .addPostFrameCallback(
  244. (_) => _txtCtrl.clear());
  245. _hasdeleteIcon = false;
  246. });
  247. }),
  248. child: Icon(
  249. Icons.clear,
  250. size: 18.0,
  251. color: Constants.BlackTextColor,
  252. ))
  253. : new Text('')),
  254. ),
  255. onChanged: (str) async {
  256. setState(() {
  257. searchStr = str;
  258. if (str.isEmpty) {
  259. _hasdeleteIcon = false;
  260. } else {
  261. _hasdeleteIcon = true;
  262. beginSearch(str);
  263. }
  264. });
  265. },
  266. onEditingComplete: () {
  267. FocusScope.of(context).requestFocus(FocusNode());
  268. }),
  269. )),
  270. ),
  271. body: Stack(
  272. children: _body,
  273. ));
  274. }
  275. }