311 lines
16 KiB
Dart
311 lines
16 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:lucide_icons/lucide_icons.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:share_plus/share_plus.dart';
|
|
import 'package:cross_file/cross_file.dart';
|
|
import 'auth_screen.dart';
|
|
import '../../../core/network/dio_client.dart';
|
|
import '../providers/auth_provider.dart';
|
|
import '../../transactions/providers/providers.dart';
|
|
|
|
import '../../../core/theme/theme_provider.dart';
|
|
import '../../business/providers/business_mode_provider.dart';
|
|
import '../../projects/providers/project_mode_provider.dart';
|
|
import '../../business/presentation/settings/business_settings_screen.dart';
|
|
|
|
class ProfileScreen extends ConsumerStatefulWidget {
|
|
const ProfileScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<ProfileScreen> createState() => _ProfileScreenState();
|
|
}
|
|
|
|
class _ProfileScreenState extends ConsumerState<ProfileScreen> {
|
|
bool _isExporting = false;
|
|
String? _profileType;
|
|
String _userName = 'Loading...';
|
|
String _userEmail = 'Loading...';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchProfileType();
|
|
}
|
|
|
|
Future<void> _fetchProfileType() async {
|
|
try {
|
|
final res = await DioClient().dio.get('/account/setup/status');
|
|
if (mounted) {
|
|
setState(() {
|
|
_profileType = res.data['profileType'];
|
|
_userName = res.data['name'] ?? 'Kifi User';
|
|
if (_userName.isEmpty) _userName = 'Kifi User';
|
|
_userEmail = res.data['email'] ?? '';
|
|
});
|
|
}
|
|
} catch (e) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
String _getInitials(String name) {
|
|
if (name.isEmpty || name == 'Kifi User' || name == 'Loading...') return 'U';
|
|
final parts = name.trim().split(' ');
|
|
if (parts.length > 1 && parts[1].isNotEmpty) {
|
|
return '${parts[0][0]}${parts[1][0]}'.toUpperCase();
|
|
}
|
|
return name.substring(0, name.length >= 2 ? 2 : 1).toUpperCase();
|
|
}
|
|
|
|
Future<void> _logout(BuildContext context) async {
|
|
const storage = FlutterSecureStorage();
|
|
await storage.delete(key: 'jwt_token');
|
|
|
|
if (context.mounted) {
|
|
Navigator.pushAndRemoveUntil(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const AuthScreen()),
|
|
(route) => false,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _exportData() async {
|
|
setState(() => _isExporting = true);
|
|
try {
|
|
final bytes = await ref.read(apiRepositoryProvider).exportTransactions();
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
final file = File('${tempDir.path}/transactions_export.csv');
|
|
await file.writeAsBytes(bytes);
|
|
|
|
final xFile = XFile(file.path);
|
|
await Share.shareXFiles([xFile], text: 'Here is my Kifi transactions export.');
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Export failed: $e')));
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() => _isExporting = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isDark = Theme.of(context).brightness == Brightness.dark;
|
|
return Scaffold(
|
|
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
|
|
appBar: AppBar(
|
|
title: Text('Profile', style: Theme.of(context).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold)),
|
|
backgroundColor: Colors.transparent,
|
|
elevation: 0,
|
|
centerTitle: true,
|
|
),
|
|
body: SafeArea(
|
|
child: Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 600),
|
|
child: SingleChildScrollView(
|
|
physics: const BouncingScrollPhysics(),
|
|
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const SizedBox(height: 20),
|
|
Center(
|
|
child: Container(
|
|
width: 100,
|
|
height: 100,
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).primaryColor.withOpacity(0.1),
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: Center(
|
|
child: Text(
|
|
_getInitials(_userName),
|
|
style: TextStyle(
|
|
fontSize: 36,
|
|
fontWeight: FontWeight.bold,
|
|
color: Theme.of(context).primaryColor,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
_userName,
|
|
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: Theme.of(context).textTheme.bodyLarge?.color),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
if (_userEmail.isNotEmpty) ...[
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
_userEmail,
|
|
style: TextStyle(color: isDark ? Colors.grey.shade400 : Colors.grey.shade600, fontSize: 16),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
const SizedBox(height: 48),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).cardColor,
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: [
|
|
BoxShadow(color: Colors.black.withOpacity(isDark ? 0.2 : 0.02), blurRadius: 10, offset: const Offset(0, 4)),
|
|
],
|
|
border: Border.all(color: Theme.of(context).dividerColor.withOpacity(isDark ? 0.1 : 0.2)),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
|
leading: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(color: Colors.blue.shade50, borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(LucideIcons.downloadCloud, color: Colors.blue.shade600, size: 22),
|
|
),
|
|
title: const Text('Export Data to CSV', style: TextStyle(fontWeight: FontWeight.w600)),
|
|
trailing: _isExporting
|
|
? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2))
|
|
: const Icon(LucideIcons.chevronRight, size: 20),
|
|
onTap: _isExporting ? null : _exportData,
|
|
),
|
|
Divider(height: 1, indent: 64, color: Theme.of(context).dividerColor.withOpacity(isDark ? 0.1 : 0.05)),
|
|
ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
|
leading: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(color: Colors.purple.shade50, borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(LucideIcons.moon, color: Colors.purple.shade600, size: 22),
|
|
),
|
|
title: const Text('Theme', style: TextStyle(fontWeight: FontWeight.w600)),
|
|
trailing: SegmentedButton<ThemeMode>(
|
|
segments: const [
|
|
ButtonSegment(value: ThemeMode.light, icon: Icon(LucideIcons.sun, size: 18)),
|
|
ButtonSegment(value: ThemeMode.system, icon: Icon(LucideIcons.monitor, size: 18)),
|
|
ButtonSegment(value: ThemeMode.dark, icon: Icon(LucideIcons.moon, size: 18)),
|
|
],
|
|
selected: {ref.watch(themeProvider)},
|
|
onSelectionChanged: (Set<ThemeMode> newSelection) {
|
|
ref.read(themeProvider.notifier).setTheme(newSelection.first);
|
|
},
|
|
showSelectedIcon: false,
|
|
style: ButtonStyle(
|
|
visualDensity: VisualDensity.compact,
|
|
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
|
),
|
|
),
|
|
),
|
|
Divider(height: 1, indent: 64, color: Theme.of(context).dividerColor.withOpacity(isDark ? 0.1 : 0.05)),
|
|
Consumer(
|
|
builder: (context, ref, child) {
|
|
final isBusinessMode = ref.watch(businessModeProvider);
|
|
return Column(
|
|
children: [
|
|
if (_profileType == 'BUSINESS') ...[
|
|
SwitchListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
|
secondary: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(color: Colors.orange.shade50, borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(LucideIcons.briefcase, color: Colors.orange.shade600, size: 22),
|
|
),
|
|
title: const Text('Business Mode', style: TextStyle(fontWeight: FontWeight.w600)),
|
|
subtitle: Text('Inventory and sales', style: TextStyle(fontSize: 12, color: Colors.grey.shade600)),
|
|
value: isBusinessMode,
|
|
activeColor: Theme.of(context).primaryColor,
|
|
onChanged: (val) {
|
|
ref.read(businessModeProvider.notifier).toggleMode();
|
|
},
|
|
),
|
|
if (isBusinessMode) ...[
|
|
Divider(height: 1, indent: 64, color: Theme.of(context).dividerColor.withOpacity(isDark ? 0.1 : 0.05)),
|
|
ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
|
leading: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(color: Colors.grey.shade100, borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(LucideIcons.settings, color: Colors.grey.shade700, size: 22),
|
|
),
|
|
title: const Text('Business Settings', style: TextStyle(fontWeight: FontWeight.w600)),
|
|
subtitle: Text('Tax, Modules, POS', style: TextStyle(fontSize: 12, color: Colors.grey.shade600)),
|
|
trailing: const Icon(LucideIcons.chevronRight, size: 20),
|
|
onTap: () {
|
|
Navigator.push(context, MaterialPageRoute(builder: (_) => const BusinessSettingsScreen()));
|
|
},
|
|
),
|
|
],
|
|
Divider(height: 1, indent: 64, color: Theme.of(context).dividerColor.withOpacity(isDark ? 0.1 : 0.05)),
|
|
Consumer(
|
|
builder: (context, ref, child) {
|
|
final isProjectMode = ref.watch(projectModeProvider);
|
|
return SwitchListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
|
secondary: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(color: Colors.teal.shade50, borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(LucideIcons.trello, color: Colors.teal.shade600, size: 22),
|
|
),
|
|
title: const Text('Project Management', style: TextStyle(fontWeight: FontWeight.w600)),
|
|
subtitle: Text('Task boards and tracking', style: TextStyle(fontSize: 12, color: Colors.grey.shade600)),
|
|
value: isProjectMode,
|
|
activeColor: Theme.of(context).primaryColor,
|
|
onChanged: (val) {
|
|
ref.read(projectModeProvider.notifier).toggleMode();
|
|
},
|
|
);
|
|
},
|
|
),
|
|
Divider(height: 1, indent: 64, color: Theme.of(context).dividerColor.withOpacity(isDark ? 0.1 : 0.05)),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
),
|
|
ListTile(
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
|
|
leading: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(color: Colors.green.shade50, borderRadius: BorderRadius.circular(10)),
|
|
child: Icon(LucideIcons.helpCircle, color: Colors.green.shade600, size: 22),
|
|
),
|
|
title: const Text('Help & Support', style: TextStyle(fontWeight: FontWeight.w600)),
|
|
trailing: const Icon(LucideIcons.chevronRight, size: 20),
|
|
onTap: () {},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 48),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton.icon(
|
|
onPressed: () => _logout(context),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red.withOpacity(0.08),
|
|
foregroundColor: Colors.red.shade700,
|
|
elevation: 0,
|
|
padding: const EdgeInsets.symmetric(vertical: 20),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
|
),
|
|
icon: const Icon(LucideIcons.logOut, size: 22),
|
|
label: const Text('Log Out', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, letterSpacing: 0.5)),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|