Chatbot
Loading preview
Starting the Flutter app
A Flutter chatbot UI with a typing indicator, quick-reply chips, and message bubbles.
import 'package:flutter/material.dart';
class Chatbot extends StatefulWidget {
const Chatbot({super.key});
@override
State<Chatbot> createState() => _ChatbotState();
}
class _ChatbotState extends State<Chatbot> {
final _controller = TextEditingController();
final _messages = <(String, bool)>[
('Hi! I\'m Bolt, your support assistant. How can I help today?', false),
];
bool _typing = false;
final _quickReplies = const [
'Track my order',
'Return an item',
'Talk to a human',
];
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _send() {
final text = _controller.text.trim();
if (text.isEmpty) return;
_controller.clear();
_reply(text);
}
void _reply(String text) {
setState(() {
_messages.add((text, true));
_typing = true;
});
Future.delayed(const Duration(milliseconds: 800), () {
if (!mounted) return;
setState(() {
_typing = false;
_messages.add((
'Got it — I\'ll help you with "$text". Can you share your order number?',
false
));
});
});
}
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(
title: const Text('Support Bot'),
backgroundColor: scheme.primary,
foregroundColor: scheme.onPrimary,
),
body: Column(
children: [
Expanded(
child: ListView(
padding: const EdgeInsets.all(16),
children: [
for (final (text, fromUser) in _messages)
Align(
alignment:
fromUser ? Alignment.centerRight : Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(vertical: 5),
padding: const EdgeInsets.symmetric(
horizontal: 14, vertical: 10),
constraints: const BoxConstraints(maxWidth: 280),
decoration: BoxDecoration(
color: fromUser
? scheme.primary
: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(16),
),
child: Text(text,
style: TextStyle(
color: fromUser
? scheme.onPrimary
: scheme.onSurface)),
),
),
if (_typing)
Align(
alignment: Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(vertical: 5),
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(16),
),
child: const Text('Bolt is typing…'),
),
),
],
),
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Row(
children: [
for (final q in _quickReplies)
Padding(
padding: const EdgeInsets.only(right: 8),
child: ActionChip(label: Text(q), onPressed: () => _reply(q)),
),
],
),
),
const SizedBox(height: 8),
SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.fromLTRB(12, 0, 12, 8),
child: Row(
children: [
Expanded(
child: TextField(
controller: _controller,
textInputAction: TextInputAction.send,
onSubmitted: (_) => _send(),
decoration: const InputDecoration(
hintText: 'Type a message',
border: OutlineInputBorder(),
isDense: true,
),
),
),
const SizedBox(width: 8),
IconButton.filled(
tooltip: 'Send message',
onPressed: _send, icon: const Icon(Icons.send)),
],
),
),
),
],
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
Messages are (String, bool) records in _messages, and _reply is the single entry point: it adds the user’s text, shows ‘Bolt is typing…’ and answers 800 ms later with a template echoing that text. The ActionChip quick replies call it directly. _send reads the TextEditingController, ignores blank input, clears the field and passes the text on, from both the send button and the keyboard.
Adapting it
- Replace the
Future.delayedin_replywith your support backend, and clear_typingon errors too. - Return
_quickRepliesfrom the server if they should change as the conversation moves on.