Requirements
Flutter 3.27 or newer, and Dart SDK 3.6 or newer. Works on iOS, Android and web.
Install
Not on pub.dev yet
pub.dev publishing is coming. Until then, install from git or from a local checkout. The pub.dev tab below will work once the package is published.
flutter pub add 'kili_flutter:{"git":{"url":"https://github.com/patrickishaf/kili-core.git","path":"libs/flutter"}}'Quick start
import 'package:kili_flutter/kili_flutter.dart';
class SupportPage extends StatelessWidget {
const SupportPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: SafeArea(
child: KiliChat(
config: KiliConfig(
apiBaseUrl: 'https://api.kili.africa',
apiKey: 'YOUR_API_KEY',
apiSecret: 'YOUR_API_SECRET',
),
),
),
);
}
}There's no stylesheet to import. The design system is built in, including the fonts. The widget also shows nothing until the customer session is ready. If setup fails, it stays blank without an error message.
Configuration
KiliConfig is a const class with these fields:
| Option | Type | Required | Description |
|---|---|---|---|
| apiBaseUrl | String | required | Base URL of the Kili REST API, for example https://api.kili.africa. |
| socketBaseUrl | String? | optional | Base URL of the realtime server. Defaults to apiBaseUrl. |
| apiKey | String | required | Your organisation's API key. |
| apiSecret | String | required | Your organisation's API secret. |
KiliChat also takes an optional brightness parameter:
| Option | Type | Required | Description |
|---|---|---|---|
| brightness | Brightness? | optional | Brightness.light or Brightness.dark. When null, the widget follows the host app's Theme. Pass a value to fix the theme, as the web SDK does with a .dark class. |
Keep the values out of your source code. Use compile-time environment variables, as in the example app:
import 'package:kili_flutter/kili_flutter.dart';
const apiBaseUrl = String.fromEnvironment('API_BASE_URL');
const apiKey = String.fromEnvironment('API_KEY');
const apiSecret = String.fromEnvironment('API_SECRET');
const kiliConfig = KiliConfig(
apiBaseUrl: apiBaseUrl,
apiKey: apiKey,
apiSecret: apiSecret,
);{
"API_BASE_URL": "https://api.kili.africa",
"API_KEY": "YOUR_API_KEY",
"API_SECRET": "YOUR_API_SECRET"
}flutter run --dart-define-from-file=env.jsonKeep the key and secret private
Never commit the key or secret. A client-side SDK runs in the app, so these values are included in your app build. The widget also sends them with every request and socket connection, so anyone with your app can read them.
Not configurable yet
There are no parameters for a brand name, greeting text, colours or launcher position. The SDK also can't take a signed-in user's name or email yet.
Placing the widget
The widget fills its parent, so give it a space: a page, a bottom sheet or a fixed-size box. A pushed route is the simplest choice:
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const SupportPage()),
);To show it in a bottom sheet that covers most of the screen:
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
builder: (context) => SizedBox(
height: MediaQuery.sizeOf(context).height * 0.9,
child: const KiliChat(config: kiliConfig),
),
);Composer keys
On desktop and web, Enter sends the message and Shift + Enter adds a new line. On Android and iOS, the keyboard's action key sends.
Fonts
The Space Grotesk and DM Sans fonts are fetched at runtime with google_fonts. To bundle them instead, turn off runtime fetching and follow the google_fonts guide for bundling fonts:
import 'package:google_fonts/google_fonts.dart';
void main() {
GoogleFonts.config.allowRuntimeFetching = false;
runApp(const MyApp());
}Local development
On the Android emulator, localhost points at the emulator, not your computer. Use http://10.0.2.2:<port> instead, where <port> is your local API port.
The example app in the SDK repository reads its keys from env.json, as shown above. Copy env.example.json to env.json, fill in your keys, and run it with --dart-define-from-file=env.json.
Building your own UI
The building blocks include AppStateProvider and useApp(context), KiliTheme, KiliColors, KiliText, KiliRadius, createKiliSocket, the http Dio client, chatService and coreService. Widgets that read state must sit under an AppStateProvider, and widgets that pick a theme must sit under a KiliTheme.
import 'package:flutter/material.dart';
import 'package:kili_flutter/kili_flutter.dart';
class ConversationCount extends StatelessWidget {
const ConversationCount({super.key});
@override
Widget build(BuildContext context) {
// Read the session state that KiliChat provides.
final state = useApp(context);
final colors = KiliTheme.of(context);
return Text(
'${state.chats.length} conversations',
style: KiliText.body(color: colors.foreground),
);
}
}How the web APIs map
The Flutter SDK is a port of the React SDK, with the same widget, logic and styling. These are the equivalents:
| React SDK | Flutter SDK |
|---|---|
| localStorage | localStorage (shared_preferences, keys prefixed kili.) |
| sessionStorage | sessionStorage (in memory, for the app's lifetime) |
| axios + interceptor | http, a Dio client with an interceptor |
| socket.io-client | socket_io_client |
| React context + hooks | InheritedNotifier + useApp(context) |
| useIsMobile() | useIsMobile(context) |
| Tailwind classes | KiliColors, KiliText, KiliRadius |