import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../providers/providers.dart'; import '../services/purchase_service.dart'; import '../theme/app_theme.dart'; /// Dismissible banner shown at the top of the chat screen when a free-tier /// limit has been reached. Tapping "Upgrade" initiates the IAP flow. class PaywallBanner extends ConsumerStatefulWidget { const PaywallBanner({super.key}); @override ConsumerState createState() => _PaywallBannerState(); } class _PaywallBannerState extends ConsumerState { bool _dismissed = false; @override Widget build(BuildContext context) { final isPro = ref.watch(isProProvider); if (isPro || _dismissed) return const SizedBox.shrink(); final sessions = ref.watch(sessionsProvider); if (sessions.length <= kFreeTierMaxSessions) return const SizedBox.shrink(); return Material( color: Colors.transparent, child: Container( margin: const EdgeInsets.fromLTRB(8, 4, 8, 0), decoration: BoxDecoration( color: AppColors.accent.withAlpha(230), borderRadius: BorderRadius.circular(10), ), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), child: Row( children: [ const Icon(Icons.lock_outline, color: Colors.white, size: 18), const SizedBox(width: 8), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ const Text( 'PAILot Pro', style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold, fontSize: 13, ), ), const Text( 'Unlimited sessions & persistent messages', style: TextStyle(color: Colors.white70, fontSize: 11), ), ], ), ), const SizedBox(width: 8), TextButton( onPressed: _handleRestore, style: TextButton.styleFrom( foregroundColor: Colors.white70, padding: const EdgeInsets.symmetric(horizontal: 8), minimumSize: Size.zero, tapTargetSize: MaterialTapTargetSize.shrinkWrap, ), child: const Text('Restore', style: TextStyle(fontSize: 11)), ), ElevatedButton( onPressed: _handleUpgrade, style: ElevatedButton.styleFrom( backgroundColor: Colors.white, foregroundColor: AppColors.accent, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), minimumSize: Size.zero, tapTargetSize: MaterialTapTargetSize.shrinkWrap, textStyle: const TextStyle( fontSize: 12, fontWeight: FontWeight.bold, ), ), child: const Text('Upgrade \$4.99'), ), const SizedBox(width: 4), GestureDetector( onTap: () => setState(() => _dismissed = true), child: const Icon(Icons.close, color: Colors.white70, size: 16), ), ], ), ), ), ); } Future _handleUpgrade() async { await PurchaseService.instance.purchaseFullAccess(); } Future _handleRestore() async { await PurchaseService.instance.restorePurchases(); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Checking for previous purchases...'), duration: Duration(seconds: 2), ), ); } } }