SDK

kili_flutter

Flutter SDK

Add Kili's chat widget to a Flutter app for iOS, Android and web with kili_flutter. Conversations go to your team's Kili inbox in real time.

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.

terminal
flutter pub add 'kili_flutter:{"git":{"url":"https://github.com/patrickishaf/kili-core.git","path":"libs/flutter"}}'

Quick start

support_page.dart
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:

OptionTypeRequiredDescription
apiBaseUrlStringrequiredBase URL of the Kili REST API, for example https://api.kili.africa.
socketBaseUrlString?optionalBase URL of the realtime server. Defaults to apiBaseUrl.
apiKeyStringrequiredYour organisation's API key.
apiSecretStringrequiredYour organisation's API secret.

KiliChat also takes an optional brightness parameter:

OptionTypeRequiredDescription
brightnessBrightness?optionalBrightness.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:

config.dart
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,
);
env.json
{
  "API_BASE_URL": "https://api.kili.africa",
  "API_KEY": "YOUR_API_KEY",
  "API_SECRET": "YOUR_API_SECRET"
}
terminal
flutter run --dart-define-from-file=env.json

Keep 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:

navigation.dart
Navigator.of(context).push(
  MaterialPageRoute(builder: (_) => const SupportPage()),
);

To show it in a bottom sheet that covers most of the screen:

sheet.dart
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:

main.dart
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.

conversation_count.dart
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 SDKFlutter SDK
localStoragelocalStorage (shared_preferences, keys prefixed kili.)
sessionStoragesessionStorage (in memory, for the app's lifetime)
axios + interceptorhttp, a Dio client with an interceptor
socket.io-clientsocket_io_client
React context + hooksInheritedNotifier + useApp(context)
useIsMobile()useIsMobile(context)
Tailwind classesKiliColors, KiliText, KiliRadius