123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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<ChatScreen> {
- final List<ChatMessage> _messages = <ChatMessage>[];
- final TextEditingController _textController = TextEditingController();
- Future<void> _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: <Widget>[
- 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();
- },
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
|