可以用flutter写一个和chatgpt的聊天界面吗。我的要求是界面模仿telegram你需要封装发送消息接收消息消息等待可以从json加载消息我的消息接收是流式传输的我要求是你把接收的消息实时展示到对话上我的消息请求是 Response response = await Diopost httpschatgpt-chatgpt-lswirmtbkxus-east-1fcappru
可以使用Flutter来实现一个类似Telegram的聊天界面,并封装发送和接收消息的功能,同时支持从JSON加载消息和实时展示接收到的消息。对于接收到的消息,我们可以使用Dio库来进行流式传输,并在聊天界面上实时展示。下面是一个示例代码实现:
import 'dart:async'; import 'dart:convert';
import 'package:dio/dio.dart'; import 'package:flutter/material.dart';
class ChatGPTChatScreen extends StatefulWidget { const ChatGPTChatScreen({Key? key}) : super(key: key);
@override _ChatGPTChatScreenState createState() => _ChatGPTChatScreenState(); }
class _ChatGPTChatScreenState extends State
@override void initState() { super.initState(); _connectToChatGPT(); }
void _connectToChatGPT() async { Response response = await Dio().post( 'https://chatgpt-chatgpt-lswirmtbkx.us-east-1.fcapp.run/test', data: formData, options: Options( headers: headers, responseType: ResponseType.stream, ), );
Stream<List<int>> stream = response.data.stream;
stream.transform(utf8.decoder).transform(LineSplitter()).listen(
(String line) {
Map<String, dynamic> messageData = jsonDecode(line);
ChatMessage message = ChatMessage.fromJson(messageData);
setState(() {
_messages.add(message);
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: Duration(milliseconds: 500),
curve: Curves.easeInOut,
);
});
},
onError: (error) {
print(error);
},
onDone: () {
print('Done!');
},
);
}
void _handleSubmitted(String text) { _textController.clear(); ChatMessage message = ChatMessage( text: text, isMe: true, animationController: AnimationController( duration: Duration(milliseconds: 300), vsync: this, ), ); setState(() { _messages.insert(0, message); }); message.animationController.forward(); // TODO: Send message to ChatGPT }
@override void dispose() { for (ChatMessage message in _messages) { message.animationController.dispose(); } super.dispose(); }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ChatGPT Chat'),
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/background.png'),
fit: BoxFit.cover,
),
),
child: Column(
children:
Widget _buildTextComposer() {
return IconTheme(
data: IconThemeData(color: Theme.of(context).accentColor),
child: Container(
margin: EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
children:
class ChatMessage extends StatelessWidget { ChatMessage({ required this.text, required this.isMe, required this.animationController, });
final String text; final bool isMe; final AnimationController animationController;
@override
Widget build(BuildContext context) {
return SizeTransition(
sizeFactor: CurvedAnimation(
parent: animationController,
curve: Curves.easeInOut,
),
axisAlignment: 0.0,
child: Container(
margin: EdgeInsets.symmetric(vertical: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children:
factory ChatMessage.fromJson(Map<String, dynamic> json) { return ChatMessage( text: json['text'], isMe: json['isMe'], animationController: AnimationController( duration: Duration(milliseconds: 300), vsync: null, ), ); } }
注意:上述代码中的 formData 和 headers 是从你的 ChatGPT API 获取到的,需要替换为你自己的参数。另外,代码中的 background.png 和 primaryColorDark 也可以替换为你自己的样式
原文地址: https://www.cveoy.top/t/topic/fDjZ 著作权归作者所有。请勿转载和采集!