Hibok
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

301 satır
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: 3);
  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.start,
  107. textBaseline: TextBaseline.ideographic,
  108. children: <Widget>[
  109. Container(
  110. margin: EdgeInsets.only(left: 20, right: 13),
  111. width: Constants.ContactAvatarSize,
  112. height: Constants.ContactAvatarSize,
  113. decoration: BoxDecoration(
  114. gradient: LinearGradient(
  115. begin: Alignment.topCenter,
  116. end: Alignment.bottomCenter,
  117. colors: <Color>[
  118. const Color(0xFF58B7F5),
  119. const Color(0xFF1874C9),
  120. ]),
  121. borderRadius: BorderRadius.circular(6)),
  122. child: Icon(
  123. IconData(0xe662, fontFamily: Constants.IconFontFamily),
  124. color: Colors.white,
  125. ),
  126. ),
  127. RichText(
  128. text: TextSpan(children: [
  129. TextSpan(
  130. text: '${I18n.of(context).search_phone}: ',
  131. style: TextStyle(
  132. color: Constants.BlackTextColor, fontSize: 16.23)),
  133. TextSpan(
  134. text: searchStr,
  135. style: TextStyle(
  136. color: Color(0xFF008AFF), fontSize: 16.23)),
  137. ]),
  138. ),
  139. ],
  140. ),
  141. color: Colors.white));
  142. }
  143. Widget buildBody() {
  144. Widget result = Container();
  145. switch (widget.type) {
  146. case GlobalSearchPageType.SearchGroup:
  147. result = _buildSearchGroupBody();
  148. break;
  149. case GlobalSearchPageType.SearchMyFriends:
  150. result = _buildSearchMyfriendsBody();
  151. break;
  152. case GlobalSearchPageType.SearchNewFriends:
  153. result = _buildSearchNewfriendsBody();
  154. break;
  155. default:
  156. }
  157. return result;
  158. }
  159. //开始搜索群
  160. _beginSearchGroup(String str) {
  161. Map refMap = Provider.of<RefNameProvider>(context).refMap;
  162. List<GroupInfoModel> groupList = GroupInfoMgr().groupInfoList;
  163. searchList = CustomUI()
  164. .getGroupSearchResult(str, groupList == null ? [] : groupList, refMap);
  165. }
  166. //开始搜索自己好友
  167. _beginSearchMyFriends(String str) async {
  168. List<FriendModel> friendList = [];
  169. friendList = await FriendListMgr().getFriendList();
  170. searchList =
  171. CustomUI().getSearchResult(str, friendList == null ? [] : friendList);
  172. }
  173. beginSearch(String str) {
  174. switch (widget.type) {
  175. case GlobalSearchPageType.SearchGroup:
  176. _beginSearchGroup(str);
  177. break;
  178. case GlobalSearchPageType.SearchMyFriends:
  179. _beginSearchMyFriends(str);
  180. break;
  181. default:
  182. }
  183. }
  184. @override
  185. Widget build(BuildContext context) {
  186. final List<Widget> _body = [];
  187. _body.add(buildBody());
  188. return Scaffold(
  189. resizeToAvoidBottomPadding: false,
  190. appBar: AppBar(
  191. backgroundColor: AppColors.NewAppbarBgColor,
  192. title: Text(I18n.of(context).search,
  193. textScaleFactor: 1.0, style: Constants.MainTitleStyle),
  194. centerTitle: true,
  195. elevation: 1,
  196. leading: CustomUI.buildCustomLeading(context),
  197. bottom: PreferredSize(
  198. preferredSize: Size.fromHeight(49),
  199. child: Container(
  200. alignment: Alignment.center,
  201. margin: EdgeInsets.only(bottom: 14, left: 12.5, right: 12.5),
  202. height: 35,
  203. decoration: BoxDecoration(
  204. color: const Color(0xFFEEEEEE),
  205. borderRadius: BorderRadius.all(Radius.circular(8))),
  206. child: TextField(
  207. keyboardType: TextInputType.text,
  208. cursorColor: Constants.BlueTextColor,
  209. textInputAction: TextInputAction.search,
  210. controller: _txtCtrl,
  211. maxLines: 1,
  212. focusNode: focusNode,
  213. style: TextStyle(
  214. textBaseline: TextBaseline.alphabetic, fontSize: 14.5),
  215. autofocus: true,
  216. inputFormatters:
  217. widget.type == GlobalSearchPageType.SearchNewFriends
  218. ? [
  219. WhitelistingTextInputFormatter.digitsOnly,
  220. LengthLimitingTextInputFormatter(15),
  221. ]
  222. : [
  223. LengthLimitingTextInputFormatter(50),
  224. ],
  225. decoration: InputDecoration(
  226. hintText: I18n.of(context).search,
  227. hintStyle: TextStyle(fontSize: 14.5),
  228. contentPadding: EdgeInsets.only(
  229. left: 20,
  230. top: (UserData().language == LanguageType.English ||
  231. UserData().language ==
  232. LanguageType.Vietnamese)
  233. ? 3
  234. : 10.5,
  235. bottom: 10.5),
  236. prefixIcon: Icon(
  237. IconData(
  238. 0xe664,
  239. fontFamily: Constants.IconFontFamily,
  240. ),
  241. color: const Color(0xFFA0A0A0),
  242. size: 18,
  243. ),
  244. suffixIcon: Padding(
  245. padding: EdgeInsetsDirectional.only(
  246. start: 2.0, end: _hasdeleteIcon ? 20.0 : 0),
  247. child: _hasdeleteIcon
  248. ? new InkWell(
  249. onTap: (() {
  250. setState(() {
  251. WidgetsBinding.instance
  252. .addPostFrameCallback(
  253. (_) => _txtCtrl.clear());
  254. _hasdeleteIcon = false;
  255. });
  256. }),
  257. child: Icon(
  258. Icons.clear,
  259. size: 18.0,
  260. color: Constants.BlackTextColor,
  261. ))
  262. : new Text('')),
  263. ),
  264. onChanged: (str) async {
  265. setState(() {
  266. searchStr = str;
  267. if (str.isEmpty) {
  268. _hasdeleteIcon = false;
  269. } else {
  270. _hasdeleteIcon = true;
  271. beginSearch(str);
  272. }
  273. });
  274. },
  275. onEditingComplete: () {
  276. FocusScope.of(context).requestFocus(FocusNode());
  277. }),
  278. )),
  279. ),
  280. body: Stack(
  281. children: _body,
  282. ));
  283. }
  284. }