Flutter实战一Flutter聊天应用(二十)
在上一篇文章《Flutter实战一Flutter聊天应用(十九)》中,我们完成了删除用户的逻辑,就是将会话的有效性设置为 打开 class _AddSessionState extends State<AddSession> {
//...
void _writeNewSession(String time,String message) {
chatsReference.child('$_myPhone/$_searchPhone').set({
"name": _searchUsername,"phone": _searchPhone,"messages": "$_myPhone$_searchPhone$message","lastMessage": "一起来聊天吧!","timestamp": time,"activate": "true"
});
chatsReference.child('$_searchPhone/$_myPhone').set({
"name": widget.myName,"phone": _myPhone,"activate": "true"
});
}
//...
}
然后再修改 class _AddSessionState extends State<AddSession> {
//...
Future<int> _addSession() async {
String time = new DateTime.now().toString();
int random = new Random().nextInt(100000);
String message = time.split(' ')[0].replaceAll('-','') + random.toString();
return await chatsReference
.child('$_myPhone/$_searchPhone')
.once()
.then((DataSnapshot onValue) {
if (onValue.value == null) {
_writeNewSession(time,message);
return 1;
} else {
if (onValue.value["activate"] == "true") {
return 0;
} else {
_writeNewSession(time,message);
return 2;
}
}
});
}
//...
}
如果上面的结果为 //...
import 'chat_screen.dart';
//...
class _GroupChatListState extends State<GroupChatList> {
//...
void _floatingButtonCallback() {
showDialog<List<String>>(
context: context,barrierDismissible: false,child: new AddSession(phone,name)).then((List<String> onValue) {
if (onValue != null) {
Navigator.of(context).push(new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new ChatScreen(
messages: onValue[2],myName: name,sheName: onValue[0],myPhone: phone,shePhone: onValue[1]);
},));
}
});
}
//...
}
在上面代码中,我们将添加屏幕的返回值设置成 //...
class _AddSessionState extends State<AddSession> {
//...
String _searchMessages = "";
//...
Future<int> _addSession() async {
//...
return await chatsReference
.child('$_myPhone/$_searchPhone')
.once()
.then((DataSnapshot onValue) {
if (onValue.value == null) {
_writeNewSession(time,message);
return 1;
} else {
if (onValue.value["activate"] == "true") {
_searchMessages = onValue.value["messages"];
return 0;
} else {
_writeNewSession(time,message);
return 2;
}
}
});
}
//...
}
上面添加了一个 class _AddSessionState extends State<AddSession> {
//...
void _handleAppend() {
showDialog<int>(
context: context,barrierDismissible: false,child: new ShowAwait(_addSession())).then((int onValue) {
if (onValue == 1 || onValue == 2) {
Navigator.of(context).pop(null);
} else if (onValue == 0) {
Navigator
.of(context)
.pop([_searchUsername,_searchPhone,_searchMessages]);
}
});
}
//...
}
在上面代码中,当 上面的图片展示的是当前添加屏幕的错误提示样式,使用的是弹窗提示,可是我们的添加屏幕本身就是以弹出窗口形式打开的,再使用弹窗提示,不就是两层弹窗了。我们需要对错误提示样式进行修改,修改 class _AddSessionState extends State<AddSession> {
//...
String _errorPrompt = "";
//...
@override
Widget build(BuildContext context) {
return new SimpleDialog(title: new Text("添加会话"),children: <Widget>[
new Container(
//...
),_errorPrompt == ""
? new Text("")
: new Center(
child: new Text(
_errorPrompt,style: new TextStyle(color: Colors.red),)),_searchUsername == ""
? new Text("")
: new Container(
//...
),//...
]);
}
}
在上面代码中,增加了一个 setState(() {
_errorPrompt = "该用户不存在!";
_searchUsername = "";
});
setState(() {
_errorPrompt = "";
});
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |