showCustomizeCookieConsentDialog function
dynamic
showCustomizeCookieConsentDialog(
- BuildContext context, {
- required CookieConsentLayout layout,
- required List<
CookeConsentCategory> categories, - required bool dismissible,
- required String acceptNecessaryCategoryId,
- required String acceptAllLabel,
- required bool showAcceptAll,
- required String customizeSaveLabel,
- required String customizeHeadline,
Implementation
showCustomizeCookieConsentDialog(
BuildContext context, {
/// Pick a layot for your dialog
required CookieConsentLayout layout,
/// 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.
required List<CookeConsentCategory> categories,
/// Whether the user can dismiss the sheet by clicking outside of it.
required bool dismissible,
/// This one cannot be toggled
required String acceptNecessaryCategoryId,
required String sharedPrefrencesPrefix,
required String acceptAllLabel,
required bool showAcceptAll,
required String customizeSaveLabel,
required String customizeHeadline,
}) {
customizeConsentBody(
BuildContext context, {
bool showTitle = false,
bool useCupertinoButtons = false,
bool useVerticalButtons = false,
}) {
int cnt = 0;
return StatefulBuilder(
builder: (context, setState) => FutureBuilder(
future: Future.wait(
categories.map(
(category) async {
return {
category.id: await _getCookieConsent(
category: category.id,
sharedPrefrencesPrefix: sharedPrefrencesPrefix)
};
},
),
),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: Padding(
padding: EdgeInsets.all(8.0),
child: CupertinoActivityIndicator(),
),
);
}
final state = <String, bool?>{
for (Map<String, bool?> part in snapshot.data!) ...part,
};
return Material(
color: Colors.transparent,
child: SizedBox(
width: 320,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Table(
columnWidths: const {
0: FlexColumnWidth(4),
1: FixedColumnWidth(66),
},
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: categories
.map((category) => TableRow(
children: [
ExpansionTile(
title: Text(category.name),
trailing: const Icon(
CupertinoIcons.question_circle_fill,
size: 18.0),
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0),
child: Text(category.description),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
CupertinoSwitch(
value: category.id ==
acceptNecessaryCategoryId
? true
: state[category.id] ?? false,
onChanged: category.id ==
acceptNecessaryCategoryId
? null
: (value) {
setCookieConsent(
category: category.id,
sharedPrefrencesPrefix:
sharedPrefrencesPrefix,
value: value);
setState(() => cnt++);
})
]),
],
))
.toList(),
),
const SizedBox(height: 20.0),
Row(
mainAxisSize: MainAxisSize.min,
children: [
CookieConsentButton(
label: customizeSaveLabel,
isPrimary: true,
onPressed: () {
Navigator.of(context).pop();
},
),
if (showAcceptAll)
CookieConsentButton(
label: acceptAllLabel,
onPressed: () async {
Navigator.of(context).pop();
for (final category in categories) {
await setCookieConsent(
category: category.id,
sharedPrefrencesPrefix:
sharedPrefrencesPrefix,
value: true);
}
},
)
],
),
],
),
),
);
},
),
);
}
switch (layout) {
case CookieConsentLayout.cupertinoBottomSheet:
case CookieConsentLayout.cupertinoAlert:
case CookieConsentLayout.floatingBottomSheet:
showCupertinoDialog(
context: context,
builder: (context) => CupertinoAlertDialog(
title: Text(customizeHeadline),
content: customizeConsentBody(context,
showTitle: false,
useCupertinoButtons: true,
useVerticalButtons: true),
),
barrierDismissible: dismissible,
);
break;
case CookieConsentLayout.materialAlert:
case CookieConsentLayout.materialBottomSheet:
case CookieConsentLayout.materialSnackBar:
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text(customizeHeadline),
content: customizeConsentBody(context, showTitle: false),
));
break;
}
}