Skip to content

Instantly share code, notes, and snippets.

@Lxxyx
Created January 8, 2025 09:45
Show Gist options
  • Save Lxxyx/58cf2daff476de93effe8df61821176b to your computer and use it in GitHub Desktop.
Save Lxxyx/58cf2daff476de93effe8df61821176b to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Chatbot Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: Colors.blue,
),
home: const ChatScreen(),
);
}
}
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final TextEditingController _messageController = TextEditingController();
final List<ChatMessage> _messages = [];
void _sendMessage() {
if (_messageController.text.isNotEmpty) {
setState(() {
_messages.add(ChatMessage(
text: _messageController.text,
isUserMessage: true,
));
_messageController.clear();
});
// Here you can add logic to send the message to a server or process it locally
_handleResponse();
}
}
void _handleResponse() {
// Simulate a response from the bot
Future.delayed(const Duration(milliseconds: 500), () {
setState(() {
_messages.add(const ChatMessage(
text: 'Hello! I\'m Bundle Bot. How can I assist you today?',
isUserMessage: false,
));
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Bundle Bot'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _messages.length,
itemBuilder: (context, index) {
return ChatBubble(
message: _messages[index],
);
},
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: _messageController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Type a message...',
),
),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: _sendMessage,
child: const Text('Send'),
),
],
),
),
],
),
);
}
}
class ChatMessage {
final String text;
final bool isUserMessage;
const ChatMessage({required this.text, required this.isUserMessage});
}
class ChatBubble extends StatelessWidget {
final ChatMessage message;
const ChatBubble({required this.message});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment:
message.isUserMessage ? MainAxisAlignment.end : MainAxisAlignment.start,
children: [
if (!message.isUserMessage)
const Icon(
Icons.person,
size: 24,
color: Colors.grey,
),
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
color: message.isUserMessage ? Colors.blue[200] : Colors.grey[200],
borderRadius: BorderRadius.all(Radius.circular(16)),
),
child: Text(message.text),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment