import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter/foundation.dart' show kIsWeb, defaultTargetPlatform; import 'package:flutter/foundation.dart' show TargetPlatform; import '../providers/time_tracking_provider.dart'; import '../widgets/error_state_widget.dart'; class PermissionScreen extends ConsumerWidget { const PermissionScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final permissionStatus = ref.watch(permissionStatusProvider); final theme = Theme.of(context); return Scaffold( appBar: AppBar( title: const Text('权限设置'), ), body: SingleChildScrollView( padding: const EdgeInsets.all(24), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // 图标 Icon( Icons.security, size: 80, color: theme.colorScheme.primary, ), const SizedBox(height: 24), // 标题 Text( '我们需要访问您的应用使用数据', style: theme.textTheme.headlineSmall?.copyWith( fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), const SizedBox(height: 16), // 说明 Text( _getPermissionDescription(), style: theme.textTheme.bodyLarge?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.7), ), textAlign: TextAlign.center, ), const SizedBox(height: 32), // 权限状态 permissionStatus.when( data: (hasPermission) { if (hasPermission) { return _buildPermissionGranted(context, theme); } else { return _buildPermissionRequest(context, ref, theme); } }, loading: () => const Center(child: CircularProgressIndicator()), error: (error, stack) => ErrorStateWidget.generic( message: '检查权限时出错,请重试', onRetry: () { ref.invalidate(permissionStatusProvider); }, ), ), const SizedBox(height: 32), // 隐私说明 _buildPrivacyInfo(theme), ], ), ), ); } String _getPermissionDescription() { if (kIsWeb) { return 'Web 平台暂不支持时间追踪功能。\n\n' '请使用 iOS 或 Android 应用。'; } else if (defaultTargetPlatform == TargetPlatform.iOS) { return '这样我们才能自动追踪您的应用使用情况,无需手动操作。\n\n' '• 完全自动化,无需手动操作\n' '• 数据仅存储在本地\n' '• 不会上传到服务器'; } else { return '这样我们才能自动追踪您的应用使用情况,无需手动操作。\n\n' '• 完全自动化,无需手动操作\n' '• 数据仅存储在本地\n' '• 不会上传到服务器'; } } Widget _buildPermissionRequest(BuildContext context, WidgetRef ref, ThemeData theme) { return Column( children: [ Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: theme.colorScheme.errorContainer, borderRadius: BorderRadius.circular(12), ), child: Row( children: [ Icon( Icons.warning_amber_rounded, color: theme.colorScheme.error, ), const SizedBox(width: 12), Expanded( child: Text( '权限未授予', style: theme.textTheme.bodyLarge?.copyWith( color: theme.colorScheme.error, fontWeight: FontWeight.w600, ), ), ), ], ), ), const SizedBox(height: 24), ElevatedButton( onPressed: () async { final service = ref.read(timeTrackingServiceProvider); final granted = await service.requestPermission(); if (granted) { ref.invalidate(permissionStatusProvider); if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('权限已授予')), ); } } else { if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('权限授予失败,请前往设置中手动开启')), ); } } }, style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: const Text( '去设置', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600), ), ), const SizedBox(height: 12), TextButton( onPressed: () { Navigator.of(context).pop(); }, child: const Text('稍后再说'), ), ], ); } Widget _buildPermissionGranted(BuildContext context, ThemeData theme) { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: theme.colorScheme.primaryContainer, borderRadius: BorderRadius.circular(12), ), child: Row( children: [ Icon( Icons.check_circle, color: theme.colorScheme.primary, ), const SizedBox(width: 12), Expanded( child: Text( '权限已授予', style: theme.textTheme.bodyLarge?.copyWith( color: theme.colorScheme.primary, fontWeight: FontWeight.w600, ), ), ), ], ), ); } Widget _buildPrivacyInfo(ThemeData theme) { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: theme.cardColor, borderRadius: BorderRadius.circular(12), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( '隐私说明', style: theme.textTheme.titleMedium?.copyWith( fontWeight: FontWeight.w600, ), ), const SizedBox(height: 8), Text( '• 所有数据仅存储在您的设备本地\n' '• 我们不会收集或上传任何数据到服务器\n' '• 您可以随时删除所有数据\n' '• 应用使用数据仅用于统计和分析', style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.7), ), ), ], ), ); } }