Info
Content

[Flutter] 1. consentmanager SDK Integration

Installing

Run this command:

With Flutter:

flutter pub add cmp_sdk

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:  
	cmp_sdk: ^0.1.0

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import

Now in your Dart code, you can use:

import 'package:cmp_sdk/cmp_sdk.dart';

Using the library

To create an instance of CmpSdk with a basic configuration:

import 'package:cmp_sdk/cmp_sdk.dart';
import 'package:cmp_sdk/cmp_config.dart';

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

  // Initialize CMP SDK with basic configuration
  final cmpSdk = CmpSdk.createInstance(
    id: "Code-ID from consentmanager, e.g. bf816dab123...",
    domain: "Server-Domain from consentmanager, e.g. delivery.consentmanager.net",
    appName: "App name, e.g. MyFlutterApp",
    language: "Language, e.g. FR",
  );
}

 

Event Callbacks

The SDK allows setting up callbacks for various consent layer events like opening, closing, errors, and button clicks. This is useful for logging purposes or executing additional logic based on user interactions.

_cmpSdkPlugin.setCallbacks(
  onOpen: () => logCallback('Consent layer opened'),
  onClose: () => logCallback('Consent layer closed'),
  onError: (type, message) => logCallback('Error: $type - $message'),
  onButtonClicked: (buttonType) => logCallback('Button clicked: $buttonType'),
);

The SDK provides methods to accept or reject consent globally, reset consent data, and open the consent layer for user interaction.

  • Accept All: await _cmpSdkPlugin.acceptAll();
  • Reject All: await _cmpSdkPlugin.rejectAll();
  • Check: await _cmpSdkPlugin.check();
  • Reset Consent: await _cmpSdkPlugin.reset();
  • Open Consent Layer: await _cmpSdkPlugin.open();

Exporting and Importing CMP String

The CMP string representing the user's consent preferences. You can import it into the CMP SDK to apply those preferences. This can be useful for transferring consent preferences between different parts of your application or storing them externally.

Future<void> importUserConsent(String cmpString) async {
  try {
    final bool success = await CmpSdkPlatform.instance.importCmpString(cmpString);
    if (success) {
      // CMP string was successfully imported
      print("CMP String successfully imported.");
      // The user's consent preferences are now updated according to the imported string
    } else {
      // Handle the case where the import was unsuccessful
      print("Failed to import CMP String.");
    }
  } catch (error) {
    // Handle any errors that occur during import
    print("Error importing CMP String: $error");
  }
}

Future<void> exportUserConsent() async {
  try {
    final String? cmpString = await CmpSdkPlatform.instance.exportCmpString();
    if (cmpString != null) {
      // CMP string successfully retrieved
      print("CMP String: $cmpString");
      // You can now store this string or share it as needed
    } else {
      // Handle the case where CMP string is null
      print("No CMP String available.");
    }
  } catch (error) {
    // Handle any errors that occur during export
    print("Error exporting CMP String: $error");
  }
}

You can retrieve various consent-related information using the SDK's methods, such as checking if consent is required, fetching the current consent status, and obtaining consent for specific vendors or purposes.

Creating a custom layout

await _cmpSdkPlugin.configureConsentLayer(CmpUiConfig(screenConfig: ScreenConfig.fullScreen));
Available layouts
  • fullScreen
  • halfScreenBottom
  • halfScreenTop
  • centerScreen
  • smallCenterScreen
  • largeTopScreen
  • largeBottomScreen

Notes

API documentation: documentation

Library: pub.dev 

Back to top