Flutter高仿微信-第26篇-新的朋友
生活随笔
收集整理的這篇文章主要介紹了
Flutter高仿微信-第26篇-新的朋友
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Flutter高仿微信系列共59篇,從Flutter客戶端、Kotlin客戶端、Web服務器、數據庫表結構、Xmpp即時通訊服務器、視頻通話服務器、騰訊云服務器全面講解。
詳情請查看
效果圖:
實現代碼:
/*** Author : wangning* Email : maoning20080809@163.com* Date : 2022/8/17 15:48* Description : 新的朋友*/ class NewFriends extends StatelessWidget {const NewFriends({super.key});@overrideWidget build(BuildContext context) {return const NewFriendsPage(title: '新的朋友');} }class NewFriendsPage extends StatefulWidget {const NewFriendsPage({super.key, required this.title});final String title;@overrideState<NewFriendsPage> createState() => _NewFriendsPageState(); }class _NewFriendsPageState extends State<NewFriendsPage> {final ScrollController _scrollController = ScrollController(); //listview 的控制器List<ContactsBeanComb> _contactList = [];var json; //是否正在加載數據@overridevoid initState() {super.initState();_updateStatus();_getData();//下面這個方法每次都底部都會執行,上面的代碼只會執行一次_scrollController.addListener(() {if (_scrollController.position.pixels >_scrollController.position.maxScrollExtent - 20) {_getData();}});}//更改狀態void _updateStatus() async {ContactsRepository.getInstance().updateContactStatusRead();Map<String, Object> result = HashMap<String, Object>();eventBus.emit(BaseEvent(BaseEvent.TYPE_READ_FRIENDS, result: result));}//加載數據_getData() async {List<ContactsBeanComb> contactList = await ContactsRepository.getInstance().findAllContactsCombNew();_contactList = [];setState(() {_contactList = contactList;});}//沒有好友請求顯示Widget _noDataWidget() {return Center(child: Padding(padding: EdgeInsets.all(10.0),child: Row(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[Text('沒有新的朋友...',style: TextStyle(fontSize: 16.0),)],),),);}Future<void> _onRefresh() async {await Future.delayed(Duration(seconds: 3), () {LogUtils.d('通訊錄refresh');});}//接受好友邀請void _receiveFriends(ContactsBeanComb contactsBeanComb){XmppManager.getInstance().createRoster(contactsBeanComb.toAccount);ContactsBean contactsBean = ContactsBean();contactsBean.id = contactsBeanComb.id;contactsBean.fromAccount = contactsBeanComb.fromAccount;contactsBean.toAccount = contactsBeanComb.toAccount;contactsBean.type = ContactsBean.typeReceive;contactsBean.addTime = contactsBeanComb.addTime;ContactsRepository.getInstance().updateContactLocal(contactsBean);ContactsRepository.getInstance().updateContactServer(contactsBean);ChatSendBean chatSendBean = ChatSendBean();chatSendBean.contentType = CommonUtils.TYPE_RECEIVE_FRIENDS;chatSendBean.content = contactsBeanComb.toAccount;String message = jsonEncode(chatSendBean);_sendMessage(contactsBeanComb.fromAccount, message);Map<String, Object> result = HashMap<String, Object>();eventBus.emit(BaseEvent(BaseEvent.TYPE_RECEIVE_FRIENDS, result: result));Navigator.pop(context);}//發送消息_sendMessage(String toAccount, var message){int id = DateTime.now().millisecondsSinceEpoch;String account = SpUtils.getString(CommonUtils.LOGIN_ACCOUNT);String toJid = toAccount + "@wangning";XmppManager.getInstance().sendMessage(toJid, message, "$account", id);}@overrideWidget build(BuildContext context) {return Scaffold(appBar: WnAppBar.getAppBar(context, Text("${widget.title}")),body: Column(children: [Expanded(child:_contactList.length > 0? RefreshIndicator(onRefresh: _onRefresh,child: ListView.builder(itemCount: _contactList.length,controller: _scrollController,itemBuilder: (context, index) {return InkWell(onLongPress: (){_showDeleteDialog(_contactList[index].fromAccount, _contactList[index].toAccount);},onTap: (){},child: Container(decoration: BoxDecoration(border: Border(bottom:BorderSide(color: Color(0xffd9d9d9), width: 0.3))),width: double.infinity,padding: EdgeInsets.only(left: 14, top: 10, bottom: 10),child: Row(children: [CommonAvatarView.showBaseImage(_contactList[index].avatar),SizedBox(width: 12,),Text(_contactList[index].nickName, maxLines: 1,style: TextStyle(fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),),SizedBox(width: 12,),Expanded(child: SizedBox()),//自動擴展擠壓_getFriendsStatus(_contactList[index]),],),),);})): _noDataWidget()),],),);}//刪除對話框Future<void> _showDeleteDialog(String fromAccount, String toAccount) async {return showDialog<Null>(context: context,barrierDismissible: false,builder: (BuildContext context) {return AlertDialog(title: Text('確定要刪除該好友嗎?', style: new TextStyle(fontSize: 17.0)),actions: <Widget>[MaterialButton(child: Text('取消'),onPressed: (){LogUtils.d("確定取消");Navigator.of(context).pop();},),MaterialButton(child: Text('確定'),onPressed: (){LogUtils.d("確定刪除");Navigator.pop(context);_deleteContacts(fromAccount, toAccount);},)],);});}//刪除聯系人_deleteContacts(String fromAccount, String toAccount) async{bool deleteServerFlag = await ContactsRepository.getInstance().deleteContactsByAccountServer(fromAccount, toAccount);if(deleteServerFlag){bool deleteFlag = await ContactsRepository.getInstance().deleteContactsByAccount(fromAccount, toAccount);if(deleteFlag){CommonToast.show(context, "刪除成功!");_getData();} else {CommonToast.show(context, "刪除失敗!");}} else {CommonToast.show(context, "刪除失敗!");}}Widget _getFriendsStatus(ContactsBeanComb contactsBeanComb){String account = SpUtils.getString(CommonUtils.LOGIN_ACCOUNT);LogUtils.d("新的朋友 _getFriendsStatus ${account} , ${contactsBeanComb.fromAccount}, ${contactsBeanComb.toAccount}");if(contactsBeanComb.fromAccount == account) {LogUtils.d("已發送");return GestureDetector(onLongPress: (){_receiveFriends(contactsBeanComb);},child: Container(margin: EdgeInsets.only(left: 12.0, right: 12),child: Text("已發送"),),);} else if(contactsBeanComb.type == ContactsBean.typeRequest||contactsBeanComb.type == ContactsBean.typeRead){LogUtils.d("同意按鈕");return Container(margin: EdgeInsets.only(left: 12.0, right: 12),child: MaterialButton(color: Colors.green,textColor: Colors.white,child: Text("同意"),onPressed: (){LogUtils.d("新的朋友點擊添加");_receiveFriends(contactsBeanComb);},),);}return Text("");} }總結
以上是生活随笔為你收集整理的Flutter高仿微信-第26篇-新的朋友的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 游泳教程
- 下一篇: 我是如何在一家独角兽公司做业务中台、数据