Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixUI/hesabix_ui/lib/pages/system_settings_page.dart
2026-05-05 02:03:23 +03:30

408 lines
12 KiB
Dart
Executable file

import 'package:flutter/material.dart';
import 'package:hesabix_ui/l10n/app_localizations.dart';
import 'package:hesabix_ui/core/api_client.dart';
import 'system_settings/models/settings_category.dart';
import 'system_settings/models/settings_item.dart';
import 'system_settings/services/settings_categorization_service.dart';
import 'system_settings/widgets/settings_search_bar.dart';
import 'system_settings/widgets/settings_category_section.dart';
/// صفحه اصلی پنل مدیریت تنظیمات سیستم
class SystemSettingsPage extends StatefulWidget {
const SystemSettingsPage({super.key});
@override
State<SystemSettingsPage> createState() => _SystemSettingsPageState();
}
class _SystemSettingsPageState extends State<SystemSettingsPage> {
final Map<String, bool> _categoryExpansionStates = {};
String _searchQuery = '';
bool _isSearching = false;
List<SettingsItem> _searchResults = [];
late List<SettingsCategory> _categories;
bool _isSuperAdmin = false;
@override
void initState() {
super.initState();
_checkAdminAccess();
_initializeCategories();
}
void _checkAdminAccess() {
final authStore = ApiClient.getAuthStore();
if (authStore != null) {
_isSuperAdmin = authStore.isSuperAdmin;
}
}
void _initializeCategories() {
_categories = SettingsCategorizationService.getCategories();
// فیلتر دسته‌ها و آیتم‌ها بر اساس دسترسی
if (!_isSuperAdmin) {
_categories = _categories
.where((category) => !category.requiresSuperAdmin)
.map((category) {
// فیلتر کردن آیتم‌های هر دسته
final filteredItems = category.items
.where((item) => !item.requiresSuperAdmin)
.toList();
return category.copyWith(items: filteredItems);
})
.toList();
}
// همه دسته‌ها به صورت پیش‌فرض باز هستند
for (var category in _categories) {
_categoryExpansionStates[category.id] = true;
}
}
void _onSearchChanged(String query) {
if (!mounted) return;
final t = AppLocalizations.of(context);
setState(() {
_searchQuery = query;
_isSearching = query.trim().isNotEmpty;
if (_isSearching) {
_searchResults = SettingsCategorizationService.searchItems(
query,
t: t,
categories: _categories,
);
} else {
_searchResults = [];
}
});
}
void _onCategoryExpansionChanged(String categoryId, bool isExpanded) {
setState(() {
_categoryExpansionStates[categoryId] = isExpanded;
});
}
void _expandAllCategories() {
setState(() {
for (var category in _categories) {
_categoryExpansionStates[category.id] = true;
}
});
}
void _collapseAllCategories() {
setState(() {
for (var category in _categories) {
_categoryExpansionStates[category.id] = false;
}
});
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;
final t = AppLocalizations.of(context);
final width = MediaQuery.sizeOf(context).width;
const kDesktopLoose = 900.0;
final isDesktopLoose = width >= kDesktopLoose;
final pagePadding = isDesktopLoose
? const EdgeInsets.fromLTRB(8, 8, 8, 12)
: const EdgeInsets.all(16);
final sectionGap =
isDesktopLoose ? 12.0 : 20.0;
final innerGap = isDesktopLoose ? 12.0 : 16.0;
return Scaffold(
backgroundColor: colorScheme.surface,
body: SingleChildScrollView(
padding: pagePadding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildWelcomeSection(theme, colorScheme, t, width),
SizedBox(height: sectionGap),
SettingsSearchBar(
onSearchChanged: _onSearchChanged,
initialQuery: _searchQuery,
dense: isDesktopLoose,
),
if (!_isSearching) _buildControlButtons(theme, colorScheme, t),
SizedBox(height: innerGap),
_isSearching
? _buildSearchResults(theme, colorScheme, t, isDesktopLoose)
: _buildCategoriesList(theme, colorScheme, t, isDesktopLoose),
SizedBox(height: isDesktopLoose ? 12 : 20),
],
),
),
);
}
Widget _buildWelcomeSection(
ThemeData theme,
ColorScheme colorScheme,
AppLocalizations t,
double width,
) {
final totalItems = _categories.fold<int>(
0,
(sum, category) => sum + category.items.length,
);
const kNarrowWelcome = 560.0;
final statsText =
'${_categories.length} ${t.settingsCategoriesCount} • $totalItems ${t.settingsCount}';
final statsStyle = theme.textTheme.bodySmall?.copyWith(
color: colorScheme.onSurface.withValues(alpha: 0.6),
);
final titleBlock = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.systemAdministration,
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
color: colorScheme.onSurface,
fontSize: 24,
),
),
const SizedBox(height: 4),
Text(
t.systemSettingsDescription,
style: theme.textTheme.bodyMedium?.copyWith(
color: colorScheme.onSurface.withValues(alpha: 0.7),
),
),
],
);
if (width < kNarrowWelcome) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
titleBlock,
const SizedBox(height: 10),
Text(statsText, style: statsStyle),
],
);
}
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(child: titleBlock),
const SizedBox(width: 12),
Flexible(
child: Text(
statsText,
style: statsStyle,
textAlign: TextAlign.end,
),
),
],
);
}
Widget _buildControlButtons(
ThemeData theme,
ColorScheme colorScheme,
AppLocalizations t,
) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: _expandAllCategories,
style: TextButton.styleFrom(
foregroundColor: colorScheme.primary,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
minimumSize: Size.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: Text(
t.expandAllCategories,
style: TextStyle(fontSize: 13),
),
),
const SizedBox(width: 8),
TextButton(
onPressed: _collapseAllCategories,
style: TextButton.styleFrom(
foregroundColor: colorScheme.primary,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
minimumSize: Size.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: Text(
t.collapseAllCategories,
style: TextStyle(fontSize: 13),
),
),
],
);
}
Widget _buildCategoriesList(
ThemeData theme,
ColorScheme colorScheme,
AppLocalizations t,
bool compactSpacing,
) {
if (_categories.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Text(
t.noSettingsFound,
style: theme.textTheme.bodyLarge?.copyWith(
color: colorScheme.onSurface.withValues(alpha: 0.5),
),
),
),
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.availableSettings,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: colorScheme.onSurface,
),
),
SizedBox(height: compactSpacing ? 12 : 16),
..._categories.map((category) {
return SettingsCategorySection(
key: ValueKey(category.id),
category: category,
isExpanded: _categoryExpansionStates[category.id] ?? true,
onExpansionChanged: (isExpanded) =>
_onCategoryExpansionChanged(category.id, isExpanded),
searchQuery: null,
showSearchResults: false,
);
}),
],
);
}
Widget _buildSearchResults(
ThemeData theme,
ColorScheme colorScheme,
AppLocalizations t,
bool compactSpacing,
) {
if (_searchResults.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
children: [
Icon(
Icons.search_off,
size: 64,
color: colorScheme.onSurface.withValues(alpha: 0.3),
),
const SizedBox(height: 16),
Text(
t.noSearchResults(_searchQuery),
style: theme.textTheme.bodyLarge?.copyWith(
color: colorScheme.onSurface.withValues(alpha: 0.5),
),
),
],
),
),
);
}
// نمایش نتایج جستجو به صورت دسته‌بندی شده
final Map<String, List<SettingsItem>> groupedResults = {};
for (var item in _searchResults) {
if (!groupedResults.containsKey(item.categoryId)) {
groupedResults[item.categoryId] = [];
}
groupedResults[item.categoryId]!.add(item);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
t.searchResults,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: colorScheme.onSurface,
),
),
const SizedBox(width: 12),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 6,
),
decoration: BoxDecoration(
color: colorScheme.primary.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(20),
),
child: Text(
t.searchResultCount(_searchResults.length),
style: TextStyle(
color: colorScheme.primary,
fontWeight: FontWeight.bold,
fontSize: 12,
),
),
),
],
),
SizedBox(height: compactSpacing ? 12 : 16),
...groupedResults.entries.map((entry) {
final categoryId = entry.key;
final items = entry.value;
final categoryMatch =
_categories.where((cat) => cat.id == categoryId).toList();
if (categoryMatch.isEmpty) {
return const SizedBox.shrink();
}
final category = categoryMatch.first;
// ایجاد یک دسته موقت فقط با آیتم‌های جستجو شده
final searchCategory = SettingsCategory(
id: category.id,
title: category.title,
description: category.description,
icon: category.icon,
color: category.color,
items: items,
order: category.order,
requiresSuperAdmin: category.requiresSuperAdmin,
);
return SettingsCategorySection(
key: ValueKey('search_${category.id}'),
category: searchCategory,
isExpanded: true,
searchQuery: _searchQuery,
showSearchResults: true,
);
}),
],
);
}
}