mirror of
https://github.com/foss42/apidash.git
synced 2025-12-04 03:46:57 +08:00
Integrated AI Chatbot Widget
This commit is contained in:
98
lib/widgets/chatbot_widget.dart
Normal file
98
lib/widgets/chatbot_widget.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:apidash/providers/providers.dart';
|
||||
|
||||
class ChatbotWidget extends ConsumerStatefulWidget {
|
||||
const ChatbotWidget({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_ChatbotWidgetState createState() => _ChatbotWidgetState();
|
||||
}
|
||||
|
||||
class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
final List<Map<String, dynamic>> _messages = [];
|
||||
|
||||
void _sendMessage(String message) async {
|
||||
final ollamaService = ref.read(ollamaServiceProvider);
|
||||
|
||||
setState(() {
|
||||
_messages.add({'role': 'user', 'message': message});
|
||||
_controller.clear();
|
||||
});
|
||||
|
||||
final response = await ollamaService.generateResponse(message);
|
||||
|
||||
setState(() {
|
||||
_messages.add({'role': 'bot', 'message': response});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
height: 400,
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black12,
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: _messages.length,
|
||||
itemBuilder: (context, index) {
|
||||
final message = _messages[index];
|
||||
return Align(
|
||||
alignment: message['role'] == 'user'
|
||||
? Alignment.centerRight
|
||||
: Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 4),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: message['role'] == 'user'
|
||||
? Theme.of(context).colorScheme.primaryContainer
|
||||
: Theme.of(context).colorScheme.secondaryContainer,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(message['message']),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _controller,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Ask about API responses, debug issues...',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
onSubmitted: _sendMessage,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.send),
|
||||
onPressed: () => _sendMessage(_controller.text),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user