Skip to main content

go_trust_mobile_sdk 1.0.8 copy "go_trust_mobile_sdk: ^1.0.8" to clipboard
go_trust_mobile_sdk: ^1.0.8 copied to clipboard

A Flutter mobile SDK for GoTrust consent and preference management.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:go_trust_mobile_sdk/features/consent_form/consent_form_page.dart';
import 'package:go_trust_mobile_sdk/features/consent_form/services/go_trust_sdk_helper.dart';
import 'package:go_trust_mobile_sdk/features/consent_preferences/widgets/login_page.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'GoTrust SDK Sample',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
        useMaterial3: true,
      ),
      home: const GoTrustSamplePage(),
    );
  }
}

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

  @override
  State<GoTrustSamplePage> createState() => _GoTrustSamplePageState();
}

class _GoTrustSamplePageState extends State<GoTrustSamplePage> {
  static const String _baseUrl = 'YOUR_BASE_URL';
  static const String _sourceId = 'YOUR_SOURCE_ID';
  static const String _consentUserToken = 'YOUR_CONSENT_USER_TOKEN';
  static const String _pfUserToken = 'YOUR_PF_USER_TOKEN';

  static const Map<String, dynamic> _subjectIdentityValues = {
    'YOUR_FIELD_ID': 'YOUR_FIELD_VALUE',
  };

  static const Map<String, dynamic> _additionalParams = {};

  Future<bool?> _shouldOpenConsentForm(
      Map<String, dynamic> subjectIdentityValues,
      ) async {
    return GoTrustSdkHelper.showConsentForm(
      sourceId: _sourceId,
      userSpecificToken: _consentUserToken,
      baseUrl: _baseUrl,
      fallback: _fallback,
    );
  }

  static final FallbackConfig _fallback = FallbackConfig(
    displayUrl: 'YOUR_FALLBACK_DISPLAY_URL',
    submitUrl: 'YOUR_FALLBACK_SUBMIT_URL',
    refreshCsrfToken: () async => 'YOUR_CSRF_TOKEN',
  );

  Future<void> _openConsentForm() async {
    final shouldOpen =
    await _shouldOpenConsentForm(_subjectIdentityValues);

    if (shouldOpen != true) {
      _showMessage(
        'Consent already exists. Consent form is not required.',
      );
      return;
    }

    await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (_) => ConsentFormPage(
          token: _consentUserToken,
          sourceId: _sourceId,
          baseUrl: _baseUrl,
          fallback: _fallback,
          enableCaptcha: false,
          callbacks: ConsentCallbacks(
            onSuccess: (
                int? statusCode,
                String? message,
                List<Map<String, dynamic>>? data,
                ) {
              debugPrint('Consent Submitted! Status: $statusCode');

              if (data != null) {
                debugPrint('Received data: $data');
              }
            },
            onError: (int? statusCode, String? message) {
              debugPrint(
                'Consent Failed! Status: $statusCode, Message: $message',
              );
            },
            onClose: (String? message) {
              debugPrint(
                'Consent Form Closed by User: $message',
              );
            },
          ),
        ),
      ),
    );
  }

  Future<void> _openPreferenceCenter() async {
    await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (_) => LoginPage(
          token: _pfUserToken,
          baseUrl: _baseUrl,
          enableCaptcha: false,
        ),
      ),
    );
  }

  void _showMessage(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GoTrust Flutter Sample'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              'Open Consent Form and Preference Center',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w600,
              ),
            ),
            const SizedBox(height: 12),
            const Text(
              'Using hardcoded subject identity values:',
              style: TextStyle(
                fontSize: 12,
                color: Colors.black54,
              ),
            ),
            const SizedBox(height: 4),
            Text(
              _subjectIdentityValues.entries
                  .map((e) => '${e.key}: ${e.value}')
                  .join('\n'),
              style: const TextStyle(
                fontSize: 12,
                color: Colors.black54,
              ),
            ),
            const SizedBox(height: 16),
            FilledButton(
              onPressed: _openConsentForm,
              child: const Text('Open Consent Form'),
            ),
            const SizedBox(height: 12),
            OutlinedButton(
              onPressed: _openPreferenceCenter,
              child: const Text('Open Preference Center'),
            ),
          ],
        ),
      ),
    );
  }
}