showCookieConsent function
dynamic
showCookieConsent(
- BuildContext context, {
- CookieConsentLayout layout = CookieConsentLayout.floatingBottomSheet,
- String title = 'Your privacy',
- String consent = 'By clicking [acceptallcookies], you agree that we can store cookies ' 'on your device and disclose information in accordance with our [cookiepolicy].',
- required Uri cookiePolicyUrl,
- String cookiePolicyLabel = 'Cookie Policy',
- bool showAcceptNecessary = true,
- String acceptNecessaryLabel = 'Only necessary cookies',
- String acceptNecessaryCategoryId = 'necessary',
- bool showAcceptAll = true,
- String acceptAllLabel = 'Accept all cookies',
- bool showCustomize = true,
- bool? showCustomizeLabel,
- bool? showCustomizeIcon,
- String customizeLabel = 'Customize settings',
- String customizeHeadline = 'Customize your choices',
- IconData customizeIcon = Icons.settings,
- bool showRejectAll = false,
- String rejectAllLabel = 'Reject all cookies',
- String customizeSaveLabel = 'Confirm my choices',
- List<
CookeConsentCategory> ? categories, - bool dismissible = true,
Implementation
showCookieConsent(
BuildContext context, {
/// Pick a layot for your dialog
CookieConsentLayout layout = CookieConsentLayout.floatingBottomSheet,
/// shared_prefrences is used to store the user's intent,
/// by combining this prefix and the category id
String sharedPrefrencesPrefix = defaultCookieConsentSharedPrefrencesPrefix,
/// Main dialog title
String title = 'Your privacy',
/// Main dialog consent text to display. You can use placeholders
/// [acceptallcookies] which inserts acceptAllLabel in bold and [cookiepolicy]
/// which inserts cookiePolicyLabel which is clickable and opens cookiePolicyUrl
/// in a web browser.
String consent =
'By clicking [acceptallcookies], you agree that we can store cookies '
'on your device and disclose information in accordance with our [cookiepolicy].',
/// Provide an url to your app's or website's cookie policy document
required Uri cookiePolicyUrl,
String cookiePolicyLabel = 'Cookie Policy',
/// Let user select only necessary cookies
bool showAcceptNecessary = true,
String acceptNecessaryLabel = 'Only necessary cookies',
String acceptNecessaryCategoryId = 'necessary',
/// Let user accept all cookies
bool showAcceptAll = true,
String acceptAllLabel = 'Accept all cookies',
/// Let user customize their consent choices
bool showCustomize = true,
bool? showCustomizeLabel,
bool? showCustomizeIcon,
String customizeLabel = 'Customize settings',
String customizeHeadline = 'Customize your choices',
IconData customizeIcon = Icons.settings,
/// Show reject all button
bool showRejectAll = false,
String rejectAllLabel = 'Reject all cookies',
String customizeSaveLabel = 'Confirm my choices',
/// Categories the user can opt in to. Preferably provide your own
/// list rather than using the default example, and tailor the descriptions
/// specifically to your app or website.
List<CookeConsentCategory>? categories,
/// Whether the user can dismiss the sheet by clicking outside of it.
bool dismissible = true,
}) {
categories ??= exampleCookieConsentCategories;
showCustomizeIcon ??= ![CookieConsentLayout.cupertinoAlert].contains(layout);
showCustomizeLabel ??= [
CookieConsentLayout.cupertinoBottomSheet,
CookieConsentLayout.cupertinoAlert
].contains(layout);
cookieConsentBody(
BuildContext context,
CookieConsentLayout layout, {
bool showTitle = false,
bool useCupertinoButtons = false,
bool useVerticalButtons = false,
Function? onPop,
}) {
onPop ??= () => Navigator.of(context).pop();
final buttons = [
if (showAcceptAll)
CookieConsentButton(
useCupertino: useCupertinoButtons,
isPrimary: true,
onPressed: () async {
onPop?.call();
for (CookeConsentCategory category in categories ?? []) {
await setCookieConsent(
category: category.id,
sharedPrefrencesPrefix: sharedPrefrencesPrefix,
value: true);
}
},
label: acceptAllLabel,
),
if (showAcceptNecessary)
CookieConsentButton(
useCupertino: useCupertinoButtons,
isPrimary: !showAcceptAll,
onPressed: () async {
onPop?.call();
await setCookieConsent(
category: acceptNecessaryCategoryId,
sharedPrefrencesPrefix: sharedPrefrencesPrefix,
value: true);
},
label: acceptNecessaryLabel,
),
if (showRejectAll)
CookieConsentButton(
useCupertino: useCupertinoButtons,
onPressed: () async {
onPop?.call();
for (CookeConsentCategory category in categories ?? []) {
await removeCookieConsent(
category: category.id,
sharedPrefrencesPrefix: sharedPrefrencesPrefix);
}
},
label: rejectAllLabel,
),
if (showCustomize && (showCustomizeIcon! || showCustomizeLabel!))
CookieConsentButton(
useCupertino: useCupertinoButtons,
icon: showCustomizeIcon ? customizeIcon : null,
onPressed: () {
onPop?.call();
showCustomizeCookieConsentDialog(
context,
layout: layout,
categories: categories!,
dismissible: dismissible,
acceptNecessaryCategoryId: acceptNecessaryCategoryId,
sharedPrefrencesPrefix: sharedPrefrencesPrefix,
acceptAllLabel: acceptAllLabel,
showAcceptAll: showAcceptAll,
customizeSaveLabel: customizeSaveLabel,
customizeHeadline: customizeHeadline,
);
},
label: showCustomizeLabel! ? customizeLabel : null,
),
];
List<TextSpan> consentSpans = [];
final patterns = [
'[acceptallcookies]',
'[cookiepolicy]',
];
int lastMatch = 0;
for (int i = 0; i < consent.length - 1; i++) {
if (i < lastMatch) continue;
for (final pattern in patterns) {
if (consent.substring(i).startsWith(pattern)) {
consentSpans.add(TextSpan(text: consent.substring(lastMatch, i)));
lastMatch = i + pattern.length;
switch (pattern) {
case '[acceptallcookies]':
consentSpans.add(
TextSpan(
text: acceptAllLabel,
style: const TextStyle(fontWeight: FontWeight.bold)),
);
break;
case '[cookiepolicy]':
consentSpans.add(TextSpan(
text: cookiePolicyLabel,
style: const TextStyle(fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () => launchUrl(cookiePolicyUrl),
));
break;
}
}
}
}
if (lastMatch < consent.length) {
consentSpans.add(TextSpan(text: consent.substring(lastMatch)));
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (showTitle)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
title,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
const SizedBox(height: 12.0),
RichText(
text: TextSpan(
style: TextStyle(
// color: Colors.white,
color: layout == CookieConsentLayout.materialSnackBar
? Colors.white
: Colors.black,
// fontStyle: FontStyle.italic,
),
children: consentSpans)),
const SizedBox(height: 12.0),
useVerticalButtons
? Column(
children: buttons,
)
: Row(
mainAxisSize: MainAxisSize.min,
children: buttons,
),
]);
}
switch (layout) {
case CookieConsentLayout.cupertinoBottomSheet:
showBarModalBottomSheet(
context: context,
builder: (_) => Padding(
padding: const EdgeInsets.all(20.0),
child: cookieConsentBody(context, layout,
showTitle: true, useCupertinoButtons: true),
),
expand: false,
);
break;
case CookieConsentLayout.cupertinoAlert:
showCupertinoDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: Text(title),
content: cookieConsentBody(context, layout,
showTitle: false,
useCupertinoButtons: true,
useVerticalButtons: true),
),
barrierDismissible: dismissible,
);
break;
case CookieConsentLayout.floatingBottomSheet:
showCustomModalBottomSheet(
context: context,
builder: (context) => Padding(
padding: const EdgeInsets.all(20.0),
child: cookieConsentBody(context, layout, showTitle: true),
),
containerWidget: (_, animation, child) => FloatingModal(
child: child,
),
expand: false,
);
break;
case CookieConsentLayout.materialAlert:
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(title),
content: cookieConsentBody(context, layout, showTitle: false),
));
break;
case CookieConsentLayout.materialBottomSheet:
showMaterialModalBottomSheet(
context: context,
builder: (_) => Padding(
padding: const EdgeInsets.all(20.0),
child: cookieConsentBody(context, layout, showTitle: true),
),
expand: false,
);
break;
case CookieConsentLayout.materialSnackBar:
final snackBar = SnackBar(
content: cookieConsentBody(context, layout,
showTitle: true,
onPop: () => ScaffoldMessenger.of(context).hideCurrentSnackBar()),
duration: const Duration(seconds: 9999999),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
break;
}
}