hs_wrapper 0.0.6 copy "hs_wrapper: ^0.0.6" to clipboard
hs_wrapper: ^0.0.6 copied to clipboard

Flutter plugin to integrate Helpshift Support SDK into your flutter app.

example/lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hs_wrapper/hs_wrapper.dart';
import 'package:hs_wrapper_example/src/constants.dart';


void main() {
  WidgetsFlutterBinding.ensureInitialized();
  setupHelpShiftSdk();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(const MyApp());
  });
}

/// configure HelpShift SDK for Native Platforms
void setupHelpShiftSdk() async {
  await HsWrapper.setUpHelpShiftSDK(
    helpShiftApiKey: Constants.helpShiftApiKey(),
    helpShiftAppId: Constants.helpShiftAppId(),
    helpShiftDomain: Constants.helpShiftDomain,
  );
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final userNameCtrl = TextEditingController();
  final userIdCtrl = TextEditingController();
  final userEmailCtrl = TextEditingController();
  var isLoggedIn = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Hs Wrapper'),
          centerTitle: true,
          actions: isLoggedIn
              ? [
                  InkWell(
                    onTap: () async {
                      var result = await HsWrapper.logoutUser();
                      if (result == true) {
                        setState(() {
                          isLoggedIn = false;
                        });
                      }
                    },
                    child: const Icon(Icons.logout),
                  )
                ]
              : [],
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              isLoggedIn == false
                  ? loginWidget(context)
                  : const SizedBox.shrink(),
              const SizedBox(height: 20),
              Container(
                width: double.infinity,
                padding: const EdgeInsets.symmetric(horizontal: 20),
                child: ElevatedButton(
                  onPressed: () {
                    HsWrapper.showAllConversation(
                        configMap: getConfigmap());
                  },
                  child: const Text('Show All Conversation'),
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.showAllConversation(
                      configMap: setMetadata());
                },
                child: const Text('Show All Conversation'),
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.openFAQsScreen(configMap: setMetadata());
                },
                child: const Text('FAQs'),
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.openSingleFAQScreen(
                      sectionId: "1", configMap: getConfigmap());
                },
                child: const Text('Order FAQs'),
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.openSingleFAQScreen(
                      sectionId: "2", configMap: getConfigmap());
                },
                child: const Text('Returns and Exchanges FAQs'),
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.openSingleFAQScreen(
                      sectionId: "3", configMap: getConfigmap());
                },
                child: const Text('Shipping and Delivery FAQs'),
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.openSingleFAQScreen(
                      sectionId: "4", configMap: getConfigmap());
                },
                child: const Text('Payment FAQs'),
              ),
              ElevatedButton(
                onPressed: () {
                  HsWrapper.openSinglePublishedFAQDetail(
                      publishId: "8", configMap: getConfigmap());
                },
                child: const Text('What Payment Methods do you accept?'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  loginWidget(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.all(
          Radius.circular(10),
        ),
        border: Border.all(
          color: Colors.grey,
        ),
      ),
      child: Column(
        children: [
          const SizedBox(height: 30),
          nameLabelUiWidget("User Name", userNameCtrl, TextInputType.name),
          const SizedBox(height: 20),
          nameLabelUiWidget("User Id", userIdCtrl, TextInputType.text),
          const SizedBox(height: 20),
          nameLabelUiWidget(
              "User Email", userEmailCtrl, TextInputType.emailAddress),
          const SizedBox(height: 20),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            width: double.infinity,
            child: ElevatedButton(
                onPressed: () async {
                  var userName = userNameCtrl.text.trim();
                  var userId = userIdCtrl.text.trim();
                  var userEmail = userEmailCtrl.text.trim();
                  dynamic result;
                  if (validationUserDetail(context)) {
                    result = await HsWrapper.loginUser(
                      userId: userId ?? "",
                      email: userEmail ?? "",
                      userName: userName ?? "",
                    );
                  }

                  userEmailCtrl.clear();
                  userIdCtrl.clear();
                  userEmailCtrl.clear();

                  if (kDebugMode) {
                    print('result $result');
                  }
                  if (result == true) {
                    setState(() {
                      isLoggedIn = true;
                    });
                  }
                },
                child: const Text('Login')),
          )
        ],
      ),
    );
  }

  nameLabelUiWidget(label, ctrl, inputType) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(label),
          const SizedBox(height: 10),
          SizedBox(
            height: 50,
            width: double.infinity,
            child: TextField(
              controller: ctrl,
              keyboardType: inputType,
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  filled: true,
                  hintStyle: TextStyle(color: Colors.grey[800]),
                  hintText: "Type in your $label",
                  fillColor: Colors.white70),
            ),
          ),
        ],
      ),
    );
  }

  /// setup your config map according to your need
  getConfigmap() {
    var config = {};
    // set tags for tracking
    config["tags"] = ["foo", "bar"];

    // set custom issue fields
    var cifMap = {};
    var isPro = {};
    isPro["type"] = "boolean";
    isPro["value"] = "true";
    cifMap["is_pro"] = isPro;

    config["customIssueFields"] = cifMap;

    return config;
  }

  /// setup your customIssueFiled Map
  getCustomIssueFieldMap() {
    var joiningDate = {};
    joiningDate["type"] = "date";
    joiningDate["value"] = 1505927361535;

    var stockLevel = {};
    stockLevel["type"] = "number";
    stockLevel["value "] = "1505";

    var employeeName = {};
    employeeName["type"] = "singleline";
    employeeName["value"] = "Bugs helpshift";

    var isPro = {};
    isPro["type"] = "boolean";
    isPro["value"] = "true";

    var cifMap = {};
    cifMap["joining_date"] = joiningDate;
    cifMap["stock_level"] = stockLevel;
    cifMap["employee_name"] = employeeName;
    cifMap["is_pro"] = isPro;

    var config = {};
    config["customIssueFields"] = cifMap;

    return config;
  }

  /// custom metadata
  setMetadata() {
    var config = {};
    var metaMap = {"usertype": "paid"};
    config["customMetadata"] = metaMap;
    return config;
  }

  validationUserDetail(BuildContext context) {
    var userId = userIdCtrl.text.trim();
    var userEmail = userEmailCtrl.text.trim();
    if (userId.isEmpty && userEmail.isEmpty) {
      Fluttertoast.showToast(msg: 'Please enter userId or email to login.');
      return false;
    } else {
      return true;
    }
  }
}
1
likes
150
points
105
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter plugin to integrate Helpshift Support SDK into your flutter app.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on hs_wrapper