inbrain_surveys 1.0.0
inbrain_surveys: ^1.0.0 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(const 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: 'The client ID provided in inBrain.ai dashboard',
clientSecret: 'The client secret provided in inBrain.ai dashboard');
_inbrainSurveysPlugin
.setUserID('Uniq identifier of the user within your application');
_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));
_inbrainSurveysPlugin.setOnSurveysClosedListener(
(bool closedByWebView, List<InBrainNativeReward>? rewards) {
getRewards();
});
}
Future<void> showSurveys() async {
_inbrainSurveysPlugin.areSurveysAvailable().then((available) {
if (available) {
_inbrainSurveysPlugin.showSurveys();
} else {
_showMyDialog(message: 'No surveys available.');
}
}).catchError((error) {
_showMyDialog(message: error.details);
});
}
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(message: error.details);
});
}
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(message: error.details);
});
}
Future<void> _showMyDialog({required String message}) async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Ooppss'),
content: Text(message),
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: 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);
})
],
));
},
)),
],
)),
),
);
}
}