Created
January 16, 2025 14:20
-
-
Save Lxxyx/6dc72467410695a4c9eba057e929b26a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'ELIZA Chatbot', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorSchemeSeed: Colors.blue, | |
), | |
home: const MyHomePage(title: 'ELIZA Chatbot'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final String title; | |
const MyHomePage({ | |
super.key, | |
required this.title, | |
}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final TextEditingController _textController = TextEditingController(); | |
final List<String> _conversation = []; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
children: [ | |
Expanded( | |
child: ListView.builder( | |
itemCount: _conversation.length, | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: Text(_conversation[index]), | |
); | |
}, | |
), | |
), | |
Row( | |
children: [ | |
Expanded( | |
child: TextField( | |
controller: _textController, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
hintText: 'Type your message', | |
), | |
), | |
), | |
SizedBox( | |
width: 10, | |
), | |
ElevatedButton( | |
onPressed: _handleSendMessage, | |
child: const Text('Send'), | |
), | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
void _handleSendMessage() { | |
final userInput = _textController.text; | |
_textController.clear(); | |
setState(() { | |
_conversation.add('You: $userInput'); | |
final response = _getResponse(userInput); | |
_conversation.add('ELIZA: $response'); | |
}); | |
} | |
String _getResponse(String userInput) { | |
// Implement the ELIZA logic here | |
// For now, just return a random response | |
final responses = [ | |
'I\'m not sure I understand. Can you explain?', | |
'That\'s very interesting. Can you tell me more?', | |
'I see. And how does that make you feel?', | |
]; | |
return responses[DateTime.now().millisecond % responses.length]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment