flutter_freshchat 1.1.3 flutter_freshchat: ^1.1.3 copied to clipboard
A Flutter plugin for integrating Freshchat in your mobile app.
import 'package:flutter/material.dart';
import 'dart:async';
import 'update_userinfo_screen.dart';
import 'package:localstorage/localstorage.dart';
import 'package:flutter_freshchat/flutter_freshchat.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final scaffoldKey = GlobalKey<ScaffoldState>();
List<Item> items = [
Item(
text: 'Update User Info',
onTap: (context) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => UpdateUserInfoScreen()));
}),
Item(
text: 'Identify User',
onTap: (context) async {
LocalStorage storage = LocalStorage('example_storage');
//Navigate to update email ID and name screen
String uid = await storage.getItem('uid');
String restoreId = await storage.getItem('restoreId');
if (uid == null) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("Please update the user info")));
} else if (restoreId == null) {
String newRestoreId =
await FlutterFreshchat.identifyUser(externalID: uid);
await storage.setItem('restoreId', newRestoreId);
} else {
await FlutterFreshchat.identifyUser(
externalID: uid, restoreID: restoreId);
}
}),
Item(
text: 'Show Conversation',
onTap: (context) async {
await FlutterFreshchat.showConversations();
}),
Item(
text: 'Show FAQs',
onTap: (context) async {
await FlutterFreshchat.showFAQs();
}),
Item(
text: 'Get Unread Message Count',
onTap: (context) async {
dynamic val = await FlutterFreshchat.getUnreadMsgCount();
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text("Message count $val")));
}),
Item(
text: 'Setup Notifications',
onTap: () {
//Navigate to update email ID and name screen
}),
Item(
text: 'Reset User',
onTap: (context) async {
await FlutterFreshchat.resetUser();
}),
];
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
await FlutterFreshchat.init(
appID: "YOUR_API_KEY_HERE", appKey: "YOUR_APP_KEY_HERE");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: const Text('Flutter Freshchat Example App'),
),
body: ListView.builder(
padding: const EdgeInsets.all(10.0),
itemBuilder: (context, i) {
return ListItem(
item: items[i].text,
onTap: () => items[i].onTap(context),
);
},
itemCount: items.length,
),
),
);
}
}
class ListItem extends StatelessWidget {
String item;
Function onTap;
ListItem({@required String item, @required Function onTap}) {
this.item = item;
this.onTap = onTap;
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Card(
child: Container(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
new CircleAvatar(
child: Text('A'),
),
Padding(padding: EdgeInsets.only(right: 10.0)),
Text(item)
],
),
),
),
);
}
}
class Item {
String text;
Function onTap;
Item({@required String text, @required Function onTap}) {
this.text = text;
this.onTap = onTap;
}
}