我有一个应用程序,可以通过 WhatsApp 消息共享记录。我面临的问题是我有一个对话框,我在其中询问用户是否想要共享记录。我的应用程序不显示对话框并直接启动 WhatsApp 应用程序。我无法理解为什么会发生这种情况。如果没有 WhatsApp 启动功能,对话框会显示,但有了该功能,对话框不会显示。
我的对话
void whatsAppDialog(
{required BuildContext context, required Function onPressed}) {
if (Platform.isIOS) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: const Text('Share Record'),
content: const Text('Do you want to share this record?'),
actions: [
CupertinoDialogAction(
child: const Text('Cancel'),
onPressed: () => Navigator.pop(context),
),
CupertinoDialogAction(
onPressed: onPressed(),
child: const Text('Share'),
),
],
);
});
} else {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Share Record'),
content: const Text('Do you want share this record?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel')),
TextButton(onPressed: onPressed(), child: const Text('Share'))
],
);
});
}
}
我的 WhatsApp 启动功能。
launchWhatsApp({
String? studentName,
String? mobileNumber,
List<Records>? savedRecords,
double? avgGrade,
String? date,
String? remarks,
}) async {
// Initialize the message with student name and average grade
String message =
'Date: $formattedDate\n\nStudent Name: $studentName\n\nAverage Grade: $avgGrade\n\nRemarks: $remarks\n\n';
// Iterate through each saved record and add its details to the message
if (savedRecords != null && savedRecords.isNotEmpty) {
for (int i = 0; i < savedRecords.length; i++) {
Records record = savedRecords[i];
message += 'Record ${i + 1}:\n';
message += 'Juz: ${record.juz}\n';
message += 'Surah: ${record.surah}\n';
message += 'Page: ${record.page ?? '-'}\n'; // Handle null page
message += 'Ayat: ${record.ayat ?? '-'}\n'; // Handle null ayat
message += 'Grade: ${record.grade}\n\n';
}
}
// Create the WhatsApp link with the composed message
final link = WhatsAppUnilink(
phoneNumber: mobileNumber,
text: message,
);
// Launch the WhatsApp link
await launchUrl(link.asUri());
}
我的按钮,我将记录保存到 firebase 数据库,然后启动对话框。
onPressed: () {
context.read<MurajaatRecordCubit>().saveRecordsToFirestore(
studentModel,
remarks: remarksController.text.isEmpty
? 'no remarks'
: remarksController.text)
.then(
(value) => Future.delayed(
const Duration(seconds: 3),
() => whatsAppDialog(
onPressed: () => launchWhatsApp(
mobileNumber: studentModel.mobileNumber,
studentName:
'${studentModel.studentName} ${studentModel.fatherName} ${studentModel.surname}\n',
savedRecords: murajaatState.savedRecords,
avgGrade: murajaatState.avgGrade,
remarks: remarksController.text),
context: context),
),
);
我正在使用whatsapp_unilink包和url_launcher包。我想显示对话框,然后用户可以选择是否共享记录。
您面临的问题似乎与您在 WhatsAppDialog 函数中处理 onPressed 函数的方式有关。
CupertinoDialogAction( onPressed: onPressed(), // Issue here child: const Text('Share'), ),
在这里,您立即调用 onPressed 函数,而不是传递对其的引用。因此,该函数在对话框构建后立即调用,而不是在用户点击“共享”按钮时调用。 要解决此问题,您应该传递对 onPressed 函数的引用,而不是立即调用它。像这样修改你的 CupertinoDialogAction 和 TextButton:
CupertinoDialogAction( onPressed: onPressed, // Pass a reference to onPressed child: const Text('Share'), ),
TextButton( onPressed: onPressed, // Pass a reference to onPressed child: const Text('Share') )
通过将引用传递给 onPressed 函数而不调用它,只有当用户点击对话框中的“共享”按钮时才会调用该函数。
确保在 WhatsAppDialog 函数的 iOS 和非 iOS 分支中应用此更改。这应该可以解决问题并确保在启动 WhatsApp 应用程序之前正确显示对话框。