check_screen.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import 'package:eitc_erm_app/utils/logger.dart';
  2. import 'package:flutter/material.dart';
  3. void main() => runApp(MyApp());
  4. class MyApp extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return MaterialApp(
  8. home: ChatScreen(),
  9. );
  10. }
  11. }
  12. class ChatMessage {
  13. ChatMessage({required this.text, required this.sender});
  14. final String sender;
  15. final String text;
  16. }
  17. class ChatScreen extends StatefulWidget {
  18. @override
  19. _ChatScreenState createState() => _ChatScreenState();
  20. }
  21. class _ChatScreenState extends State<ChatScreen> {
  22. final List<ChatMessage> _messages = <ChatMessage>[];
  23. final TextEditingController _textController = TextEditingController();
  24. Future<void> _sendMessage() async {
  25. logd('111111');
  26. ChatMessage message = ChatMessage(
  27. text: _textController.text,
  28. sender: '你', // 更真实的场景应该是获取用户信息
  29. );
  30. logd('2222222');
  31. setState(() {
  32. _messages.insert(0, message);
  33. });
  34. logd('33333333');
  35. // _textController.text = '';
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return Scaffold(
  40. appBar: AppBar(title: Text('姓名'), centerTitle: true),
  41. body: ListView.builder(
  42. padding: EdgeInsets.all(8.0),
  43. reverse: true,
  44. itemCount: _messages.length,
  45. itemBuilder: (context, index) {
  46. final message = _messages[index];
  47. return ListTile(
  48. title: Text(message.sender),
  49. subtitle: Text(message.text),
  50. );
  51. },
  52. ),
  53. bottomSheet: Container(
  54. decoration: BoxDecoration(
  55. color: Theme.of(context).cardColor,
  56. border: Border(
  57. top: BorderSide(color: Colors.grey),
  58. ),
  59. ),
  60. child: Row(
  61. children: <Widget>[
  62. Expanded(
  63. child: Padding(
  64. padding: EdgeInsets.all(8.0),
  65. child: TextField(
  66. controller: _textController,
  67. onSubmitted: (value) async {
  68. logd('Submitted text: $value');
  69. _sendMessage();
  70. await Future.delayed(Duration(seconds: 2));
  71. _textController.clear();
  72. },
  73. decoration: InputDecoration.collapsed(hintText: '请输入'),
  74. ),
  75. ),
  76. ),
  77. Container(
  78. margin: EdgeInsets.all(8.0),
  79. child: IconButton(
  80. icon: Icon(Icons.send),
  81. onPressed: () async {
  82. _textController.text.isNotEmpty ? _sendMessage : null;
  83. await Future.delayed(Duration(seconds: 2));
  84. _textController.clear();
  85. },
  86. ),
  87. ),
  88. ],
  89. ),
  90. ),
  91. );
  92. }
  93. }