appgain_io 0.0.1
appgain_io: ^0.0.1 copied to clipboard
Appgain SDK Integration.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:one_context/one_context.dart';
import 'package:logger/logger.dart';
import 'appgain.dart';
void main() async {
runApp(MaterialApp(
theme: ThemeData(
primarySwatch: Colors.teal,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
debugShowCheckedModeBanner: false,
navigatorObservers: [OneContext().heroController],
navigatorKey: OneContext().navigator.key,
builder: OneContext().builder,
home: const HomeScreen()));
}
String? appId ;
String? apiKey ;
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
initPlatformState();
// Future(initUser);
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion =
await Appgain.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
initUser() {
TextFieldAlertDialog().initSDKDialog(context);
}
Future fireAutomator() async {
TextFieldAlertDialog().fireTriggerPointDialog(context);
}
Future fireAutomatorWithPersonalization() async {
TextFieldAlertDialog().fireAutomatorWithPersonalizationDialog(context);
}
Future cancelFireAutomator() async {
TextFieldAlertDialog().cancelFireTriggerPointDialog(context);
}
Future addPurchase() async {
TextFieldAlertDialog().logPurchaseDialog(context);
}
Future updateUserId() async {
TextFieldAlertDialog().updateUserIdDialog(context);
}
Future updateUser() async {
TextFieldAlertDialog().updateUserDialog(context);
}
Future addEmailChannel() async {
TextFieldAlertDialog().emailNotificationChannelDialog(context);
}
Future addSMSChannel() async {
TextFieldAlertDialog().smsNotificationChannelDialog(context);
}
Future logEvent() async {
TextFieldAlertDialog().logEventDialog(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Appgain SDK Plugin'),
),
body: SingleChildScrollView(
child: Center(
child: Column(children: [
const SizedBox(height: 10),
InvokeButton(
buttonName: 'INITIALIZE',
function: () => initUser()),
InvokeButton(
buttonName: 'GET USER ID', function: () => Appgain().getUserId()),
InvokeButton(
buttonName: 'MATCH LINK', function: () => Appgain().matchLink()),
InvokeButton(
buttonName: 'FIRE AUTOMATOR',
function: () => fireAutomator()),
InvokeButton(
buttonName: 'FIRE AUTOMATOR WITH PERSONALIZATION',
function: () => fireAutomatorWithPersonalization()),
InvokeButton(
buttonName: 'CANCEL AUTOMATOR',
function: () => cancelFireAutomator()),
InvokeButton(
buttonName: 'ADD PURCHASE',
function: () => addPurchase()),
InvokeButton(
buttonName: "UPDATE USER ID", function: () => updateUserId()),
InvokeButton(
buttonName: 'UPDATE USER DATA', function: () => updateUser()),
InvokeButton(
buttonName: 'ADD EMAIL CHANNEL',
function: () => addEmailChannel()),
InvokeButton(
buttonName: 'ADD SMS CHANNEL', function: () => addSMSChannel()),
InvokeButton(buttonName: 'LOG EVENT', function: () => logEvent()),
]),
)),
);
}
}
class InvokeButton extends StatelessWidget {
final String? buttonName;
final Function function;
const InvokeButton(
{Key? key, required this.buttonName, required this.function})
: super(key: key);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
width: double.infinity - 20,
child: OutlinedButton(
child: Text(buttonName!.toUpperCase()),
onPressed: () {
function();
},
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: Colors.teal,
),
),
);
}
}
BuildContext? rootContext;
class TextFieldAlertDialog {
final _textFieldController = TextEditingController();
final _textFieldController2 = TextEditingController();
final _textFieldController3 = TextEditingController();
final _textFieldController4 = TextEditingController();
var logger = Logger(printer: PrettyPrinter());
initSDKDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Init Sdk'),
content: //<Widget>[
SizedBox(
height: 100,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
decoration: inputDecoration("APP ID"),
),
TextField(
controller: _textFieldController2,
textInputAction: TextInputAction.go,
decoration: inputDecoration("API KEY"),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
var prefs = await SharedPreferences.getInstance();
Appgain().initAppgainSDK(appid: _textFieldController.text,apiKey: _textFieldController2.text);
prefs.setString('appId', _textFieldController.text);
prefs.setString('appKey', _textFieldController2.text);
Navigator.of(context).pop();
},
)
],
);
});
}
fireTriggerPointDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('FIRE AUTOMATER FOR'),
content: //<Widget>[
SizedBox(
height: 50,
child: TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
// keyboardType: TextInputType.text(),
decoration: inputDecoration("TRIGGER POINT NAME"),
)),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain()
.fireAutomator(triggerPoint: _textFieldController.text);
},
)
],
);
});
}
fireAutomatorWithPersonalizationDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('TRIGGER WITH PERSONALIZATION'),
content: //<Widget>[
SizedBox(
height: 145,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
decoration: inputDecoration('TRIGGER POINT FOR'),
),
TextField(
controller: _textFieldController2,
textInputAction: TextInputAction.go,
// keyboardType: TextInputType.text(),
decoration: inputDecoration('KEY'),
),
TextField(
controller: _textFieldController3,
textInputAction: TextInputAction.done,
decoration: inputDecoration('VALUE'),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
var personalizationData = {
_textFieldController2.text: _textFieldController3.text,
};
await Appgain().fireAutomatorWithPersonalization(
triggerPoint: _textFieldController.text,
personalizationMap: personalizationData);
},
)
],
);
});
}
cancelFireTriggerPointDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('CANCEL FIRE AUTOMATER FOR'),
content: //<Widget>[
SizedBox(
height: 50,
child: TextField(
controller: _textFieldController,
textInputAction: TextInputAction.done,
// keyboardType: TextInputType.text(),
decoration: inputDecoration("TRIGGER POINT NAME"),
)),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().cancelfireAutomator(
triggerPoint: _textFieldController.text);
},
)
],
);
});
}
updateUserIdDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('UPDATE USER ID'),
content: //<Widget>[
SizedBox(
height: 50,
child: TextField(
controller: _textFieldController,
textInputAction: TextInputAction.done,
// keyboardType: TextInputType.text(),
decoration: inputDecoration("NEW USER ID"),
)),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().setuserId(userId: _textFieldController.text);
},
)
],
);
});
}
emailNotificationChannelDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('CREATE NOTIFICATION CHANNEL'),
content: //<Widget>[
SizedBox(
height: 50,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
decoration: inputDecoration('ENTER EMAIL'),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().addNotificationChannel(
notificationType: 'email',
item: _textFieldController.value.text,
);
},
)
],
);
});
}
smsNotificationChannelDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('CREATE NOTIFICATION CHANNEL'),
content: //<Widget>[
SizedBox(
height: 50,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.done,
decoration: inputDecoration("ENTER MOBILE NUMBER"),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().addNotificationChannel(
notificationType: 'SMS',
item: _textFieldController.value.text);
},
)
],
);
});
}
logPurchaseDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Add Purchase'),
content: //<Widget>[
SizedBox(
height: 150,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
decoration: inputDecoration('product name'),
),
TextField(
controller: _textFieldController2,
textInputAction: TextInputAction.go,
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
decoration: inputDecoration('amount'),
),
TextField(
controller: _textFieldController3,
textInputAction: TextInputAction.done,
decoration: inputDecoration('currency'),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().addPurchase(
productName: _textFieldController.text,
amount: _textFieldController2.text == ''
? 0
: double.parse(_textFieldController2.text),
currency: _textFieldController3.text);
},
)
],
);
});
}
updateUserDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('update user'),
content: //<Widget>[
SizedBox(
height: 200,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
// keyboardType: TextInputType.text(),
decoration: inputDecoration('USER EMAIL'),
),
TextField(
controller: _textFieldController2,
textInputAction: TextInputAction.next,
// keyboardType: TextInputType.text(),
decoration: inputDecoration('phone'),
),
TextField(
controller: _textFieldController3,
textInputAction: TextInputAction.next,
// keyboardType: TextInputType.text(),
decoration: inputDecoration('field one'),
),
TextField(
controller: _textFieldController4,
textInputAction: TextInputAction.done,
// keyboardType: TextInputType.text(),
decoration: inputDecoration('field two'),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
var updatedData = {
'userEmail': _textFieldController.text,
'phone': _textFieldController2.text,
'updatedField1': _textFieldController3.text,
"updatedField2": _textFieldController4.text
};
await Appgain().updateUser(data: updatedData);
},
)
],
);
});
}
enableNotificationChannelDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('enable notification channel'.toUpperCase()),
content: //<Widget>[
SizedBox(
height: 50,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
decoration: inputDecoration("Notification Type"),
),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().enableReciveNotification(
type: _textFieldController.text);
},
)
],
);
});
}
logEventDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Log event'),
content: //<Widget>[
SizedBox(
height: 200,
child: Column(
children: <Widget>[
TextField(
controller: _textFieldController,
textInputAction: TextInputAction.go,
decoration: inputDecoration('Type'),
),
TextField(
controller: _textFieldController2,
textInputAction: TextInputAction.go,
decoration: inputDecoration('action'),
),
TextField(
controller: _textFieldController3,
textInputAction: TextInputAction.go,
decoration: inputDecoration('key'),
),
TextField(
controller: _textFieldController4,
textInputAction: TextInputAction.done,
decoration: inputDecoration('value'),
),
],
)),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () async {
Navigator.of(context).pop();
await Appgain().logEvent(
type: _textFieldController.value.text,
action: _textFieldController2.value.text,
extras: {
_textFieldController3.value.text:
_textFieldController4.value.text
},
);
},
)
],
);
});
}
showAlertDialog(String message) {
OneContext().showDialog(
useRootNavigator: false,
builder: (_) => AlertDialog(
title: const Text(''),
content: Text(message),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () {
Navigator.pop(_);
},
)
],
));
}
InputDecoration inputDecoration(String? message) =>
InputDecoration(hintText: message!);
}