|
|
|
import 'dart:async';
|
|
|
|
import 'dart:collection';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
|
|
import 'package:huixiang/generated/l10n.dart';
|
|
|
|
import 'package:huixiang/im/database/contact.dart';
|
|
|
|
import 'package:huixiang/im/database/message.dart';
|
|
|
|
import 'package:huixiang/im/out/message.pb.dart';
|
|
|
|
import 'package:huixiang/main.dart';
|
|
|
|
import 'package:huixiang/retrofit/data/base_data.dart';
|
|
|
|
import 'package:huixiang/retrofit/data/msg_stats.dart';
|
|
|
|
import 'package:huixiang/retrofit/data/page.dart';
|
|
|
|
import 'package:huixiang/retrofit/retrofit_api.dart';
|
|
|
|
import 'package:huixiang/utils/font_weight.dart';
|
|
|
|
import 'package:huixiang/view_widget/classic_header.dart';
|
|
|
|
import 'package:huixiang/view_widget/my_footer.dart';
|
|
|
|
import 'package:huixiang/view_widget/round_button.dart';
|
|
|
|
import 'package:pull_to_refresh/pull_to_refresh.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import 'on_chat_message.dart';
|
|
|
|
import 'on_chat_msg_instance.dart';
|
|
|
|
|
|
|
|
class IMPage extends StatefulWidget {
|
|
|
|
IMPage(Key key) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return _IMPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _IMPage extends State<IMPage> implements OnChatMessage {
|
|
|
|
ApiService apiService;
|
|
|
|
int pageNum = 1;
|
|
|
|
List<Message> messages = [];
|
|
|
|
Map<String, int> msgNumber = {
|
|
|
|
"1": 0,
|
|
|
|
"2": 0,
|
|
|
|
"3": 0,
|
|
|
|
"4": 0,
|
|
|
|
"5": 0,
|
|
|
|
"6": 0,
|
|
|
|
};
|
|
|
|
int state = 0;
|
|
|
|
final TextEditingController imEditingController = TextEditingController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onMessage(txt) {
|
|
|
|
// SmartDialog.showToast("列表 $txt", alignment: Alignment.center);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
OnChatMsgInstance.instance.onChatMessage = null;
|
|
|
|
|
|
|
|
socketClient.removeCallback(socketClient.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
OnChatMsgInstance.instance.onChatMessage = this;
|
|
|
|
|
|
|
|
loadMessageList();
|
|
|
|
SharedPreferences.getInstance().then((value) {
|
|
|
|
apiService = ApiService(Dio(), token: value.getString("token"), context: context);
|
|
|
|
queryMsgStats();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_refresh() {
|
|
|
|
pageNum = 1;
|
|
|
|
loadMessageList();
|
|
|
|
queryMsgStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> userIds = [];
|
|
|
|
Map<String, Contact> contactMap = {};
|
|
|
|
Map<String, Message> lastMessageMap = {};
|
|
|
|
Stream streamSubscription ;
|
|
|
|
|
|
|
|
loadMessageList() async {
|
|
|
|
SharedPreferences shared = await SharedPreferences.getInstance();
|
|
|
|
String userId = shared.getString("userId");
|
|
|
|
|
|
|
|
debugPrint("messages: loadMessageList");
|
|
|
|
socketClient.addCallback(userId, (Message message) {
|
|
|
|
if (userIds.contains(message.fromId)) {
|
|
|
|
userIds.remove(message.fromId);
|
|
|
|
}
|
|
|
|
userIds.insert(0, message.fromId);
|
|
|
|
|
|
|
|
lastMessageMap[message.fromId] = message;
|
|
|
|
|
|
|
|
debugPrint("messages_records : ${message.toJson()}");
|
|
|
|
if (contactMap[message.fromId] == null) {
|
|
|
|
/// TODO: message.fromId request Api and setState
|
|
|
|
}
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
debugPrint("messages: queryList");
|
|
|
|
messages = await hxDatabase.queryList(userId);
|
|
|
|
messages.forEach((element) {
|
|
|
|
debugPrint("messages: ${element.toJson()}");
|
|
|
|
});
|
|
|
|
userIds = messages
|
|
|
|
.map((e) => e.toId != userId ? e.toId : e.fromId)
|
|
|
|
.toSet().where((element) => element != userId)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
lastMessageMap = groupBy(messages, (p0) => p0.toId != userId ? p0.toId : p0.fromId);
|
|
|
|
|
|
|
|
List<Contact> contacts = await hxDatabase.queryContact(userIds);
|
|
|
|
if (contacts?.isEmpty ?? false) {
|
|
|
|
/// TODO: userIds request Api
|
|
|
|
} else {
|
|
|
|
List<String> queryUserIds = userIds.where((u) => contacts.where((c) => c.userId == u).isEmpty).toList();
|
|
|
|
/// TODO: queryUserIds request Api
|
|
|
|
}
|
|
|
|
contactMap = groupBy(contacts, (p0) => p0.userId);
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateLastMessage(String userId) async {
|
|
|
|
Message message = await hxDatabase.lastMessage(userId);
|
|
|
|
if (message != null) {
|
|
|
|
lastMessageMap[userId] = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<S, T> groupBy<S, T>(Iterable<T> values, S Function(T) key) {
|
|
|
|
var map = <S, T>{};
|
|
|
|
for (var element in values) {
|
|
|
|
map[key(element)] ??= element;
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryMessage() async {
|
|
|
|
// BaseData<PageInfo<Message>> baseData = await apiService.msgList({
|
|
|
|
// "pageNum": pageNum,
|
|
|
|
// "pageSize": 10,
|
|
|
|
// "searchKey": "",
|
|
|
|
// "state": "",
|
|
|
|
// "typed": ""
|
|
|
|
// }).catchError((onError) {
|
|
|
|
// _refreshController.loadFailed();
|
|
|
|
// _refreshController.refreshFailed();
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
// if (baseData != null && baseData.isSuccess) {
|
|
|
|
// if (pageNum == 1) {
|
|
|
|
// messages.clear();
|
|
|
|
// }
|
|
|
|
// List<Message> message = [];
|
|
|
|
// message.addAll(baseData.data.list);
|
|
|
|
// message.forEach((element) {
|
|
|
|
// if (element.typed == 2 || element.typed == 3) {
|
|
|
|
// messages.add(element);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// _refreshController.loadComplete();
|
|
|
|
// _refreshController.refreshCompleted();
|
|
|
|
// if (mounted) setState(() {});
|
|
|
|
// if (pageNum * 10 > int.tryParse(baseData.data.total)) {
|
|
|
|
// _refreshController.loadNoData();
|
|
|
|
// } else {
|
|
|
|
// pageNum += 1;
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// _refreshController.loadFailed();
|
|
|
|
// _refreshController.refreshFailed();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
queryMsgStats() async {
|
|
|
|
if (apiService == null) {
|
|
|
|
SharedPreferences value = await SharedPreferences.getInstance();
|
|
|
|
apiService = ApiService(
|
|
|
|
Dio(),
|
|
|
|
context: context,
|
|
|
|
token: value.getString("token"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
BaseData<List<MsgStats>> baseData =
|
|
|
|
await apiService.stats().catchError((onError) {});
|
|
|
|
if (baseData != null && baseData.isSuccess) {
|
|
|
|
setState(() {
|
|
|
|
msgNumber.forEach((key, value) {
|
|
|
|
msgNumber[key] = 0;
|
|
|
|
});
|
|
|
|
baseData.data.forEach((element) {
|
|
|
|
if (msgNumber.containsKey(element.name)) {
|
|
|
|
msgNumber[element.name] = element.number;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
_refreshController.loadComplete();
|
|
|
|
_refreshController.refreshCompleted();
|
|
|
|
}
|
|
|
|
EasyLoading.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefreshController _refreshController = RefreshController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
backgroundColor: Color(0xFFFFFFFF),
|
|
|
|
body: SmartRefresher(
|
|
|
|
enablePullDown: true,
|
|
|
|
enablePullUp: false,
|
|
|
|
header: MyHeader(),
|
|
|
|
physics: BouncingScrollPhysics(),
|
|
|
|
footer: CustomFooter(
|
|
|
|
loadStyle: LoadStyle.ShowWhenLoading,
|
|
|
|
builder: (BuildContext context, LoadStatus mode) {
|
|
|
|
return (messages.length == 0) ? Container() : MyFooter(mode);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
controller: _refreshController,
|
|
|
|
onRefresh: _refresh,
|
|
|
|
onLoading: () {
|
|
|
|
setState(() {
|
|
|
|
_refresh();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
// color: Colors.white,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
gradient: LinearGradient(
|
|
|
|
begin: Alignment.topCenter,
|
|
|
|
end: Alignment.bottomCenter,
|
|
|
|
colors: [
|
|
|
|
Color(0xFFD9FFDE),
|
|
|
|
Color(0xFFD9FFDE),
|
|
|
|
Color(0xFFFFFFFF),
|
|
|
|
Color(0xFFFFFFFF),
|
|
|
|
],
|
|
|
|
stops: [0, 0.2, 0.4, 1],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
physics: BouncingScrollPhysics(),
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.only(bottom: 30.h),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
padding: EdgeInsets.only(
|
|
|
|
top: MediaQuery.of(context).padding.top + 12.h,
|
|
|
|
bottom: 15.h,
|
|
|
|
right: 16.w,
|
|
|
|
left: 16.w),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
S.of(context).xiaoxi,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.black,
|
|
|
|
fontSize: 18.sp,
|
|
|
|
fontWeight: MyFontWeight.bold,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
GestureDetector(
|
|
|
|
behavior: HitTestBehavior.opaque,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushNamed('/router/chat_friend_group').then((value) {
|
|
|
|
_refresh();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.all(12),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Color(0xFFFFFFFF),
|
|
|
|
borderRadius: BorderRadius.circular(20.r),
|
|
|
|
),
|
|
|
|
child: Image.asset(
|
|
|
|
"assets/image/friend_grouping.webp",
|
|
|
|
fit: BoxFit.fill,
|
|
|
|
height: 14.h,
|
|
|
|
width: 14.h,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
imSearchItem(),
|
|
|
|
chatList(),
|
|
|
|
// buildMessage(),fgg
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
///搜索
|
|
|
|
Widget imSearchItem() {
|
|
|
|
return Container(
|
|
|
|
margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 13.h),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Color(0xFFFFFFFF),
|
|
|
|
borderRadius: BorderRadius.circular(4),
|
|
|
|
boxShadow: [
|
|
|
|
BoxShadow(
|
|
|
|
color: Colors.black.withAlpha(12),
|
|
|
|
offset: Offset(0, 3),
|
|
|
|
blurRadius: 14,
|
|
|
|
spreadRadius: 0,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
child: TextField(
|
|
|
|
textInputAction: TextInputAction.search,
|
|
|
|
onEditingComplete: () {
|
|
|
|
FocusScope.of(context).requestFocus(FocusNode());
|
|
|
|
},
|
|
|
|
controller: imEditingController,
|
|
|
|
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(0xFF353535),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
prefixIconConstraints: BoxConstraints(),
|
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
///聊天列表
|
|
|
|
Widget chatList() {
|
|
|
|
return Container(
|
|
|
|
child: ListView.builder(
|
|
|
|
padding: EdgeInsets.only(top: 16),
|
|
|
|
itemCount: userIds.length,
|
|
|
|
shrinkWrap: true,
|
|
|
|
physics: NeverScrollableScrollPhysics(),
|
|
|
|
itemBuilder: (context, position) {
|
|
|
|
return GestureDetector(
|
|
|
|
behavior: HitTestBehavior.opaque,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushNamed(
|
|
|
|
'/router/chat_details_page',
|
|
|
|
arguments: {
|
|
|
|
"toId": userIds[position],
|
|
|
|
},
|
|
|
|
).then((value) {
|
|
|
|
updateLastMessage(userIds[position]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: chatItem(userIds[position]),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget chatItem(userId) {
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.only(left: 16.w, right: 17.w, bottom: 18.h),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
// MImage(
|
|
|
|
// "",
|
|
|
|
// isCircle: true,
|
|
|
|
// width: 40,
|
|
|
|
// height: 40,
|
|
|
|
// fit: BoxFit.cover,
|
|
|
|
// errorSrc: "assets/image/default_user.webp",
|
|
|
|
// fadeSrc: "assets/image/default_user.webp",
|
|
|
|
// ),
|
|
|
|
Image.asset(
|
|
|
|
"assets/image/fuka_zj.webp",
|
|
|
|
height: 54.h,
|
|
|
|
width: 54.h,
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
width: 12.w,
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
"喽哈 $userId",
|
|
|
|
// overflow: TextOverflow.fade,
|
|
|
|
maxLines: 1,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16.sp,
|
|
|
|
color: Color(0xFF060606),
|
|
|
|
fontWeight: MyFontWeight.semi_bold,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
"2021.03.08 13:22",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12.sp,
|
|
|
|
color: Color(0xFFA29E9E),
|
|
|
|
fontWeight: MyFontWeight.regular,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 7.h,
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
"新开的火锅店好吃得很",
|
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 12.sp,
|
|
|
|
color: Color(0xFF353535),
|
|
|
|
fontWeight: MyFontWeight.regular,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 16,
|
|
|
|
height: 16,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.circular(100),
|
|
|
|
color: Color(0xFFFF441A),
|
|
|
|
),
|
|
|
|
child: RoundButton(
|
|
|
|
text: "99",
|
|
|
|
textColor: Colors.white,
|
|
|
|
fontWeight: MyFontWeight.regular,
|
|
|
|
backgroup: Color(0xFFFF441A),
|
|
|
|
fontSize: 10.sp,
|
|
|
|
radius: 100,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|