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.
 
 
 
 
 
 

397 line
14 KiB

  1. import 'package:chat/data/UserData.dart';
  2. import 'package:chat/data/constants.dart';
  3. import 'package:chat/generated/i18n.dart';
  4. import 'package:chat/home/add_friend.dart';
  5. import 'package:chat/utils/CustomUI.dart';
  6. import 'package:chat/utils/HttpUtil.dart';
  7. import 'package:chat/utils/MessageMgr.dart';
  8. import 'package:chat/utils/TokenMgr.dart';
  9. import 'package:chat/utils/conversation_table.dart';
  10. import 'package:dio/dio.dart';
  11. import 'package:cached_network_image/cached_network_image.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter/services.dart';
  14. import 'ProfilePage.dart';
  15. class _ContactItem extends StatelessWidget {
  16. _ContactItem(
  17. {@required this.avatar,
  18. @required this.title,
  19. @required this.userId,
  20. this.groupTitle,
  21. this.onPressed,
  22. this.gradient,
  23. this.iconCode,
  24. this.isShowDivder: true,
  25. this.state});
  26. final int userId;
  27. final int iconCode;
  28. final String avatar;
  29. final String title;
  30. final String groupTitle;
  31. final VoidCallback onPressed;
  32. final Gradient gradient;
  33. final bool isShowDivder;
  34. final int state;
  35. static const double MARGIN_VERTICAL = 10.0;
  36. static const double GROUP_TITLE_HEIGHT = 24.0;
  37. @override
  38. Widget build(BuildContext context) {
  39. Widget _avatarIcon;
  40. if (iconCode == null) {
  41. _avatarIcon = ClipRRect(
  42. borderRadius: BorderRadius.circular(6),
  43. child: CachedNetworkImage(
  44. imageUrl: this.avatar,
  45. width: Constants.ContactAvatarSize,
  46. height: Constants.ContactAvatarSize,
  47. ));
  48. } else {
  49. _avatarIcon = Container(
  50. width: Constants.ContactAvatarSize,
  51. height: Constants.ContactAvatarSize,
  52. decoration: BoxDecoration(
  53. gradient: gradient, borderRadius: BorderRadius.circular(6)),
  54. child: Icon(
  55. IconData(this.iconCode, fontFamily: Constants.IconFontFamily),
  56. color: Colors.white,
  57. ),
  58. );
  59. }
  60. Widget _button = Container(
  61. padding: const EdgeInsets.symmetric(
  62. vertical: MARGIN_VERTICAL, horizontal: 16.0),
  63. decoration: BoxDecoration(color: Colors.white),
  64. child: Row(
  65. children: <Widget>[
  66. _avatarIcon,
  67. SizedBox(width: 10.0),
  68. Expanded(child: Text(title)),
  69. state == 1
  70. ? InkWell(
  71. child: Container(
  72. padding: EdgeInsets.symmetric(horizontal: 21, vertical: 7),
  73. child: Text(
  74. I18n.of(context).added,
  75. textScaleFactor: 1.0,
  76. style: TextStyle(color: const Color(0xFF8A8B8B)),
  77. ),
  78. ),
  79. )
  80. : (state == 0
  81. ? InkWell(
  82. child: Container(
  83. padding:
  84. EdgeInsets.symmetric(horizontal: 21, vertical: 7),
  85. child: Text(
  86. I18n.of(context).already_applied,
  87. textScaleFactor: 1.0,
  88. style: TextStyle(color: const Color(0xFF8A8B8B)),
  89. ),
  90. ),
  91. )
  92. : InkWell(
  93. onTap: () {
  94. Navigator.of(context).push(
  95. new MaterialPageRoute(
  96. builder: (context) {
  97. return AddFriendPage(
  98. userId: this.userId,
  99. pageType: SendMessagePageType.AddFriends,
  100. originalName: I18n.of(context)
  101. .i_am
  102. .replaceFirst(
  103. '/s1', UserData().basicInfo.nickName),
  104. );
  105. },
  106. ),
  107. );
  108. },
  109. child: Container(
  110. padding:
  111. EdgeInsets.symmetric(horizontal: 21, vertical: 7),
  112. decoration: BoxDecoration(
  113. color: const Color(0xFF3875E9),
  114. borderRadius: BorderRadius.circular(5)),
  115. child: Text(
  116. I18n.of(context).add,
  117. textScaleFactor: 1.0,
  118. style: TextStyle(color: Colors.white),
  119. ),
  120. ),
  121. )),
  122. ],
  123. ),
  124. );
  125. //分组标签
  126. Widget _itemBody;
  127. if (this.groupTitle != null) {
  128. _itemBody = Column(
  129. children: <Widget>[
  130. Container(
  131. height: GROUP_TITLE_HEIGHT,
  132. padding: EdgeInsets.only(left: 16.0, right: 16.0),
  133. color: const Color(AppColors.ContactGroupTitleBgColor),
  134. alignment: Alignment.centerLeft,
  135. child: Text(this.groupTitle,
  136. textScaleFactor: 1.0, style: AppStyles.GroupTitleItemTextStyle),
  137. ),
  138. _button,
  139. ],
  140. );
  141. } else {
  142. _itemBody = _button;
  143. }
  144. return InkWell(
  145. onTap: () {
  146. Navigator.of(context).push(
  147. new MaterialPageRoute(
  148. builder: (context) {
  149. return ProfilePage(
  150. userId: userId,
  151. );
  152. },
  153. ),
  154. );
  155. },
  156. child: Container(
  157. color: Colors.white,
  158. child: Column(
  159. children: <Widget>[
  160. _itemBody,
  161. isShowDivder
  162. ? Container(
  163. height: 1,
  164. color: const Color(0xFFF3F3F3),
  165. margin: EdgeInsets.only(
  166. left: 26 + Constants.ContactAvatarSize),
  167. )
  168. : Container()
  169. ],
  170. )));
  171. }
  172. }
  173. class ContactsPage extends StatefulWidget {
  174. @override
  175. _ContactsPageState createState() => _ContactsPageState();
  176. }
  177. class _ContactsPageState extends State<ContactsPage> {
  178. String _currentLetter = '';
  179. ScrollController _scrollController;
  180. TextEditingController _txtCtrl = new TextEditingController();
  181. FocusNode editFocus = FocusNode();
  182. bool _hasdeleteIcon = false;
  183. List<FriendModel> searchList = [];
  184. List<FriendModel> friendList = [];
  185. @override
  186. void initState() {
  187. print('ContactsPage initState');
  188. super.initState();
  189. getFriendList();
  190. _scrollController = new ScrollController();
  191. MessageMgr().on('post_add_friend', msgAddFriend);
  192. }
  193. msgAddFriend(data) {
  194. for (int i = 0; i < friendList.length; i++) {
  195. if (friendList[i].friendId == data) {
  196. setState(() {
  197. friendList[i].state = 0;
  198. });
  199. break;
  200. }
  201. }
  202. }
  203. getFriendList() async {
  204. Map data = {
  205. "userId": UserData().basicInfo.userId,
  206. };
  207. data['sign'] = TokenMgr().getSign(data);
  208. Response res = await HttpUtil().post('maillist/user/get', data: data);
  209. Map resData = res.data;
  210. print(resData['data']);
  211. if (resData['code'] == 0 && resData['data'] != null) {
  212. resData['data'].forEach((f) {
  213. var friend = FriendModel.fromServerJson(f);
  214. if (friend.friendId != UserData().basicInfo.userId)
  215. friendList.add(friend);
  216. });
  217. setState(() {});
  218. }
  219. }
  220. @override
  221. void dispose() {
  222. _scrollController.dispose();
  223. editFocus.dispose();
  224. MessageMgr().off('post_add_friend', msgAddFriend);
  225. super.dispose();
  226. }
  227. @override
  228. Widget build(BuildContext context) {
  229. final List<Widget> _body = [];
  230. if (!_hasdeleteIcon) {
  231. friendList.sort((a, b) => a.nameTag.compareTo(b.nameTag));
  232. _body.add(ListView.builder(
  233. controller: _scrollController,
  234. itemBuilder: (BuildContext context, int index) {
  235. int _contactIndex = index;
  236. bool _isGroupTitle = true;
  237. FriendModel _contact = friendList[_contactIndex];
  238. if (_contactIndex >= 1 &&
  239. _contact.nameTag == friendList[_contactIndex - 1].nameTag) {
  240. _isGroupTitle = false;
  241. }
  242. return _ContactItem(
  243. userId: _contact.friendId,
  244. avatar: _contact.avatar,
  245. title: _contact.name,
  246. state: _contact.state,
  247. isShowDivder: _isGroupTitle,
  248. groupTitle: _isGroupTitle ? _contact.nameTag : null);
  249. },
  250. itemCount: friendList.length,
  251. ));
  252. } else {
  253. _body.add(ListView.builder(
  254. controller: _scrollController,
  255. itemBuilder: (BuildContext context, int index) {
  256. FriendModel _contact = searchList[index];
  257. return _ContactItem(
  258. userId: _contact.friendId,
  259. avatar: _contact.avatar,
  260. title: _contact.name,
  261. state: _contact.state,
  262. isShowDivder: true,
  263. groupTitle: null);
  264. },
  265. itemCount: searchList.length,
  266. ));
  267. }
  268. if (_currentLetter != null &&
  269. _currentLetter.isNotEmpty &&
  270. !_hasdeleteIcon) {
  271. _body.add(Center(
  272. child: Container(
  273. width: Constants.IndexLetterBoxSize,
  274. height: Constants.IndexLetterBoxSize,
  275. decoration: BoxDecoration(
  276. color: AppColors.IndexLetterBoxBgColor,
  277. borderRadius: BorderRadius.all(
  278. Radius.circular(Constants.IndexLetterBoxRadius)),
  279. ),
  280. child: Center(
  281. child: Text(_currentLetter,
  282. textScaleFactor: 1.0, style: AppStyles.IndexLetterBoxTextStyle),
  283. ),
  284. ),
  285. ));
  286. }
  287. return Scaffold(
  288. resizeToAvoidBottomPadding: false,
  289. appBar: AppBar(
  290. backgroundColor: AppColors.NewAppbarBgColor,
  291. title: Text(
  292. I18n.of(context).contact_add,
  293. textScaleFactor: 1.0,
  294. style: TextStyle(color: AppColors.NewAppbarTextColor),
  295. ),
  296. centerTitle: true,
  297. leading: CustomUI.buildCustomLeading(context),
  298. elevation: 1,
  299. bottom: PreferredSize(
  300. preferredSize: Size.fromHeight(49),
  301. child: Container(
  302. alignment: Alignment.center,
  303. margin: EdgeInsets.only(bottom: 14, left: 12.5, right: 12.5),
  304. height: 35,
  305. decoration: BoxDecoration(
  306. color: const Color(0xFFEEEEEE),
  307. borderRadius: BorderRadius.all(Radius.circular(8))),
  308. child: TextField(
  309. keyboardAppearance: Brightness.light,
  310. keyboardType: TextInputType.text,
  311. textInputAction: TextInputAction.search,
  312. controller: _txtCtrl,
  313. focusNode: editFocus,
  314. maxLines: 1,
  315. style: TextStyle(
  316. textBaseline: TextBaseline.alphabetic, fontSize: 14.5),
  317. autofocus: false,
  318. inputFormatters: [
  319. LengthLimitingTextInputFormatter(50),
  320. ],
  321. decoration: InputDecoration(
  322. hintText: I18n.of(context).search,
  323. hintStyle: TextStyle(fontSize: 14.5),
  324. prefixIcon: Icon(
  325. IconData(
  326. 0xe664,
  327. fontFamily: Constants.IconFontFamily,
  328. ),
  329. color: const Color(0xFFA0A0A0),
  330. size: 18,
  331. ),
  332. suffixIcon: Padding(
  333. padding: EdgeInsetsDirectional.only(
  334. start: 2.0, end: _hasdeleteIcon ? 20.0 : 0),
  335. child: _hasdeleteIcon
  336. ? new InkWell(
  337. onTap: (() {
  338. setState(() {
  339. WidgetsBinding.instance
  340. .addPostFrameCallback(
  341. (_) => _txtCtrl.clear());
  342. _hasdeleteIcon = false;
  343. });
  344. }),
  345. child: Icon(
  346. Icons.clear,
  347. size: 18.0,
  348. color: Constants.BlackTextColor,
  349. ))
  350. : new Text('')),
  351. filled: true,
  352. fillColor: Colors.transparent,
  353. border: InputBorder.none,
  354. ),
  355. onChanged: (str) async {
  356. setState(() {
  357. if (str.isEmpty) {
  358. _hasdeleteIcon = false;
  359. } else {
  360. _hasdeleteIcon = true;
  361. searchList = CustomUI().getSearchResult(
  362. str, friendList == null ? [] : friendList);
  363. }
  364. });
  365. },
  366. onEditingComplete: () {}),
  367. )),
  368. ),
  369. body: Stack(
  370. children: _body,
  371. ));
  372. }
  373. }