Flutter实战一Flutter聊天应用(十八)
在上一篇文章中,我们完成了基本的添加聊天功能,但是还没有在聊天列表显示添加的新聊天,在这篇文章中我们将实现这个功能——在聊天列表中展示所有的聊天。 首先,我们在 import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'chat_screen.dart';
import 'prompt_wait.dart';
class GroupChatListBody extends StatefulWidget {
GroupChatListBody({
this.phone,this.myName,Key key,})
: super(key: key);
final String phone;
final String myName;
@override
_GroupChatListBodyState createState() => new _GroupChatListBodyState(phone);
}
class _GroupChatListBodyState extends State<GroupChatListBody> {
_GroupChatListBodyState(this._phone);
final String _phone;
@override
Widget build(BuildContext context) {
return new Text("聊天列表");
}
}
回到 class _GroupChatListState extends State<GroupChatList> {
//...
Widget build(BuildContext context) {
//...
return new Scaffold(
appBar: new AppBar(
title: new Text("纸聊"),centerTitle: true,elevation: 0.0,),drawer: drawer,body: new Center(
child: phone == "null"
? null
: new GroupChatListBody(phone: phone,myName: name),//...
);
}
}
再回到
class GroupChatListBodyItem extends StatelessWidget {
GroupChatListBodyItem(
{this.name,this.lastMessage,this.timestamp,this.messages,this.myPphone,this.shePphone});
final String name;
final String lastMessage;
final String timestamp;
final String messages;
final String myName;
final String myPphone;
final String shePphone;
@override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: () {
Navigator.of(context).push(new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new ChatScreen(
messages: messages,myName: myName,sheName: name,myPphone: myPphone,shePphone: shePphone);
},));
},child: new Container(
decoration: new BoxDecoration(),padding: new EdgeInsets.symmetric(vertical: 4.0,horizontal: 8.0),child: new Row(
children: <Widget>[
new CircleAvatar(
child: new Text(name[0]),backgroundColor: Theme.of(context).buttonColor),new Flexible(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[
new Text(" " + name,textScaleFactor: 1.2),new Text(ReadableTime(timestamp),textAlign: TextAlign.right,style: new TextStyle(
color: Theme.of(context).hintColor)),]),new Container(
padding: new EdgeInsets.only(top: 2.0),child: new Text(" " + lastMessage,overflow: TextOverflow.ellipsis,style: new TextStyle(
color: Theme.of(context).hintColor))),],))
],)));
}
}
前面的 String ReadableTime(String timestamp) {
List<String> timeList = timestamp.split(" ");
List<String> times = timeList[1].split(":");
String time;
if (new DateTime.now().toString().split(" ")[0] == timeList[0]) {
if (int.parse(times[0]) < 6) {
time = "凌晨${times[0]}:${times[1]}";
} else if (int.parse(times[0]) < 12) {
time = "上午${times[0]}:${times[1]}";
} else if (int.parse(times[0]) == 12) {
time = "中午${times[0]}:${times[1]}";
} else {
time =
"下午${(int.parse(times[0])- 12).toString().padLeft(2,'0')}:${times[1]}";
}
} else {
time = timeList[0];
}
return time;
}
在 class _GroupChatListBodyState extends State<GroupChatListBody> {
//...
DatabaseReference _chatsReference;
@override
void initState() {
super.initState();
_chatsReference = FirebaseDatabase.instance.reference().child('chats/$_phone');
FirebaseDatabase.instance.setPersistenceEnabled(true);
_chatsReference.keepSynced(true);
}
//...
}
最后修改 class _GroupChatListBodyState extends State<GroupChatListBody> {
//...
@override
Widget build(BuildContext context) {
return new FirebaseAnimatedList(
query: _chatsReference,sort: (DataSnapshot a,DataSnapshot b) =>
b.value["timestamp"].compareTo(a.value["timestamp"]),defaultChild: new CircularProgressIndicator(),itemBuilder: (BuildContext context,DataSnapshot snapshot,Animation<double> animation) {
return new SizeTransition(
sizeFactor: animation,child: new GroupChatListBodyItem(
name: snapshot.value["name"],lastMessage: snapshot.value["lastMessage"],timestamp: snapshot.value["timestamp"],messages: snapshot.value["messages"],myName: widget.myName,myPphone: _phone,shePphone: snapshot.value["phone"],);
},);
}
}
大家可以在GitHub上直接查看group_chat_list_body.dart文件、group_chat_list.dart文件和chat_screen.dart文件的代码。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |