You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

130 regels
3.6 KiB

  1. /*
  2. AI测试场景
  3. */
  4. import 'package:flutter/material.dart';
  5. import 'package:demo001/doubao/DouBao.dart';
  6. class ChatItem {
  7. String msg = "";
  8. int state = 0;
  9. ChatItem({required this.msg});
  10. void append(String _msg) {
  11. msg += _msg;
  12. }
  13. void end() {
  14. state = 1;
  15. }
  16. }
  17. class AIChatScene extends StatefulWidget {
  18. @override
  19. _AIChatSceneState createState() => _AIChatSceneState();
  20. }
  21. class _AIChatSceneState extends State<AIChatScene> {
  22. // final String apiKey = "sk-3adfd188a3134e718bbf704f525aff17";
  23. final Doubao doubao = Doubao(
  24. apiKey: "418ec475-e2dc-4b76-8aca-842d81bc3466",
  25. modelId: "ep-20250203161136-9lrxg");
  26. final List<ChatItem> _chats = [ChatItem(msg: "我是测试代码")];
  27. ChatItem? _currchat;
  28. final TextEditingController _textController = TextEditingController();
  29. //发送消息
  30. _sendMessage() async {
  31. _currchat = ChatItem(msg: "");
  32. setState(() {
  33. _chats.add(_currchat!);
  34. });
  35. var stream = doubao.chat(_textController.text);
  36. _textController.text = "";
  37. await for (final content in stream) {
  38. setState(() {
  39. _currchat?.append(content);
  40. });
  41. print('实时更新: $content');
  42. }
  43. print('结束恢复');
  44. _currchat?.end();
  45. }
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. appBar: AppBar(title: Text("AI测试")),
  50. body: ListView.builder(
  51. itemCount: _chats.length,
  52. itemBuilder: (context, index) {
  53. var item = _chats[index];
  54. return _buildAudioMessage(item);
  55. },
  56. ),
  57. bottomNavigationBar: Padding(
  58. padding: const EdgeInsets.all(20.0),
  59. child: Container(
  60. decoration: BoxDecoration(
  61. color: Colors.grey[200], // 背景颜色
  62. borderRadius: BorderRadius.circular(30), // 圆角
  63. ),
  64. padding: EdgeInsets.symmetric(
  65. horizontal: 20, vertical: 10), // 输入框和按钮的内边距
  66. child: Row(
  67. children: [
  68. Expanded(
  69. child: TextField(
  70. controller: _textController, // 用于获取输入的文本
  71. decoration: InputDecoration(
  72. hintText: '输入消息...', // 提示文本
  73. border: InputBorder.none, // 去除默认边框
  74. contentPadding:
  75. EdgeInsets.symmetric(vertical: 12), // 调整内边距
  76. ),
  77. ),
  78. ),
  79. IconButton(
  80. icon: Icon(Icons.send, color: Colors.blue), // 发送按钮图标
  81. onPressed: _sendMessage, // 发送按钮点击事件
  82. ),
  83. ],
  84. ),
  85. ),
  86. ));
  87. }
  88. // 构建语音消息
  89. Widget _buildAudioMessage(ChatItem data) {
  90. return Padding(
  91. padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
  92. child: Column(
  93. crossAxisAlignment: CrossAxisAlignment.start,
  94. children: [
  95. // 音频播放按钮
  96. GestureDetector(
  97. onTap: () {},
  98. child: Container(
  99. padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
  100. decoration: BoxDecoration(
  101. color: Colors.green,
  102. borderRadius: BorderRadius.circular(30),
  103. ),
  104. child: Row(
  105. children: [
  106. Text(
  107. data.msg,
  108. style: TextStyle(color: Colors.white),
  109. ),
  110. ],
  111. ),
  112. ),
  113. ),
  114. SizedBox(height: 5),
  115. ],
  116. ),
  117. );
  118. }
  119. }