chat360_flutter_sdk 0.1.0 copy "chat360_flutter_sdk: ^0.1.0" to clipboard
chat360_flutter_sdk: ^0.1.0 copied to clipboard

A Flutter package that wraps the Chat360 web client in a WebView.

example/lib/main.dart

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

void main() => runApp(const ExampleApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ExampleHome(),
    );
  }
}

class ExampleHome extends StatefulWidget {
  const ExampleHome({super.key});

  @override
  State<ExampleHome> createState() => _ExampleHomeState();
}

class _ExampleHomeState extends State<ExampleHome> {
  // Sample config values — replace with real values when testing against your bot
  final _botIdController = TextEditingController(text: 'ce6e990d-0d8e-409f-83b9-53deae65279e');
  final _appIdController = TextEditingController(text: '');
  final _baseUrlController = TextEditingController(text: 'https://app.chat360.io');

  bool _configSet = false;
  bool _isDebug = true;
  bool _useNewUI = false;
  bool _useCustomBaseUrl = true;

  @override
  void dispose() {
    _botIdController.dispose();
    _appIdController.dispose();
    _baseUrlController.dispose();
    super.dispose();
  }

  void _setConfig() {
    final cfg = Chat360Config(
      botId: _botIdController.text,
      appId: _appIdController.text,
      isDebug: _isDebug,
      useNewUI: _useNewUI,
      baseUrl: _useCustomBaseUrl ? _baseUrlController.text : 'https://app.chat360.io',
    );
    Chat360Bot.shared.setConfig(cfg);
    setState(() => _configSet = true);
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Config set')));
  }

  void _setLocationProvider() {
    Chat360Bot.shared.onLocationNeeded = (callbackId) async {
      // Return a static location for demo purposes
      return {'latitude': '12.34567', 'longitude': '76.54321'};
    };
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Location provider set')));
  }

  void _setWindowHandler() {
    Chat360Bot.shared.handleWindowEvents = (body) async {
      // Echo back metadata
      return {'received_key': body['example'] ?? 'none'};
    };
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Window handler set')));
  }

  Future<void> _startChat() async {
    if (!_configSet) {
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please set config first')));
      return;
    }

    await Chat360Bot.shared.startChatbot(
      context,
      title: 'Support',
      onBackClick: () => ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Chat closed'))),
      header: Container(
        color: Colors.grey.shade200,
        padding: const EdgeInsets.all(12),
        child: const Text('Example header — customize me'),
      ),
    );
  }

  Future<void> _sendEventToBot() async {
    await Chat360Bot.shared.sendEventToBot({'example': 'hello-from-flutter'});
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Sent CHAT360_WINDOW_EVENT')));
  }

  Future<void> _sendJsBridgeEvent() async {
    // Use the public convenience that delegates to the JS bridge internally
    await Chat360Bot.shared.sendEventToBot({'from': 'dart-bridge'});
    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Sent via Chat360Bot.sendEventToBot')));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Chat360 Flutter SDK — Example')),
      body: SingleChildScrollView(
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const Text('Configuration', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 8),
              TextField(controller: _botIdController, decoration: const InputDecoration(labelText: 'Bot ID')),
              const SizedBox(height: 8),
              TextField(controller: _appIdController, decoration: const InputDecoration(labelText: 'App ID')),
              const SizedBox(height: 8),
              Row(
                children: [
                  Expanded(child: TextField(controller: _baseUrlController, decoration: const InputDecoration(labelText: 'Base URL'))),
                  const SizedBox(width: 8),
                  // SizedBox(
                  //   width: 60,
                  //   height: 60,
                  //   child: Column(
                  //     children: [
                  //       CheckboxListTile(
                  //         title: const Text('Use custom baseUrl'),
                  //         value: _useCustomBaseUrl,
                  //         onChanged: (v) => setState(() => _useCustomBaseUrl = v ?? true),
                  //         controlAffinity: ListTileControlAffinity.leading,
                  //         contentPadding: EdgeInsets.zero,
                  //       ),
                  //     ],
                  //   ),
                  // ),
                ],
              ),
              const SizedBox(height: 8),
          
              CheckboxListTile(
                title: const Text('Debug mode (isDebug)'),
                value: _isDebug,
                onChanged: (v) => setState(() => _isDebug = v ?? false),
                controlAffinity: ListTileControlAffinity.leading,
                contentPadding: EdgeInsets.zero,
              ),
          
              CheckboxListTile(
                title: const Text('Use new UI (useNewUI)'),
                value: _useNewUI,
                onChanged: (v) => setState(() => _useNewUI = v ?? false),
                controlAffinity: ListTileControlAffinity.leading,
                contentPadding: EdgeInsets.zero,
              ),
          
              const SizedBox(height: 12),
              ElevatedButton(onPressed: _setConfig, child: const Text('Set Config')),
              const SizedBox(height: 12),
          
              const Divider(),
              const SizedBox(height: 8),
          
              const Text('Demo Actions', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 8),
              ElevatedButton(onPressed: _setLocationProvider, child: const Text('Set Location Provider')),
              const SizedBox(height: 8),
              ElevatedButton(onPressed: _setWindowHandler, child: const Text('Set Window Event Handler')),
              const SizedBox(height: 8),
              ElevatedButton(onPressed: _startChat, child: const Text('Start Chatbot (push screen)')),
              const SizedBox(height: 8),
              ElevatedButton(onPressed: _sendEventToBot, child: const Text('Send CHAT360_WINDOW_EVENT to bot')),
              const SizedBox(height: 8),
              ElevatedButton(onPressed: _sendJsBridgeEvent, child: const Text('Send via Chat360JSBridge')),
          
              const SizedBox(height: 24),
              const Divider(),
              const SizedBox(height: 8),
          
              const Text('Manual test area', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 8),
              const Text('Open the chatbot and interact with the web UI. Use input fields to test keyboard behavior.'),
              const SizedBox(height: 12),
              TextField(decoration: const InputDecoration(labelText: 'Focus to open keyboard')),
              const SizedBox(height: 12),
              const Text('Logs: enable device logs to see debug traces from the SDK'),
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
150
points
19
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that wraps the Chat360 web client in a WebView.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, url_launcher, webview_flutter

More

Packages that depend on chat360_flutter_sdk