idxdmp 2.4.9 copy "idxdmp: ^2.4.9" to clipboard
idxdmp: ^2.4.9 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: "91f8eae5-0554-4499-8221-28880465ceae");
  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"),
            ),
          ),
        ]
      ),
    );
  }
}
1
likes
0
points
31
downloads

Publisher

unverified uploader

Weekly Downloads

IDX DMP flutter SDK.

Homepage

License

unknown (license)

Dependencies

flutter, plugin_platform_interface, webview_flutter

More

Packages that depend on idxdmp

Packages that implement idxdmp