193 lines
6.5 KiB
Dart
193 lines
6.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../core/app_colors.dart';
|
|
import '../core/app_design_tokens.dart';
|
|
import '../core/navigation_provider.dart';
|
|
import '../providers/ai_consent_provider.dart';
|
|
import '../providers/auth_provider.dart';
|
|
import '../pages/settings/ai_consent_details_page.dart';
|
|
|
|
class AiConsentGate extends ConsumerStatefulWidget {
|
|
final Widget child;
|
|
const AiConsentGate({super.key, required this.child});
|
|
|
|
@override
|
|
ConsumerState<AiConsentGate> createState() => _AiConsentGateState();
|
|
}
|
|
|
|
class _AiConsentGateState extends ConsumerState<AiConsentGate> {
|
|
String? _loadedUserId;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final user = ref.watch(authProvider).user;
|
|
if (user == null || user.id.isEmpty || user.role != 'User') {
|
|
return widget.child;
|
|
}
|
|
|
|
if (_loadedUserId != user.id) {
|
|
_loadedUserId = user.id;
|
|
Future<void>.microtask(
|
|
() => ref.read(aiConsentProvider.notifier).load(user.id),
|
|
);
|
|
}
|
|
|
|
final consent = ref.watch(aiConsentProvider);
|
|
final currentRoute = ref.watch(currentRouteProvider).name;
|
|
final isConsentInformationRoute =
|
|
currentRoute == 'aiConsentDetails' || currentRoute == 'staticText';
|
|
if (consent.userId != user.id || consent.isLoading) {
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: CircularProgressIndicator(color: AppColors.primary),
|
|
),
|
|
);
|
|
}
|
|
if (consent.granted || isConsentInformationRoute) return widget.child;
|
|
|
|
return Scaffold(
|
|
body: DecoratedBox(
|
|
decoration: const BoxDecoration(gradient: AppColors.bgGradient),
|
|
child: SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 400),
|
|
child: _ConsentCard(userId: user.id),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ConsentCard extends ConsumerWidget {
|
|
final String userId;
|
|
const _ConsentCard({required this.userId});
|
|
|
|
Future<void> _grant(BuildContext context, WidgetRef ref) async {
|
|
final succeeded = await ref.read(aiConsentProvider.notifier).grant(userId);
|
|
if (!succeeded && context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Authorization could not be saved.')),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _decline(BuildContext context, WidgetRef ref) async {
|
|
await ref.read(authProvider.notifier).logout();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final consent = ref.watch(aiConsentProvider);
|
|
return Container(
|
|
padding: const EdgeInsets.fromLTRB(20, 10, 20, 20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white.withValues(alpha: 0.96),
|
|
borderRadius: AppRadius.cardBorder,
|
|
border: Border.all(color: AppColors.primaryLight, width: 1),
|
|
boxShadow: AppShadows.panel,
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Align(
|
|
child: Image.asset(
|
|
'assets/branding/health_login_character_transparent.png',
|
|
height: 112,
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
const SizedBox(height: 2),
|
|
Align(
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.primarySoft,
|
|
borderRadius: AppRadius.pillBorder,
|
|
),
|
|
child: const Text(
|
|
'AI Service',
|
|
style: TextStyle(
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.primaryDark,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 2),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Icon(
|
|
Icons.auto_awesome,
|
|
size: 40,
|
|
color: AppColors.primary,
|
|
),
|
|
const SizedBox(height: 14),
|
|
const Text(
|
|
'AI 健康服务授权',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(fontSize: 21, fontWeight: FontWeight.w700),
|
|
),
|
|
const SizedBox(height: 14),
|
|
const Text(
|
|
'小脉健康的核心功能使用第三方 AI 服务。经你同意后,你主动输入的健康信息、对话内容、图片和报告可能会发送给相关 AI 服务,用于健康记录、饮食识别、报告整理和 AI 回复。',
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
height: 1.6,
|
|
color: AppColors.textSecondary,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
const Text(
|
|
'AI 生成内容仅供健康管理参考,不能替代医生诊断或治疗。',
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
height: 1.5,
|
|
color: AppColors.textSecondary,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => const AiConsentDetailsPage(),
|
|
),
|
|
);
|
|
},
|
|
child: const Text('查看《AI 数据处理说明》'),
|
|
),
|
|
const SizedBox(height: 8),
|
|
FilledButton(
|
|
onPressed: consent.isSaving
|
|
? null
|
|
: () => _grant(context, ref),
|
|
child: const Text('同意并继续'),
|
|
),
|
|
const SizedBox(height: 8),
|
|
OutlinedButton(
|
|
onPressed: consent.isSaving
|
|
? null
|
|
: () => _decline(context, ref),
|
|
child: const Text('不同意并退出'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|