brace_chat 0.1.0
brace_chat: ^0.1.0 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)));
}