idxdmp 2.4.7
idxdmp: ^2.4.7 copied to clipboard
IDX DMP flutter SDK.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter/services.dart';
import 'package:idxdmp/idxdmp.dart';
void main() {
runApp(const MaterialApp(
title: "Example App",
home: MyApp()
));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _dmpIsInit = false;
Map<String, String> _adCustomParameters = {};
final _idxdmpPlugin = Idxdmp();
final providerIdController = TextEditingController(text: "a5beb245-2949-4a76-95f5-bddfc2ec171c");
final urlController = TextEditingController(text: "https://www.ynet.co.il/food/article/1234");
final titleController = TextEditingController(text: "IDX News Site - IDX");
final domainController = TextEditingController(text: "https://www.ynet.co.il");
final authorController = TextEditingController(text: "IDX");
final categoryController = TextEditingController(text: "finance");
final descriptionController = TextEditingController(text: "Description text");
final tagsController = TextEditingController(text: "tag1,tag2,tag3");
@override
void initState() {
super.initState();
}
@override
void dispose() {
providerIdController.dispose();
urlController.dispose();
titleController.dispose();
domainController.dispose();
authorController.dispose();
categoryController.dispose();
descriptionController.dispose();
tagsController.dispose();
super.dispose();
}
Future<void> handleInitSdk() async {
bool dmpIsInit;
try {
dmpIsInit =
await _idxdmpPlugin.initSdk(providerIdController.text, 'My flutter app', 'v1.1.1') ?? false;
} on PlatformException {
dmpIsInit = false;
}
setState(() {
_dmpIsInit = dmpIsInit;
});
}
Future<void> handleSendEvent() async {
try {
if (!_dmpIsInit) {
return;
}
await _idxdmpPlugin.sendEvent(<String, String>{
"url": urlController.text,
"title": titleController.text,
"domain": domainController.text,
"author": authorController.text,
"category": categoryController.text,
"description": descriptionController.text,
"tags": tagsController.text,
});
var customParameters = await _idxdmpPlugin.getCustomAdTargeting();
setState(() { _adCustomParameters = customParameters; });
} on PlatformException {
setState(() { _adCustomParameters = <String, String>{}; });
}
}
Future<void> handleResetUserState() async {
try {
await _idxdmpPlugin.resetUserState();
} on PlatformException {
}
setState(() {
_dmpIsInit = false;
_adCustomParameters = <String, String>{};
});
}
void handleOpenWebviewPage() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const WebViewScreen()),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('DMP Flutter example'),
),
body: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.blueAccent,
),
onPressed: handleOpenWebviewPage,
child: const Text("Go to webview"),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: Column(
children: [
Center(
child: Text(_dmpIsInit ? "SDK IS READY!" : "SDK IS NOT INIT"),
),
Center(
child: Text('Ad custom parameters: $_adCustomParameters\n'),
),
],
)
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: providerIdController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Provider Id',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: urlController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'URL',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: titleController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Title',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: domainController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Domain',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: authorController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Author',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: categoryController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Category',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: descriptionController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Description',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: tagsController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Tags',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.blueAccent,
elevation: 0,
),
onPressed: handleInitSdk,
child: const Text("Init SDK"),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.blueAccent,
),
onPressed: handleSendEvent,
child: const Text("Send event"),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.redAccent,
),
onPressed: handleResetUserState,
child: const Text("Reset user state"),
),
),
],
)
),
);
}
}
class WebViewScreen extends StatefulWidget {
const WebViewScreen({super.key});
@override State<WebViewScreen> createState() => _WebViewState();
}
class _WebViewState extends State<WebViewScreen> {
final urlController = TextEditingController(text: "https://trmnlsnk.blogspot.com/2022/10/thrid-dmp.html?enableDmpPublisherDebug=true");
final webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000));
DMPWebViewConnector? webViewConnector;
@override
void initState() {
super.initState();
webViewConnector = DMPWebViewConnector(webViewController, 'My flutter app', '1.2.3');
webViewController.loadRequest(Uri.parse(urlController.text));
}
void handleOpenUrl() async {
await webViewController.loadRequest(Uri.parse(urlController.text));
}
Future<void> handleShowDebug() async {
Map<String, String>? adParameters = await webViewConnector?.getCustomAdTargeting();
// ignore: use_build_context_synchronously
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('AlertDialog Title'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
const Text("Web view connector debug!"),
Text("Ad parameters: $adParameters"),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Web view screen'),
),
body: Column(
children: <Widget>[
Expanded(child: WebViewWidget(controller: webViewController)),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: TextField(
controller: urlController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'URL',
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.blueAccent,
),
onPressed: handleOpenUrl,
child: const Text("Open url"),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.blueAccent,
),
onPressed: handleShowDebug,
child: const Text("Show debug"),
),
),
]
),
);
}
}