Files
Prisma/lib/features/search/presentation/widgets/recent_searches.dart
T

160 lines
5.5 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,
),
),
],
),
);
}
final visibleItems =
_expanded ? widget.items : widget.items.take(20).toList();
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.symmetric(horizontal: 12, vertical: 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: 14,
color: theme.colorScheme.primary,
),
const SizedBox(width: 6),
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 140),
child: Text(
item.query,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w500,
),
),
),
if (item.resultCount > 0) ...[
const SizedBox(width: 6),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6, vertical: 2),
decoration: BoxDecoration(
color: theme.colorScheme.secondaryContainer,
borderRadius: BorderRadius.circular(8),
),
child: Text(
'${item.resultCount}',
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSecondaryContainer,
fontSize: 10,
fontWeight: FontWeight.bold,
),
),
),
],
if (widget.onDelete != null) ...[
const SizedBox(width: 4),
GestureDetector(
onTap: () => widget.onDelete!(item.id),
child: Padding(
padding: const EdgeInsets.all(2),
child: Icon(
Icons.close_rounded,
size: 14,
color: theme.colorScheme.onSurfaceVariant,
),
),
),
],
],
),
),
),
),
],
),
if (widget.items.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
? 'Свернуть'
: 'Показать все (${widget.items.length})',
),
),
),
],
],
);
}
}