我是 Flutter 的新手。我有一个应用程序,我想打开一个带有预填接收地址的电子邮件应用程序,以便用户可以联系我。我有下面的代码,但我无法以某种方式使其工作。由于某种原因,我没有为应用程序提供午餐。我也不确定它是否也是最好的方法。
ListTile(
onTap: () {
_launchEmail("[email protected]");
},
leading: const Icon(Icons.email, size: 24.0, color: Colors.white),
title: const Text("Contact us"),
textColor: Colors.white,
dense: true,
),
列表;
void _launchEmail(String toMail) async {
if (Platform.isIOS) {
var email = 'mailto:$toMail';
if (await canLaunch(email)) {
await launch(email);
} else {
throw 'LOG (THROW): Email açılamadı. Email: $email';
}
} else {
var email =
'mailto:$toMail?subject=${Uri.encodeFull('tome text here')}&body=${Uri.encodeFull('Mesaj')}';
if (await canLaunch(email)) {
await launch(email);
} else {
throw 'LOG (THROW): Email açılamadı. Email: $email';
}
}
}
launch
已弃用。使用launchUrl
代替。
Future<void> launchEmail({required String email, String? subject}) async {
final url =
Uri(scheme: 'mailto', path: email, queryParameters: {'subject': subject});
if (await launchUrl(url)) return;
throw Exception('Failed to launch url');
}