go_trust_mobile_sdk 1.0.4
go_trust_mobile_sdk: ^1.0.4 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/services/go_trust_sdk_helper.dart';
import 'package:go_trust_mobile_sdk/features/consent_form/consent_form_page.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 = 'https://preprod.gotrust.tech';
static const String _sourceId =
'_J8u5pqsj_PanaXjxm-BwAw0D99zqnywhtG8RJWp5ZEDLg==';
static const String _customerId =
'4C3_opTopeRH5ScJzPvjSJbg_PW6LzxzMJoV1eSes7Y=';
static const String _preferenceFormId =
'AJfzcSYTaSfAN6xwuy2q3nbuJCtLkbMWQiB3xz0s';
static const String _token = "ed-tkQZQQtCVYBYRdDzlbzvqDAVCy9A4c04Htb57jQDDfXv1lZgbrypHNQd0Bq3B0YHVObcdOD_zuBf3p0o=";
// Hardcoded field-id : value pairs identifying the subject, so the
// multi-identity flow can be tested with a single tap — no input needed.
// Field ids come from your GoTrust form configuration (e.g. "107" = phone,
// "10085" = email). Add/remove entries here to try different combinations.
static const Map<String, dynamic> _subjectIdentityValues = {
"10085": "pratyush@appvintech.com"
// '10085': 'pratyush@appvintech.com',
};
// Optional extra field-id : value pairs — not used for consent lookup,
// just prefilled into the form.
static const Map<String, dynamic> _additionalParams = <String, dynamic>{};
Future<bool> _shouldOpenConsentForm(
Map<String, dynamic> subjectIdentityValues) async {
return GoTrustSdkHelper.showConsentForm(
_sourceId,
subjectIdentityValues,
_baseUrl,
);
}
Future<void> _openConsentForm() async {
final shouldOpen = await _shouldOpenConsentForm(_subjectIdentityValues);
if (!shouldOpen) {
_showMessage('Consent already exists. Consent form is not required.');
return;
}
await Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => ConsentFormPage(
sourceId: _sourceId,
// customerId: _customerId,
initialSubjectValues: _subjectIdentityValues,
additionalParams: _additionalParams,
baseUrl: _baseUrl,
enableCaptcha: false,
onSuccess: (
int? statusCode,
String? message,
List<Map<String, dynamic>>? data,
) {
debugPrint('Consent Submitted! Status: $statusCode');
if (data != null) {
print("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(
pfToken: _token,
// customerId: _customerId,
// preferenceFormId: _preferenceFormId,
consentId: _sourceId,
additionalParams: _additionalParams,
baseUrl: _baseUrl,
initialSubjectValues: _subjectIdentityValues,
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'),
),
],
),
),
);
}
}