onetrust_publishers_native_cmp 6.20.0 copy "onetrust_publishers_native_cmp: ^6.20.0" to clipboard
onetrust_publishers_native_cmp: ^6.20.0 copied to clipboard

outdated

Add the OneTrust native CMP to your Flutter project.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io' show Platform;

import 'package:flutter/services.dart';
import 'package:onetrust_publishers_native_cmp/onetrust_publishers_native_cmp.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _cmpDownloadStatus = 'Waiting';
  int _c0003Status = -1;
  int _c0004Status = -1;
  String _dataSubjectId = "Unknown";
  @override
  void initState() {
    super.initState();
    initOneTrust();
    startListening();
  }

  Future<void> initOneTrust() async {
    bool? status;
    String appId;
    bool? shouldShowBanner;
    String? id;

    if (Platform.isAndroid) {
      appId = "162cfe19-aff6-4d60-b10e-6e7b6fdcfb8b-test";
    } else if (Platform.isIOS) {
      appId = "162cfe19-aff6-4d60-b10e-6e7b6fdcfb8b-test";
    } else {
      Exception("Platform not found!");
      return;
    }

    try {
      status = await OTPublishersNativeSDK.startSDK(
          "cdn.cookielaw.org", appId, "en");
      shouldShowBanner = await OTPublishersNativeSDK.shouldShowBanner();
      id = await OTPublishersNativeSDK.getCachedIdentifier();
    } on PlatformException {
      print("Error communicating with platform code");
    }

    if (status! && shouldShowBanner!) {
      OTPublishersNativeSDK.showBannerUI();
    }

    if (!mounted) return;

    setState(() {
      _cmpDownloadStatus = status! ? 'Success!' : 'Error';
      _dataSubjectId = id!;
    });
  }

  void startListening() {
    var consentListener =
        OTPublishersNativeSDK.listenForConsentChanges(["C0003", "C0004"])
            .listen((event) {
      setCategoryState(event['categoryId'], event['consentStatus']);
      print("New status for " +
          event['categoryId'] +
          " is " +
          event['consentStatus'].toString());
    });

    var interactionListener =
        OTPublishersNativeSDK.listenForUIInteractions().listen((event) {
      print(event);
    });

    //consentListener.cancel(); //Cancel event stream before opening a new one
  }

  void getConsentForWebView() async {
    var js = await OTPublishersNativeSDK.getOTConsentJSForWebView();
    print("JavaScript to inject to WebView =  + ${js!}");
  }

  void setCategoryState(String category, int status) {
    setState(() {
      switch (category) {
        case "C0003":
          _c0003Status = status;
          break;
        case "C0004":
          _c0004Status = status;
          break;
        default:
          break;
      }
    });
  }

  Future<void> getConsentForC2() async {
    int? status;
    try {
      status = await OTPublishersNativeSDK.getConsentStatusForCategory("C0004");
    } on PlatformException {
      print("Error communicating with platform-side code.");
    }
    print("Queried Status for C0004 is = " + status.toString());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('OneTrust Plugin Demo App'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('OneTrust Download Status: $_cmpDownloadStatus\n'),
              ElevatedButton(
                  onPressed: () {
                    OTPublishersNativeSDK.showBannerUI();
                  },
                  child: Text("Load Banner")),
              ElevatedButton(
                  onPressed: () {
                    OTPublishersNativeSDK.showPreferenceCenterUI();
                  },
                  child: Text("Load Preference Center")),
              ElevatedButton(
                  onPressed: () {
                    getConsentForWebView();
                  },
                  child: Text("Get JS Consent for WebView")),
              Text('Category C0003 Status: $_c0003Status\n'),
              Text('Category C0004 Status: $_c0004Status\n'),
              Text('Data Subject Identifier is'),
              Text(_dataSubjectId,
                  style: TextStyle(fontWeight: FontWeight.bold))
            ],
          ),
        ),
      ),
    );
  }
}
7
likes
0
pub points
93%
popularity

Publisher

unverified uploader

Add the OneTrust native CMP to your Flutter project.

Homepage

License

unknown (license)

Dependencies

flutter

More

Packages that depend on onetrust_publishers_native_cmp