Flutter SDK

Cross-Platform SDK fuer Flutter 3.16+ mit iOS, Android und Web Support.

Systemvoraussetzungen

Dart Version3.0+
Flutter Version3.16+
PlattformeniOS, Android, Web

Installation

pubspec.yaml
dependencies:
  consent_sdk: ^1.0.0
flutter pub get

Grundlegende Einrichtung

main.dart
import 'package:flutter/material.dart';
import 'package:consent_sdk/consent_sdk.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Consent SDK initialisieren
  await ConsentManager.instance.initialize(
    config: ConsentConfig(
      apiEndpoint: 'https://api.example.com/consent',
      siteId: 'my-flutter-app',
    ),
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const ConsentWrapper(
        child: HomeScreen(),
      ),
    );
  }
}

Widget Integration

home_screen.dart
import 'package:flutter/material.dart';
import 'package:consent_sdk/consent_sdk.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          // StreamBuilder fuer reaktive Updates
          StreamBuilder<ConsentState?>(
            stream: ConsentManager.instance.consentStream,
            builder: (context, snapshot) {
              final consent = snapshot.data;

              if (consent?.hasConsent(ConsentCategory.analytics) ?? false) {
                return const AnalyticsWidget();
              }

              return const SizedBox.shrink();
            },
          ),

          // ConsentGate Widget
          ConsentGate(
            category: ConsentCategory.marketing,
            fallback: const Center(
              child: Text('Marketing-Zustimmung erforderlich'),
            ),
            child: const MarketingWidget(),
          ),

          // Buttons
          ElevatedButton(
            onPressed: () => ConsentManager.instance.acceptAll(),
            child: const Text('Alle akzeptieren'),
          ),

          ElevatedButton(
            onPressed: () => ConsentManager.instance.rejectAll(),
            child: const Text('Alle ablehnen'),
          ),

          TextButton(
            onPressed: () => ConsentManager.instance.showSettings(context),
            child: const Text('Einstellungen'),
          ),
        ],
      ),
    );
  }
}

Custom Cookie Banner

cookie_banner.dart
import 'package:flutter/material.dart';
import 'package:consent_sdk/consent_sdk.dart';

class CustomCookieBanner extends StatelessWidget {
  const CustomCookieBanner({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<bool>(
      stream: ConsentManager.instance.isBannerVisibleStream,
      builder: (context, snapshot) {
        if (!(snapshot.data ?? false)) {
          return const SizedBox.shrink();
        }

        return Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.1),
                blurRadius: 10,
              ),
            ],
          ),
          child: SafeArea(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text(
                  'Wir verwenden Cookies um Ihr Erlebnis zu verbessern.',
                  style: TextStyle(fontSize: 14),
                ),
                const SizedBox(height: 16),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    TextButton(
                      onPressed: () => ConsentManager.instance.rejectAll(),
                      child: const Text('Ablehnen'),
                    ),
                    TextButton(
                      onPressed: () => ConsentManager.instance.showSettings(context),
                      child: const Text('Einstellungen'),
                    ),
                    ElevatedButton(
                      onPressed: () => ConsentManager.instance.acceptAll(),
                      child: const Text('Alle akzeptieren'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

API Referenz

Methode/PropertyBeschreibung
initialize()SDK initialisieren (Future)
hasConsent()Consent pruefen
consentStreamStream fuer Consent-Updates
isBannerVisibleStreamStream fuer Banner-Sichtbarkeit
acceptAll()Alle akzeptieren (Future)
rejectAll()Alle ablehnen (Future)
setConsent()Kategorien setzen (Future)
showSettings()Einstellungs-Dialog oeffnen