smooch_plugin 2.0.51 copy "smooch_plugin: ^2.0.51" to clipboard
smooch_plugin: ^2.0.51 copied to clipboard

Smooch resources on Flutter

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:smooch_plugin/smooch_constants.dart';
import 'package:smooch_plugin/smooch_plugin.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const String START_SMOOCH_ACTION =
      "android.intent.action.START_SMOOCH";
  static const _channel = MethodChannel("app_state_method_channel");

  Future<String> _startAction;
  int _count = -1;

  void _updateUnreadCount(count) {
    setState(() {
      _count = count;
    });
  }

  void _handleMessageAction(uri) {
    print(uri);
  }

  void _logoutComplete(logoutResult) {
    print("onLogoutComplete");
  }

  void _onHelpClick() {
    print("onHelpClick");
  }

  Future<void> _onPushNotification(Map<String, dynamic> userInfo) async {
    print(userInfo);
  }

  @override
  void initState() {
    super.initState();

    if (mounted) initPlatformState();
  }

  Future<String> _getStartAction() async {
    try {
      return _channel.invokeMethod("getIntentAction");
    } on PlatformException catch (e) {
      print("Failed to Invoke: '${e.message}'.");
      return "unknown";
    }
  }

  Future<void> initPlatformState() async {
    SmoochPlugin.initializeChannel(
      onUpdateUnreadCount: _updateUnreadCount,
      onHandleMessageAction: _handleMessageAction,
      onLogout: _logoutComplete,
      onHelpClick: _onHelpClick,
      onPushNotification: _onPushNotification,
    );

    await smoochInit();
  }

  void smoochInit() async {
    List<String> menuItemsAllowed = [
      SKTMenuItemCamera,
      SKTMenuItemDocument,
      SKTMenuItemGallery,
      SKTMenuItemLocation,
    ];

    try {
      final bool result = await SmoochPlugin.init(
        appId: "000000",
        integrationId: "000000000",
        userId: "000000000",
        smoochToken: "0000000000",
        chatColor: "#0000FF",
        menuItemsAllowed: menuItemsAllowed,
      );

      print('result Initialize $result');

      if (result == false) {
        print("Failed to initialize smooch");
        return;
      }

      _startAction?.then((action) {
        print("action received: $action");
        if (action == START_SMOOCH_ACTION) {
          SmoochPlugin.show(lang: "en");
        }
      });
    } on PlatformException catch (e) {
      print('Exception: $e');
    }
  }

  Future<void> showSmooch() async {
    try {
      var accessory = <String, String>{
        'title': 'Help',
        'icon': 'no_icon',
        'flow_name': 'flow_name'
      };

      await SmoochPlugin.show(
          lang: "es", sendStart: true, accessory: accessory);
    } on PlatformException catch (e) {
      print('Exception: $e');
    }
  }

  logoutSmooch() async {
    bool result = await SmoochPlugin.logout();
    print('LOGOUT $result');
  }

  changeCompany() async {
    await logoutSmooch();
    await smoochInit();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Smooch plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              FlatButton(
                onPressed: showSmooch,
                child: Text("Abrir smooch"),
              ),
              Text('Unread messages count: $_count'),
              FlatButton(
                onPressed: logoutSmooch,
                child: Text("Logout"),
              ),
              FlatButton(
                onPressed: changeCompany,
                child: Text("Change Company"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
20
pub points
0%
popularity

Publisher

unverified uploader

Smooch resources on Flutter

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on smooch_plugin