Flutter高仿微信-第42篇-创建群
创始人
2024-02-16 09:44:53
0

Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。

 详情请查看

效果图:

实现代码:

/*** Author : wangning* Email : maoning20080809@163.com* Date : 2022/11/6 19:46* Description : 发起群聊*/class AddGroup extends StatefulWidget{@overrideState createState() => _AddGroupState();}class _AddGroupState extends State{final ScrollController _scrollController = ScrollController(); //listview 的控制器List _contactList = [];bool isLoading = false;String account = SpUtils.getString(CommonUtils.LOGIN_ACCOUNT);UserBean? _userBean;@overridevoid initState() {super.initState();_getData();}_getData() async {List contactList = await ContactsRepository.getInstance().findAllContactsComb(account);_userBean = await UserRepository.getInstance().findUserByAccount(account);setState(() {_contactList = contactList;});}//完成操作void _complete() async {//已选择的账户List selectAccounts = [];//已选择的头像List selectAvatars = [];//选择多个好友for(int i = 0; i < _contactList.length;i++){ContactsBeanComb contactsBeanComb = _contactList[i];if(contactsBeanComb.isCheck){String selectAccount = "";String selectAvatar = "";if(account == contactsBeanComb.toAccount){selectAccount = contactsBeanComb.fromAccount??"";} else {selectAccount = contactsBeanComb.toAccount??"";}selectAvatar = contactsBeanComb.avatar;selectAccounts.add(selectAccount);selectAvatars.add(selectAvatar);}}if(selectAccounts.isEmpty){CommonToast.show(context, "请选择一个好友!");return;}LoadingDialogUtils.showLoadingDialog(context, msg: "请稍后...");selectAvatars.add(_userBean?.avatar??"");selectAccounts.add(account);int max = 1000000000;int ran = Random().nextInt(max) + max;String groupId = "g${ran}";//创建群bool isSuccess = await XmppManager.getInstance().createMUC(groupId, true);if(isSuccess){String addTime = WnDateUtils.getCurrentTime();//加入群await XmppManager.getInstance().joinMucGroup(groupId);GroupBean groupBean = GroupBean(groupId: groupId, groupName: groupId, avatar: "", addTime: addTime);GroupRepository.getInstance().insertGroup(groupBean);//邀请好友入群await XmppManager.getInstance().addMembersInGroup(groupId, selectAccounts);List groupUserList = [];GroupUserBean groupUserBean = GroupUserBean(groupId: groupId, account: account, accountType: GroupUserBean.ACCOUNT_TYPE_OWNER, addTime: addTime);LogUtils.d("添加群主:${groupUserBean.toJson()}");//插入群主到本地数据库GroupUserRepository.getInstance().insertGroupUser(groupUserBean);groupUserList.add(groupUserBean);selectAccounts.forEach((selectAccount) {if(selectAccount != account){//只添加群员GroupUserBean groupUserBean2 = GroupUserBean(groupId: groupId, account: selectAccount, accountType: GroupUserBean.ACCOUNT_TYPE_MEMBER, addTime: addTime);LogUtils.d("添加成员:${groupUserBean2.toJson()}");//插入群员到本地数据库GroupUserRepository.getInstance().insertGroupUser(groupUserBean2);groupUserList.add(groupUserBean2);}});await GroupRepository.getInstance().insertGroupServer(groupBean);await GroupUserRepository.getInstance().insertGroupUserServer(groupUserList);await ImageUtils.processMergeAvatarImage(groupId, selectAvatars);LoadingDialogUtils.dimissLoadingDialog(context);Navigator.pop(context);Navigator.pushNamed(context, Routes.group_chat_main, arguments: {"groupId":groupId   //参数map});} else {LoadingDialogUtils.dimissLoadingDialog(context);CommonToast.show(context, "创建群聊失败!");}}@overrideWidget build(BuildContext context) {return Scaffold(appBar: WnAppBar.getAppBar(context, Text("发起群聊")),body: Column(children: [SizedBox(height: 10,),Container(child: Text("请选择好友", style: TextStyle(fontSize: 22, color: Colors.black54, fontWeight: FontWeight.bold),),),Expanded(child:ListView.builder(itemCount: _contactList.length,controller: _scrollController,itemBuilder: (context, index) {return InkWell(onLongPress: (){LogUtils.d("长按。${index}");},onTap: (){//_goDetails(_contactList[index]);},child: Container(decoration: BoxDecoration(border: Border(bottom:BorderSide(color: Color(0xffd9d9d9), width: 0.3))),padding: EdgeInsets.only(left: 14, top: 10, bottom: 10),child: Row(children: [CommonAvatarView.showBaseImage(_contactList[index].avatar, 44, 44),SizedBox(width: 12,),Text(_contactList[index].nickName, maxLines: 1,style: TextStyle(fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),),Expanded(child: Text("")),Checkbox(value: _contactList[index].isCheck, onChanged: (isCheck){//LogUtils.d("点击:${isCheck}");_contactList[index].isCheck = isCheck!;setState(() {});}),],),),);})),Container(alignment: Alignment.centerRight,margin: EdgeInsets.only(right: 12, bottom: 6),child: ElevatedButton(style: ElevatedButton.styleFrom(backgroundColor: Colors.green,textStyle: TextStyle(fontSize: 20)),onPressed: (){_complete();},child: Text("完成")),),],),);}}

相关内容

热门资讯

特朗普,被起诉!美股高开跳水!... 今晚,美国多项数据公布,此外,美联储理事丽莎·库克起诉特朗普,不确定性再次增加。 美股跳水 晚间,美...
美国父母起诉OpenAI:指控... 一对加利福尼亚州的父母于本周二对人工智能巨头OpenAI提出了过失致死的指控。他们声称,OpenAI...
原创 《... 【版权申明:本文为@影吹斯汀 独家原创稿,未经许可不得以任何形式抄袭or转载,违者必究!】 暑期档失...
8月多家银行首席合规官亮相!业... 8月,已有多位银行首席合规官亮相! 8月26日,国家金融监督管理总局山东监管局官网发布一则批复,核准...
美联储理事莉萨·库克起诉,反对... 彭博社8月28日报道,美联储理事莉萨·库克已提起诉讼,反对美国总统特朗普以“她在抵押贷款申请中撒谎”...
因借款合同纠纷,建设银行起诉宁... 天眼查APP显示,近日,中国建设银行股份有限公司新增一则开庭公告,案由为“借款合同纠纷”,原告为中国...
卡罗特(02549.HK)委任... 格隆汇8月28日丨卡罗特(02549.HK)宣布,陈婉梅辞去以下职务:(i)公司秘书;(ii)根据联...
净利大增19倍,稀土龙头200... 记者丨董鹏 编辑丨张伟贤 8月28日,北方稀土再次大涨5%,公司总市值重返2000亿元,逼近2021...
斯诺克武汉公开赛|肖国栋终结黑... 8月28日下午,2025斯诺克武汉公开赛一场1/4决赛焦点战在中国球员肖国栋和本届赛事的最大黑马选手...
原创 1... 墨西哥突然传出要对华加征关税,背后推手直指美国。但这份看似强硬的提案,却暗藏时间差游戏——它真会落地...