import 'package:eitc_erm_app/utils/logger.dart'; import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: ChatScreen(), ); } } class ChatMessage { ChatMessage({required this.text, required this.sender}); final String sender; final String text; } class ChatScreen extends StatefulWidget { @override _ChatScreenState createState() => _ChatScreenState(); } class _ChatScreenState extends State { final List _messages = []; final TextEditingController _textController = TextEditingController(); Future _sendMessage() async { logd('111111'); ChatMessage message = ChatMessage( text: _textController.text, sender: '你', // 更真实的场景应该是获取用户信息 ); logd('2222222'); setState(() { _messages.insert(0, message); }); logd('33333333'); // _textController.text = ''; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('姓名'), centerTitle: true), body: ListView.builder( padding: EdgeInsets.all(8.0), reverse: true, itemCount: _messages.length, itemBuilder: (context, index) { final message = _messages[index]; return ListTile( title: Text(message.sender), subtitle: Text(message.text), ); }, ), bottomSheet: Container( decoration: BoxDecoration( color: Theme.of(context).cardColor, border: Border( top: BorderSide(color: Colors.grey), ), ), child: Row( children: [ Expanded( child: Padding( padding: EdgeInsets.all(8.0), child: TextField( controller: _textController, onSubmitted: (value) async { logd('Submitted text: $value'); _sendMessage(); await Future.delayed(Duration(seconds: 2)); _textController.clear(); }, decoration: InputDecoration.collapsed(hintText: '请输入'), ), ), ), Container( margin: EdgeInsets.all(8.0), child: IconButton( icon: Icon(Icons.send), onPressed: () async { _textController.text.isNotEmpty ? _sendMessage : null; await Future.delayed(Duration(seconds: 2)); _textController.clear(); }, ), ), ], ), ), ); } }