consent_mode_flutter 1.0.2 copy "consent_mode_flutter: ^1.0.2" to clipboard
consent_mode_flutter: ^1.0.2 copied to clipboard

A comprehensive Flutter library for managing user consent according to GDPR, CCPA, and other privacy regulations.

example/lib/main.dart

import 'package:consent_mode_flutter/consent_mode_flutter.dart';
import 'package:flutter/material.dart';

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

  // Initialize consent mode
  await ConsentModeFlutter.initialize();

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Consent Mode Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: ConsentModeFlutterWrapper(
        bannerConfig: ConsentModeFlutterConfigHelper.bannerConfig(context),
        bannerStyle: ConsentModeFlutterUiStyleHelper.bannerStyle(context),
        child: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Map<ConsentModeFlutterType, bool> _currentPreferences = {};

  @override
  void initState() {
    super.initState();
    _loadPreferences();

    // Listen to consent changes
    ConsentModeFlutter.addListener(_onConsentChanged);
  }

  @override
  void dispose() {
    ConsentModeFlutter.removeListener(_onConsentChanged);
    super.dispose();
  }

  void _loadPreferences() {
    setState(() {
      _currentPreferences = ConsentModeFlutter.preferences;
    });
  }

  void _onConsentChanged(Map<ConsentModeFlutterType, bool> preferences) {
    setState(() {
      _currentPreferences = preferences;
    });
    // Here you can trigger essential, analytics, marketing tools, etc. based on consent
    if (ConsentModeFlutter.isConsentGranted(ConsentModeFlutterType.essential)) {
      debugPrint('Essential consent granted - initialize essential');
    }

    if (ConsentModeFlutter.isConsentGranted(ConsentModeFlutterType.analytics)) {
      debugPrint('Analytics consent granted - initialize analytics');
    }

    if (ConsentModeFlutter.isConsentGranted(ConsentModeFlutterType.marketing)) {
      debugPrint('Marketing consent granted - initialize marketing tools');
    }

    if (ConsentModeFlutter.isConsentGranted(
      ConsentModeFlutterType.functional,
    )) {
      debugPrint('Functional consent granted - initialize functional');
    }
  }

  void _showConsentManager() {
    ConsentModeFlutterUtils.showCustomBottomSheet(
      context,
      ConsentModeFlutterBanner(
        config: ConsentModeFlutterConfigHelper.bannerConfig(context),
        style: ConsentModeFlutterUiStyleHelper.bannerStyle(context),
        onConsentChanged: (Map<ConsentModeFlutterType, bool> preferences) {},
        onDismissed: () {},
      ),
    );
  }

  Future<void> _resetConsent() async {
    await ConsentModeFlutter.reset();
    setState(() {
      _currentPreferences = ConsentModeFlutter.preferences;
    });

    // Show a snackbar or restart app to show banner again
    if (mounted) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text(
            'Consent preferences reset. Restart app to see banner again.',
          ),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: const Text('Consent Mode Example'),
          actions: [
            IconButton(
              onPressed: _showConsentManager,
              icon: const Icon(Icons.settings),
              tooltip: 'Manage Consent',
            ),
          ],
        ),
        body: Container(
          padding: EdgeInsets.symmetric(horizontal: 8),
          height: height,
          width: width,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      const Text(
                        'Current Consent Status:',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 16),
                      ...ConsentModeFlutterType.values.map(
                        (type) => Card(
                          child: ListTile(
                            title: Text(type.displayName),
                            subtitle: Text(type.description),
                            trailing: Icon(
                              _currentPreferences[type] == true
                                  ? Icons.check_circle
                                  : Icons.cancel,
                              color:
                                  _currentPreferences[type] == true
                                      ? Colors.green
                                      : Colors.red,
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(height: 24),
                      const Text(
                        'Conditional Content Examples:',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 16),
                      // Analytics content - only shown if analytics consent is granted
                      _consentGate(
                        type: ConsentModeFlutterType.analytics,
                        consentText:
                            'Analytics content hidden - consent not granted',
                        title: 'Analytics Content',
                        content:
                            'This content is only visible when analytics consent is granted.',
                      ),

                      const SizedBox(height: 8),
                      // Marketing content
                      _consentGate(
                        type: ConsentModeFlutterType.marketing,
                        consentText:
                            'Marketing content hidden - consent not granted',
                        title: 'Marketing Content',
                        content:
                            'This promotional content requires marketing consent.',
                      ),
                      const SizedBox(height: 8),
                      _consentGate(
                        type: ConsentModeFlutterType.functional,
                        consentText:
                            'Functional content hidden - consent not granted',
                        title: 'Functional Content',
                        content: 'Personalized content',
                      ),
                    ],
                  ),
                ),
              ),
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: _showConsentManager,
                      child: const Text('Manage Consent'),
                    ),
                  ),
                  const SizedBox(width: 16),
                  Expanded(
                    child: OutlinedButton(
                      onPressed: _resetConsent,
                      child: const Text('Reset Consent'),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _consentGate({
    required ConsentModeFlutterType type,
    required String consentText,
    required String title,
    required String content,
  }) {
    return ConsentModeFlutterGate(
      requiredConsent: type,
      fallback: Card(
        color: Colors.grey,
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.all(16.0),
          child: Text(consentText),
        ),
      ),
      child: Card(
        color: Colors.orange,
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                ),
              ),
              Text(content, style: TextStyle(color: Colors.white)),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
150
points
56
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive Flutter library for managing user consent according to GDPR, CCPA, and other privacy regulations.

Repository (GitHub)
View/report issues

Topics

#gdpr #ccpa #consent #privacy #flutter-plugin

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface, shared_preferences, url_launcher

More

Packages that depend on consent_mode_flutter

Packages that implement consent_mode_flutter