app_review_service.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import 'package:flutter/material.dart';
  2. import 'package:in_app_review/in_app_review.dart';
  3. import 'package:url_launcher/url_launcher.dart';
  4. class AppReviewService {
  5. static final InAppReview _inAppReview = InAppReview.instance;
  6. static const String _appStoreId = '6743112546'; // 替换为你的App Store ID
  7. // 触发评价逻辑
  8. static Future<void> requestAppReview(BuildContext context) async {
  9. try {
  10. bool isAvailable = await _inAppReview.isAvailable();
  11. if (isAvailable) {
  12. // 尝试显示应用内评分
  13. await _inAppReview.requestReview();
  14. } else {
  15. // 不可用时直接跳转到App Store
  16. await _launchAppStore();
  17. }
  18. } catch (e) {
  19. // 任何错误都跳转到App Store
  20. await _launchAppStore();
  21. ScaffoldMessenger.of(context).showSnackBar(
  22. const SnackBar(content: Text('已为您打开应用商店评价页面'))
  23. );
  24. print('Error requesting review: $e');
  25. }
  26. }
  27. // 使用url_launcher直接打开App Store
  28. static Future<void> _launchAppStore() async {
  29. final url = Uri.parse('https://apps.apple.com/app/id$_appStoreId');
  30. if (await canLaunchUrl(url)) {
  31. await launchUrl(url, mode: LaunchMode.externalApplication);
  32. }
  33. }
  34. // 直接打开应用商店(备用方法)
  35. static Future<void> openStoreReview(BuildContext context) async {
  36. await _launchAppStore();
  37. }
  38. }