Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。
效果图:
实现代码:
import 'package:flutter/material.dart'; import 'package:wnchat2/bean/contacts/contacts_bean_comb.dart'; import 'package:wnchat2/bean/groupchat/group_user_bean.dart'; import 'package:wnchat2/common/common_utils.dart'; import 'package:wnchat2/common/sp_utils.dart'; import 'package:wnchat2/common/wn_app_bar.dart'; import 'package:wnchat2/event/base_event.dart'; import 'package:wnchat2/repository/user_repository.dart'; import 'package:wnchat2/routers/routers.dart'; import 'package:wnchat2/utils/image_utils.dart'; import 'package:wnchat2/utils/loading_dialog_utils.dart'; import 'package:wnchat2/utils/wn_date_utils.dart'; import 'package:wnchat2/views/common_avatar_view.dart'; import '../common/event_bus_utils.dart'; import '../repository/contacts_repository.dart'; import '../repository/group_user_repository.dart'; import '../utils/common_toast.dart'; import '../utils/log_utils.dart'; /** * Author : wangning * Email : maoning20080809@163.com * Date : 2022/11/6 19:46 * Description : 添加群成员 */ class AddGroupMember extends StatefulWidget{ String groupId; AddGroupMember({required this.groupId}); @override State<StatefulWidget> createState() => _AddGroupMemberState(); } class _AddGroupMemberState extends State<AddGroupMember>{ ScrollController _scrollController = ScrollController(); //listview 的控制器 List<ContactsBeanComb> _contactList = []; bool isLoading = false; String account = SpUtils.getString(CommonUtils.LOGIN_ACCOUNT); @override void initState() { super.initState(); _getData(); } _getData() async { List<ContactsBeanComb> contactList = await ContactsRepository.getInstance().findAllContactsCombGroup(widget.groupId); setState(() { _contactList = contactList; }); } //完成操作 void _complete() async { //已选择的账户 List<String> selectAccounts = []; //选择多个好友 for(int i = 0; i < _contactList.length;i++){ ContactsBeanComb contactsBeanComb = _contactList[i]; if(contactsBeanComb.isCheck){ String selectAccount = ""; if(account == contactsBeanComb.toAccount){ selectAccount = contactsBeanComb.fromAccount??""; } else { selectAccount = contactsBeanComb.toAccount??""; } selectAccounts.add(selectAccount); } } if(selectAccounts.isEmpty){ CommonToast.show(context, "请选择一个好友!"); return; } LoadingDialogUtils.showLoadingDialog(context, msg: "请稍后..."); String addTime = WnDateUtils.getCurrentTime(); List<GroupUserBean> groupUserList = []; for(int i = 0; i < selectAccounts.length; i++){ String selectAccount = selectAccounts[i]; if(selectAccount != account){ //只添加群员 GroupUserBean groupUserBean = GroupUserBean(groupId: widget.groupId, account: selectAccount, accountType: GroupUserBean.ACCOUNT_TYPE_MEMBER, addTime: addTime); LogUtils.d("添加成员:${groupUserBean.toJson()}"); //插入群员到本地数据库 await GroupUserRepository.getInstance().insertGroupUser(groupUserBean); groupUserList.add(groupUserBean); } } List<String>? avatarList = await UserRepository.getInstance().findAvatarListByGroupId(widget.groupId); await GroupUserRepository.getInstance().insertGroupUserServer(groupUserList); //刷新群聊数据 eventBus.emit(BaseEvent(BaseEvent.TYPE_REFRESH_GROUP, result: HashMap<String, Object>())); if(avatarList != null && avatarList.isNotEmpty) { await ImageUtils.processMergeAvatarImage(widget.groupId, avatarList); } LoadingDialogUtils.dimissLoadingDialog(context); CommonToast.show(context, "邀请好友成功!"); Navigator.popUntil(context, ModalRoute.withName(Routes.group_chat_main)); } @override Widget 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("完成") ), ), ], ), ); } }
转载:https://blog.csdn.net/maoning20080808/article/details/128050550
查看评论