Skip to main content

brace_chat 0.1.1 copy "brace_chat: ^0.1.1" to clipboard
brace_chat: ^0.1.1 copied to clipboard

Brace live chat for Flutter apps. Opens your Brace chat widget in a web view, with the signed-in user, identity verification and chat events.

example/lib/main.dart

import 'package:brace_chat/brace_chat.dart';
import 'package:flutter/material.dart';

// Your Live Chat channel's website token: Settings > Channels > Live Chat > Installation.
const websiteToken = String.fromEnvironment(
  'BRACE_WEBSITE_TOKEN',
  defaultValue: 'YOUR_WEBSITE_TOKEN',
);

final navigatorKey = GlobalKey<NavigatorState>();

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  BraceChat.instance.configure(
    BraceChatConfig(websiteToken: websiteToken),
    navigatorKey: navigatorKey,
  );
  BraceChat.instance.events.listen((e) {
    if (e is BraceChatUnavailable) debugPrint('chat is paused');
  });
  runApp(MaterialApp(navigatorKey: navigatorKey, home: const Home()));
}

class Home extends StatelessWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    final chat = BraceChat.instance;
    return Scaffold(
      appBar: AppBar(title: const Text('Brace chat sample')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          spacing: 16,
          children: [
            ValueListenableBuilder<BraceChatState>(
              valueListenable: chat.state,
              builder: (context, state, _) => state.isAvailable
                  ? FilledButton(
                      onPressed: () => chat.open(context),
                      child: const Text('Chat with us'),
                    )
                  : const Text('Chat is not available right now.'),
            ),
            OutlinedButton(
              // In a real app the identity hash comes from YOUR server, never from the app.
              onPressed: () {
                chat.setUser(
                  const BraceChatUser(
                    identifier: 'user-42',
                    name: 'Test User',
                    email: 'test.user@example.com',
                  ),
                );
                _confirm(
                  context,
                  'Signed in as Test User. Open the chat to see it.',
                );
              },
              child: const Text('Sign in as a test user'),
            ),
            TextButton(
              onPressed: () {
                chat.reset();
                _confirm(
                  context,
                  'Signed out. The chat starts fresh next time.',
                );
              },
              child: const Text('Sign out'),
            ),
          ],
        ),
      ),
    );
  }
}

// Both calls change only what the chat shows next time it opens, so say that they happened.
void _confirm(BuildContext context, String text) {
  ScaffoldMessenger.of(context)
    ..hideCurrentSnackBar()
    ..showSnackBar(SnackBar(content: Text(text)));
}
0
likes
160
points
340
downloads

Documentation

API reference

Publisher

verified publisherbrace.chat

Weekly Downloads

Brace live chat for Flutter apps. Opens your Brace chat widget in a web view, with the signed-in user, identity verification and chat events.

Homepage

Topics

#chat #live-chat #helpdesk #customer-support

License

MIT (license)

Dependencies

file_selector, flutter, url_launcher, webview_flutter, webview_flutter_android, webview_flutter_wkwebview

More

Packages that depend on brace_chat