Flutter实战一Flutter聊天应用(二)
随着项目的代码越来越多,我们会碰到各种问题,所以我们需要学习一下如何使用IntelliJ定位并解决问题。我们可以在IntelliJ IDE上调试在模拟器/仿真器或真机设备上运行的Flutter应用程序,使用IntelliJ编辑器可以:
IntelliJ编辑器在您的应用程序运行时会显示系统日志,并提供了一个调试器UI来处理断点并控制执行流程。还可以使用断点来调试Flutter应用程序:
IntelliJ编辑器启动调试器UI,并在应用程序到达断点时暂停执行。然后,我们可以使用调试器UI中的控制器来确定错误的原因。 在这篇文章中,我们要创建一个显示用户聊天消息的窗口控件,将创建和组合多个较小的控件。从一个表示单个聊天消息的控件开始,将该控件嵌套在父级可滚动列表中,并将可滚动列表嵌套在基本应用程序脚手架(Scaffold)中。 首先,我们需要一个表示单个聊天消息的控件,定义一个名为 我们还需要定义 const String _name = "hekaiyou";
class ChatMessage extends StatelessWidget {
ChatMessage({this.text});
final String text;
@override
Widget build(BuildContext context) {
return new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),child: new Row(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
new Container(
margin: const EdgeInsets.only(right: 16.0),child: new CircleAvatar(child: new Text(_name[0])),),new Column(
crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
new Text(_name,style: Theme.of(context).textTheme.subhead),new Container(
margin: const EdgeInsets.only(top: 5.0),child: new Text(text),)
]
)
]
)
);
}
}
为使CircleAvatar控件看起来更有个性,我们将_name变量的值的第一个字符传递给子Text控件。我们将使用CrossAxisAlignment.start作为Row构造函数的crossAxisAlignment参数来定位头像和消息,相对于其父窗口控件。 对于头像,父对象是一个 在头像旁边,垂直对齐两个 我们尚未为此应用程序指定主题,因此 接下来的我们要做的是获取聊天消息列表,并在UI中显示。此列表是可以滚动的,以便用户可以查看聊天记录。列表还应按时间顺序显示消息,最近的消息显示在可见列表的最下面。 在我们的 class ChatScreenState extends State<ChatScreen> {
final List<ChatMessage> _messages = <ChatMessage>[];
final TextEditingController _textController = new TextEditingController();
//...
在当前用户从文本字段发送消息时,我们的应用程序应该将新消息添加到消息列表中。因此需要修改 class ChatScreenState extends State<ChatScreen> {
//...
void _handleSubmitted(String text) {
_textController.clear();
ChatMessage message = new ChatMessage(
text: text,);
setState((){
_messages.insert(0,message);
});
}
//...
}
您调用 一般来说,可以在一些私有数据改变之后,再使用一个空的闭包调用 现在可以显示聊天消息列表了,我们将从 在我们的 class ChatScreenState extends State<ChatScreen> {
//...
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('谈天说地'),body: new Column(
children: <Widget>[
new Flexible(
child: new ListView.builder(
padding: new EdgeInsets.all(8.0),reverse: true,itemBuilder: (_,int index) => _messages[index],itemCount: _messages.length,)
),new Divider(height: 1.0),new Container(
decoration: new BoxDecoration(
color: Theme.of(context).cardColor,child: _buildTextComposer(),)
]
)
);
}
//...
}
将参数传递给
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- React Native优秀的第三方插件(Android & iOS)
- react-native – App Store和Play Store如何处理恶意代码推
- 解决SWFUpload上传文件组件使用时报告2049错误
- Qemu&KVM第二篇之(3)使用virsh xml file 创建VM
- c – SECURITY_ATTRIBUTES结构和CreateNamedPipe()
- React 基础语法
- Ajax学习二
- 报错:Binary XML file line #7: Error inflating class an
- Flexbox 设计指南
- excel模仿word邮件合并输出xls文件或打印