Files
Prisma/lib/features/search/presentation/widgets/recent_searches.dart
T
2026-09-04 18:06:46 +03:00

207 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../backend/backend.dart';
class RecentSearches extends StatefulWidget {
const RecentSearches({
required this.items,
required this.onTap,
this.onDelete,
super.key,
});
final List<SearchHistory> items;
final ValueChanged<String> onTap;
final ValueChanged<String>? onDelete;
@override
State<RecentSearches> createState() => _RecentSearchesState();
}
class _RecentSearchesState extends State<RecentSearches> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
if (widget.items.isEmpty) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 24, horizontal: 16),
alignment: Alignment.center,
child: Column(
children: [
Icon(
Icons.history_toggle_off_rounded,
size: 40,
color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.5),
),
const SizedBox(height: 10),
Text(
'История поиска пуста',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
);
}
// Deduplicate items by normalized query
final seen = <String>{};
final uniqueItems = <SearchHistory>[];
for (final item in widget.items) {
final key = item.query.trim().toLowerCase();
if (key.isEmpty) continue;
if (seen.add(key)) {
uniqueItems.add(item);
}
}
if (uniqueItems.isEmpty) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 24, horizontal: 16),
alignment: Alignment.center,
child: Column(
children: [
Icon(
Icons.history_toggle_off_rounded,
size: 40,
color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.5),
),
const SizedBox(height: 10),
Text(
'История поиска пуста',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
);
}
final visibleItems =
_expanded ? uniqueItems : uniqueItems.take(20).toList();
final screenW = MediaQuery.sizeOf(context).width;
final maxQueryWidth = (screenW - 200).clamp(180.0, 480.0);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
spacing: 8,
runSpacing: 8,
children: [
for (final item in visibleItems)
Material(
color: theme.colorScheme.surfaceContainer,
borderRadius: BorderRadius.circular(16),
child: InkWell(
borderRadius: BorderRadius.circular(16),
onTap: () => widget.onTap(item.query),
child: Container(
padding:
const EdgeInsets.only(left: 10, right: 6, top: 6, bottom: 6),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: theme.colorScheme.outlineVariant
.withValues(alpha: 0.35),
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.history_rounded,
size: 15,
color: theme.colorScheme.primary,
),
const SizedBox(width: 6),
ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxQueryWidth),
child: Tooltip(
message: item.query,
waitDuration: const Duration(milliseconds: 600),
child: Text(
item.query,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
),
),
if (item.resultCount > 0) ...[
const SizedBox(width: 6),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: theme.colorScheme.primary.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(8),
),
child: Text(
'${item.resultCount}',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.primary,
fontSize: 11,
fontWeight: FontWeight.bold,
),
),
),
],
if (widget.onDelete != null) ...[
const SizedBox(width: 4),
Material(
color: Colors.transparent,
shape: const CircleBorder(),
clipBehavior: Clip.antiAlias,
child: InkWell(
customBorder: const CircleBorder(),
onTap: () => widget.onDelete!(item.id),
child: Padding(
padding: const EdgeInsets.all(4),
child: Icon(
Icons.close_rounded,
size: 15,
color: theme.colorScheme.onSurfaceVariant
.withValues(alpha: 0.75),
),
),
),
),
],
],
),
),
),
),
],
),
if (uniqueItems.length > 20) ...[
const SizedBox(height: 10),
Center(
child: TextButton.icon(
onPressed: () => setState(() => _expanded = !_expanded),
icon: Icon(
_expanded
? Icons.unfold_less_rounded
: Icons.unfold_more_rounded,
size: 16,
),
label: Text(
_expanded
? 'Свернуть'
: 'Показать все (${uniqueItems.length})',
),
),
),
],
],
);
}
}