inbrain_surveys 0.0.9
inbrain_surveys: ^0.0.9 copied to clipboard
Survey library to monetize your mobile app, provided by inBrain.ai.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:inbrain_surveys/inbrain_surveys.dart';
import 'package:inbrain_surveys/types/types.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _rewardPoints = 0.0;
final _inbrainSurveysPlugin = InbrainSurveys();
List _nativeSurveys = [];
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
_inbrainSurveysPlugin.setInbrain(
apiClientId: '35c6e720-4f76-4d25-9e18-e718678e27ae',
clientSecret:
'UnrB/pju7cH1dvTKL72blwZuhVrdsyZaS16qHOrjGdDVf2NWm+V7vmR8c3gMGnApmG/OUNkc1LLzgzO7K7T9DA==');
// _inbrainSurveysPlugin.setUserID('[email protected]');
_inbrainSurveysPlugin.setSessionID('newSessionId');
_inbrainSurveysPlugin.setNavigationBarConfig(const InBrainNavbarConfig(
backgroundColor: Colors.red,
buttonsColor: Colors.green,
titleColor: Colors.blue,
title: 'inBrain.ai Surveys',
hasShadow: false,
));
_inbrainSurveysPlugin.setStatusBarConfig(const InBrainStatusBarConfig(
lightStatusBar: true, statusBarColor: Colors.orange));
//init callback function
_inbrainSurveysPlugin.setOnSurveysClosedListener(
(bool closedByWebView, List<InBrainNativeReward>? rewards) {
getRewards();
});
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> showSurveys() async {
_inbrainSurveysPlugin.areSurveysAvailable().then((available) {
_inbrainSurveysPlugin.showSurveys();
}).catchError((error) {
_showMyDialog(error);
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> getRewards() async {
_inbrainSurveysPlugin.getRewards().then((rewards) {
double sum = 0.0;
for (int i = 0; i < rewards.length; i++) {
sum = sum + rewards[i].amount;
}
setState(() {
_rewardPoints = sum;
});
_inbrainSurveysPlugin.confirmRewards(rewards);
}).catchError((error) {
_showMyDialog(error);
});
}
Future<void> getNativeSurveys() async {
InBrainFilter filter = const InBrainFilter(
placementId: null,
categoryIds: [
InBrainCategory.automotive,
InBrainCategory.childrenAndParenting
],
excludedCategoryIds: null);
_inbrainSurveysPlugin
.getNativeSurveys(filter)
.then((List<InBrainSurvey> surveys) {
setState(() {
_nativeSurveys = surveys;
});
}).catchError((error) {
_showMyDialog(error);
});
if (!mounted) return;
}
Future<void> _showMyDialog(error) async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Ooppss'),
content: Text(error.details),
actions: <Widget>[
TextButton(
child: const Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
// child: Text('Running on: 1'),
child: Row(
children: [
Column(
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
shadowColor: Colors.green,
),
onPressed: () {
showSurveys();
},
child: const Text('Show Surveys'),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
shadowColor: Colors.green,
),
onPressed: () {
getNativeSurveys();
},
child: const Text('Get Native Surveys'),
),
Text('Reward points: $_rewardPoints '),
],
),
Expanded(
child: ListView.builder(
itemCount: _nativeSurveys.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Column(
children: [
ElevatedButton(
child: Text('open survey ${_nativeSurveys[index].id}'),
onPressed: () {
String searchId = _nativeSurveys[index].searchId;
String id = _nativeSurveys[index].id;
_inbrainSurveysPlugin.showNativeSurvey(searchId, id);
})
],
));
},
)),
],
)),
),
);
}
}