ContactsScreen.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import 'package:azlistview/azlistview.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:lpinyin/lpinyin.dart';
  4. import 'package:prime_chat/screens/ProfileScreen.dart';
  5. import '../basic/Common.dart';
  6. import '../basic/Entities.dart';
  7. import '../basic/Method.dart';
  8. import 'NewFriendScreen.dart';
  9. import 'components/Avatar.dart';
  10. // 好友列表页面
  11. class ContactsScreen extends StatefulWidget {
  12. const ContactsScreen({Key? key}) : super(key: key);
  13. @override
  14. State<StatefulWidget> createState() => _ContactsScreenState();
  15. }
  16. class _ContactsScreenState extends State<ContactsScreen> {
  17. List<FriendInfo> _friends = [];
  18. double susItemHeight = 40;
  19. @override
  20. void initState() {
  21. super.initState();
  22. _loadFriends();
  23. }
  24. void _loadFriends() {
  25. method.getFriends().then((value) {
  26. _friends = [];
  27. if (value != null) {
  28. for (var e in value) {
  29. _friends.add(FriendInfo(profile: e));
  30. }
  31. }
  32. _handleList(_friends);
  33. });
  34. }
  35. void _handleList(List<FriendInfo> list) {
  36. for (int i = 0, length = list.length; i < length; i++) {
  37. String pinyin = PinyinHelper.getPinyinE(list[i].name);
  38. String tag = pinyin.substring(0, 1).toUpperCase();
  39. // list[i].namePinyin = pinyin;
  40. if (RegExp("[A-Z]").hasMatch(tag)) {
  41. list[i].tagIndex = tag;
  42. } else {
  43. list[i].tagIndex = "#";
  44. }
  45. }
  46. // A-Z sort.
  47. SuspensionUtil.sortListBySuspensionTag(_friends);
  48. // show sus tag.
  49. SuspensionUtil.setShowSuspensionStatus(_friends);
  50. // add header.
  51. _friends.insert(0, FriendInfo(profile: const Profile(name: "header"), tagIndex: '↑'));
  52. setState(() {});
  53. }
  54. Widget _buildHeader() {
  55. return Container(
  56. padding: const EdgeInsets.all(20),
  57. alignment: Alignment.center,
  58. child: Column(
  59. // mainAxisAlignment: MainAxisAlignment.center,
  60. children: <Widget>[
  61. ListTile(
  62. leading: ClipRRect(
  63. borderRadius: BorderRadius.circular(10.0),
  64. child: Container(
  65. color: Colors.orange,
  66. height: 40.0,
  67. width: 40.0,
  68. child: const Icon(Icons.person_add, color: Colors.white, size: 30.0,),
  69. ),
  70. ),
  71. title: const Text("新的好友"),
  72. onTap: () {
  73. Navigator.push(
  74. context,
  75. MaterialPageRoute(builder: (context) => const NewFriendScreen()),
  76. );
  77. },
  78. )
  79. ],
  80. ),
  81. );
  82. }
  83. Widget _buildSusWidget(String susTag) {
  84. return Container(
  85. padding: const EdgeInsets.symmetric(horizontal: 15.0),
  86. height: susItemHeight,
  87. width: double.infinity,
  88. alignment: Alignment.centerLeft,
  89. child: Row(
  90. children: <Widget>[
  91. Text(
  92. susTag,
  93. textScaleFactor: 1.2,
  94. ),
  95. const Expanded(
  96. child: Divider(
  97. height: .0,
  98. indent: 10.0,
  99. ))
  100. ],
  101. ),
  102. );
  103. }
  104. Widget _buildListItem(FriendInfo model) {
  105. String susTag = model.getSuspensionTag();
  106. return Column(
  107. children: <Widget>[
  108. Offstage(
  109. offstage: model.isShowSuspension != true,
  110. child: _buildSusWidget(susTag),
  111. ),
  112. ListTile(
  113. leading: Avatar(model.profile.avatar),
  114. title: Text(model.name),
  115. onTap: () {
  116. Navigator.push(
  117. context, MaterialPageRoute(
  118. builder: (context) => ProfileScreen(
  119. profile: model.profile, isFriend: true)));
  120. },
  121. )
  122. ],
  123. );
  124. }
  125. Decoration getIndexBarDecoration(Color color) {
  126. return BoxDecoration(
  127. color: color,
  128. borderRadius: BorderRadius.circular(20.0),
  129. border: Border.all(color: Colors.grey[300]!, width: .5));
  130. }
  131. Widget _buildList(BuildContext context) {
  132. return AzListView(
  133. data: _friends,
  134. itemCount: _friends.length,
  135. itemBuilder: (BuildContext context, int index) {
  136. if (index == 0) return _buildHeader();
  137. final model = _friends[index];
  138. return _buildListItem(model);
  139. },
  140. physics: const BouncingScrollPhysics(),
  141. indexBarData: SuspensionUtil.getTagIndexList(_friends),
  142. indexHintBuilder: (context, hint) {
  143. return Container(
  144. alignment: Alignment.center,
  145. width: 60.0,
  146. height: 60.0,
  147. decoration: BoxDecoration(
  148. color: Colors.blue[700]!.withAlpha(200),
  149. shape: BoxShape.circle,
  150. ),
  151. child:
  152. Text(hint, style: const TextStyle(color: Colors.white, fontSize: 30.0)),
  153. );
  154. },
  155. indexBarMargin: const EdgeInsets.all(10),
  156. indexBarOptions: IndexBarOptions(
  157. needRebuild: true,
  158. decoration: getIndexBarDecoration(Colors.grey[50]!),
  159. downDecoration: getIndexBarDecoration(Colors.grey[200]!),
  160. ),
  161. );
  162. }
  163. @override
  164. Widget build(BuildContext context) {
  165. return Scaffold(
  166. appBar: AppBar(
  167. title: const Text('好友'),
  168. actions: [
  169. IconButton(
  170. onPressed: () async {
  171. // Navigator.push(
  172. // context,
  173. // MaterialPageRoute(builder: (context) => const AddFriendScreen()),
  174. // );
  175. },
  176. icon: const Icon(Icons.add),
  177. ),
  178. ],
  179. ),
  180. body: RefreshIndicator(
  181. onRefresh: () async {
  182. _loadFriends();
  183. },
  184. child: _buildList(context),
  185. ),
  186. );
  187. }
  188. }