ai_chat_bot_flutter 1.0.2
ai_chat_bot_flutter: ^1.0.2 copied to clipboard
A customizable Flutter AI chatbot widget with bottom sheet or full page mode, predefined prompts, and theme support.
AI Chat Bot Flutter š¤ #
A modern, customizable Flutter AI chatbot package with OpenRouter integration, local conversation history, predefined responses, and full theme control.
⨠Features #
- š¤ AI-powered chatbot using OpenRouter
- š¬ Floating chatbot launcher
- š± Two chat display modes:
- Bottom Sheet
- Full Page
- ā” Predefined quick questions
- š” Predefined instant responses
- š§ Smart response flow:
- Predefined messages
- Predefined buttons
- AI fallback
- š¾ Local conversation history
- šļø Clear chat history support
- š Automatically restores previous messages
- š§© Optional custom storage key
- šØ Modern default UI
- šØ Fully customizable colors
- š¼ļø Fully customizable icons
- āļø Customizable chatbot labels and messages
- š Optional message timestamps
- š¢ Optional assistant status indicator
- š¦ Configurable maximum stored messages
- š§© Easy integration into existing Flutter applications
šø Preview #
š Installation #
Add the package to your pubspec.yaml:
dependencies:
ai_chat_bot_flutter: ^1.0.2
Then run:
flutter pub get
š¦ Import #
import 'package:ai_chat_bot_flutter/ai_chat_bot_flutter.dart';
š OpenRouter API Token #
This package uses OpenRouter to generate AI responses.
Step 1 ā Create an OpenRouter account #
Visit:
Step 2 ā Create an API key #
- Open your OpenRouter dashboard.
- Go to API Keys.
- Select Create New Key.
- Copy your generated API key.
ā ļø Never commit your API key to a public GitHub repository.
For production applications, using a backend service to protect your API credentials is recommended.
š¾ Initialize Local Chat Storage #
Version 1.0.2 supports persistent local conversation history.
Initialize the chatbot database before runApp():
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await ChatBotLocalDatabase.init();
runApp(const MyApp());
}
The chatbot can now automatically save and restore conversations on the device.
ā” Quick Start #
import 'package:ai_chat_bot_flutter/ai_chat_bot_flutter.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await ChatBotLocalDatabase.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'AI Chat Bot Example',
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
),
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AI Chat Bot'),
),
body: Stack(
children: [
const Center(
child: Text(
'Tap the chat button to get started.',
),
),
SmartChatBotHelper(
appName: 'Facebook',
appDescription:
'Facebook is a social app where users can connect with '
'friends, chat, share posts, photos, and videos.',
token: yourOpenRouterToken,
predefinedMessages: const {
'hi':
'Hi š Welcome! How can I help you today?',
'who are you':
'Iām your assistant š¤, here to help you use the app.',
},
predefinedButtons: const {
'How do I create a post?':
"Tap 'What's on your mind?' to create a new post.",
'How do I add a friend?':
"Open a user's profile and tap 'Add Friend'.",
'How do I send a message?':
'Open Messenger, select a friend, and start chatting.',
'How do I upload a photo?':
"Tap 'Photo/Video' while creating a post.",
'How do I see notifications?':
'Tap the bell icon to view your notifications.',
},
),
],
),
);
}
}
That's all you need to get started.
š§ How Responses Work #
The chatbot follows this response flow:
- The user sends a message.
- The chatbot checks
predefinedMessages. - If a matching predefined message is found, it returns the predefined response immediately.
- If the message was triggered from a predefined button, it uses the configured predefined button response.
- Otherwise, the message is sent to OpenRouter.
- The AI response is displayed to the user.
- The conversation is saved locally when persistence is enabled.
This allows common questions to be answered instantly without using an AI request.
ā” Predefined Messages #
Use predefinedMessages for greetings or common messages:
predefinedMessages: const {
'hi':
'Hi š How can I help you today?',
'hello':
'Hello! Welcome to the app.',
'who are you':
'Iām your AI assistant.',
},
These responses do not require an OpenRouter request.
š” Predefined Quick Questions #
Use predefinedButtons to show common questions:
predefinedButtons: const {
'How do I create an account?':
'Open the registration page and enter your information.',
'How do I update my profile?':
'Open Profile and select Edit Profile.',
'How do I contact support?':
'Open Help & Support from the settings page.',
},
Users can select a question instead of typing it manually.
š± Chat Opening Modes #
The chatbot supports two display modes.
Bottom Sheet #
This is the default:
openType: ChatBotOpenType.bottomSheet,
The chatbot opens as a modal bottom sheet.
Full Page #
openType: ChatBotOpenType.page,
The chatbot opens as a full-screen page.
Example:
SmartChatBotHelper(
appName: 'My App',
appDescription: 'Description of my application.',
token: yourOpenRouterToken,
openType: ChatBotOpenType.page,
predefinedMessages: const {},
predefinedButtons: const {},
),
š¾ Local Conversation History #
Conversation persistence is enabled by default:
persistMessages: true,
Messages are stored locally on the user's device.
When the chatbot is opened again, previous messages are automatically restored.
Example:
SmartChatBotHelper(
appName: 'My App',
appDescription: 'My application description.',
token: yourOpenRouterToken,
persistMessages: true,
predefinedMessages: const {},
predefinedButtons: const {},
),
To disable local history:
persistMessages: false,
š Custom Storage Key #
Normally, you do not need to provide a storage key.
The package automatically generates one using appName.
For example:
appName: 'My App'
automatically creates a storage key similar to:
ai_chat_bot_my_app
If your application supports multiple accounts or multiple chatbot instances, you can provide your own key:
storageKey: 'support_${user.id}',
Example:
SmartChatBotHelper(
appName: 'Support',
appDescription: 'Customer support assistant.',
token: yourOpenRouterToken,
storageKey: 'support_user_123',
predefinedMessages: const {},
predefinedButtons: const {},
),
š¦ Maximum Stored Messages #
You can prevent local chat history from growing indefinitely.
Default:
maxStoredMessages: 200,
Example:
SmartChatBotHelper(
appName: 'My App',
appDescription: 'My application.',
token: yourOpenRouterToken,
maxStoredMessages: 100,
predefinedMessages: const {},
predefinedButtons: const {},
),
Only the latest configured number of messages will be retained.
šļø Clear Conversation History #
The chatbot can display a clear-history button:
showClearHistoryButton: true,
Users will receive a confirmation dialog before their history is removed.
You can also clear conversation history programmatically:
await ChatBotLocalDatabase.clearConversation(
'your_storage_key',
);
To delete every chatbot conversation stored by the package:
await ChatBotLocalDatabase.clearAll();
šØ Theme Customization #
The package includes a modern default design.
You do not need to configure a theme unless you want to match your application's branding.
Example:
SmartChatBotHelper(
appName: 'My App',
appDescription: 'My application description.',
token: yourOpenRouterToken,
predefinedMessages: const {},
predefinedButtons: const {},
theme: const ChatBotTheme(
primaryColor: Colors.green,
backgroundColor: Color(0xFFF8FAFC),
surfaceColor: Colors.white,
userMessageColor: Colors.green,
userMessageTextColor: Colors.white,
botMessageColor: Colors.white,
botMessageTextColor: Color(0xFF1E293B),
sendButtonColor: Colors.green,
launcherBackgroundColor: Colors.green,
launcherIconColor: Colors.white,
),
),
š¼ļø Icon Customization #
Chatbot icons can also be customized.
theme: const ChatBotTheme(
launcherIcon: Icons.chat_rounded,
botIcon: Icons.support_agent_rounded,
sendIcon: Icons.send_rounded,
backIcon: Icons.arrow_back_rounded,
closeIcon: Icons.close_rounded,
clearHistoryIcon:
Icons.delete_outline_rounded,
suggestionIcon:
Icons.lightbulb_outline_rounded,
),
āļø Text Customization #
Visible chatbot labels can be customized using ChatBotStrings.
SmartChatBotHelper(
appName: 'My App',
appDescription: 'My application.',
token: yourOpenRouterToken,
predefinedMessages: const {},
predefinedButtons: const {},
strings: const ChatBotStrings(
headerTitle: 'My AI Assistant',
headerStatus:
'Ready to help',
inputHint:
'Ask me anything...',
suggestionsTitle:
'You can ask',
thinkingText:
'Thinking...',
launcherTooltip:
'Open Assistant',
clearHistoryTitle:
'Delete conversation?',
clearHistoryMessage:
'Your local chat history will be permanently removed.',
clearHistoryCancel:
'Keep',
clearHistoryConfirm:
'Delete',
welcomeMessage:
'Hi š How can I help you today?',
),
),
š Message Timestamps #
Timestamps can be shown or hidden:
showMessageTimestamp: true,
Disable them with:
showMessageTimestamp: false,
š¢ Assistant Status #
The assistant header can display its status indicator.
showStatusIndicator: true,
To hide it:
showStatusIndicator: false,
The status text can be customized:
strings: const ChatBotStrings(
headerStatus: 'Available',
),
š¬ Predefined Questions Style #
Predefined questions can appear as regular horizontal quick actions:
showPredefinedAsChatMessages: false,
Or inside the chat interface:
showPredefinedAsChatMessages: true,
āļø SmartChatBotHelper Configuration #
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
appName |
String |
ā | ā | Application/assistant name |
appDescription |
String |
ā | ā | Context provided to the AI |
token |
String |
ā | ā | OpenRouter API token |
predefinedMessages |
Map<String, String> |
ā | ā | Instant predefined responses |
predefinedButtons |
Map<String, String> |
ā | ā | Suggested quick questions |
theme |
ChatBotTheme? |
ā | Default theme | Chatbot appearance |
strings |
ChatBotStrings |
ā | Default strings | Visible chatbot labels |
openType |
ChatBotOpenType |
ā | bottomSheet |
Chat opening mode |
showPredefinedAsChatMessages |
bool |
ā | false |
Show predefined questions inside chat |
showMessageTimestamp |
bool |
ā | true |
Show message timestamps |
showStatusIndicator |
bool |
ā | true |
Show assistant status |
persistMessages |
bool |
ā | true |
Save conversations locally |
storageKey |
String? |
ā | Auto-generated | Custom local storage key |
showClearHistoryButton |
bool |
ā | true |
Show clear conversation action |
maxStoredMessages |
int |
ā | 200 |
Maximum locally stored messages |
šØ Main ChatBotTheme Options #
General #
| Property | Description |
|---|---|
primaryColor |
Primary chatbot brand color |
backgroundColor |
Main chat background |
surfaceColor |
Surface/card background |
textPrimaryColor |
Main text color |
textSecondaryColor |
Secondary text color |
dividerColor |
Divider color |
shadowColor |
Shadow color |
Messages #
| Property | Description |
|---|---|
userMessageColor |
User bubble background |
userMessageTextColor |
User message text |
botMessageColor |
Assistant bubble background |
botMessageTextColor |
Assistant message text |
userTimestampTextColor |
User timestamp color |
botTimestampTextColor |
Bot timestamp color |
messageBubbleRadius |
Bubble corner radius |
Input #
| Property | Description |
|---|---|
inputBackgroundColor |
Input background |
inputBorderColor |
Input border |
inputTextColor |
Input text |
inputHintColor |
Input placeholder |
sendButtonColor |
Send button background |
sendButtonIconColor |
Send icon color |
sendIcon |
Send button icon |
Header #
| Property | Description |
|---|---|
headerBackgroundColor |
Header background |
headerIconColor |
Header action icon color |
botAvatarBackgroundColor |
Assistant avatar background |
botAvatarIconColor |
Assistant avatar icon |
onlineIndicatorColor |
Status indicator |
botIcon |
Assistant icon |
Launcher #
| Property | Description |
|---|---|
launcherBackgroundColor |
Floating launcher background |
launcherIconColor |
Launcher icon color |
launcherIcon |
Launcher icon |
launcherSize |
Launcher button size |
launcherRadius |
Launcher border radius |
launcherElevation |
Launcher shadow elevation |
launcherBottom |
Distance from bottom |
launcherRight |
Distance from right |
Suggestions #
| Property | Description |
|---|---|
predefinedButtonColor |
Suggestion background |
predefinedButtonTextColor |
Suggestion text |
predefinedButtonBorderColor |
Suggestion border |
predefinedButtonRadius |
Suggestion radius |
suggestionIcon |
Suggestions section icon |
š§© Complete Configuration Example #
SmartChatBotHelper(
appName: 'My App',
appDescription:
'My App helps users manage their daily activities.',
token: yourOpenRouterToken,
openType:
ChatBotOpenType.page,
persistMessages: true,
maxStoredMessages: 200,
showClearHistoryButton: true,
showMessageTimestamp: true,
showStatusIndicator: true,
showPredefinedAsChatMessages: true,
predefinedMessages: const {
'hi':
'Hello š How can I help you?',
},
predefinedButtons: const {
'How do I get started?':
'Open the home page and follow the setup instructions.',
},
strings: const ChatBotStrings(
headerTitle: 'My Assistant',
headerStatus: 'Ready to help',
inputHint: 'Ask a question...',
thinkingText: 'Thinking...',
),
theme: const ChatBotTheme(
primaryColor:
Color(0xFF16A34A),
userMessageColor:
Color(0xFF16A34A),
sendButtonColor:
Color(0xFF16A34A),
launcherBackgroundColor:
Color(0xFF16A34A),
launcherIcon:
Icons.chat_rounded,
botIcon:
Icons.support_agent_rounded,
sendIcon:
Icons.send_rounded,
),
),
š Local Storage #
Local conversation data is stored using hive_ce_flutter.
The data stays on the user's device and belongs to the application using the package.
No server-side conversation database is required.
Local Hive storage is not encrypted by default. If your application handles sensitive conversation data, consider adding encrypted storage appropriate to your application's security requirements.
š Security #
Never hardcode a production API key in code that will be published publicly.
Avoid:
const token = 'your-real-production-key';
especially in public repositories.
For production applications, consider routing AI requests through your own backend service so the provider API key is not distributed inside the client application.
š ļø Requirements #
- Flutter
- Dart
^3.10.4 - OpenRouter API key
The package internally uses:
httphive_ce_flutter
š Issues & Contributions #
Found a bug or have a feature request?
Open an issue on GitHub:
https://github.com/taufiqelahi/ai_chatbot/issues
Contributions and pull requests are welcome.
š® Planned Features #
Future releases may include:
- š Voice input and speech-to-text
- š File and image attachments
- š Multi-language support
- š§ Multi-turn AI context improvements
- š Chatbot usage analytics
- š Multiple AI providers such as OpenAI and Gemini
- š Optional encrypted local conversation storage
- š¤ Voice responses / text-to-speech
š License #
This project is licensed under the MIT License.
See the LICENSE file for details.
šØāš» Author #
Taufique A Elahi
GitHub: @taufiqelahi
Made with ā¤ļø for the Flutter community