feat: Enhance SearchAppBar with debounce and customizable padding
- Added debounce functionality for search input in SearchAppBar to improve performance. - Introduced a controller parameter to allow external control of the search input. - Made padding customizable via a new padding parameter. - Improved leading button handling with an option to show/hide it. feat: Implement Category Editor Dialog - Created a new CategoryEditorDialog widget for editing category details. - Allows users to set category name, color, and icon with a preview. - Supports creating new categories with a confirmation dialog. feat: Add Category Menu Button - Introduced CategoryMenuButton for selecting categories with an option to create new ones. - Displays a menu with existing categories and allows editing. - Supports dynamic background color based on active state. feat: Create Note List View - Added NoteListView widget for displaying a list of notes with reordering capability. - Integrates refresh functionality and displays last sync time. - Supports selection and deletion of notes with visual feedback.
This commit is contained in:
@@ -1,29 +1,42 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:notas/theme/app_palette.dart';
|
||||
|
||||
class SearchAppBar extends StatefulWidget {
|
||||
const SearchAppBar({
|
||||
super.key,
|
||||
this.controller,
|
||||
this.onMenuPressed,
|
||||
this.onLeadingPressed,
|
||||
this.leadingIcon = Icons.menu,
|
||||
this.leadingTooltip = 'Menú',
|
||||
this.leadingWidget,
|
||||
this.showLeadingButton = true,
|
||||
this.trailingWidget,
|
||||
this.onSearchChanged,
|
||||
this.searchHint = 'Buscar notas...',
|
||||
this.searchMaxWidth = 600,
|
||||
this.searchDebounceDuration = const Duration(milliseconds: 300),
|
||||
this.padding = const EdgeInsets.only(left: 8, right: 20, top: 7, bottom: 7),
|
||||
this.showSearch = true,
|
||||
this.titleText,
|
||||
});
|
||||
|
||||
final TextEditingController? controller;
|
||||
final VoidCallback? onMenuPressed;
|
||||
final VoidCallback? onLeadingPressed;
|
||||
final IconData leadingIcon;
|
||||
final String leadingTooltip;
|
||||
final Widget? leadingWidget;
|
||||
final bool showLeadingButton;
|
||||
final Widget? trailingWidget;
|
||||
final ValueChanged<String>? onSearchChanged;
|
||||
final String searchHint;
|
||||
final double searchMaxWidth;
|
||||
final Duration searchDebounceDuration;
|
||||
final EdgeInsetsGeometry padding;
|
||||
final bool showSearch;
|
||||
final String? titleText;
|
||||
|
||||
@@ -32,60 +45,87 @@ class SearchAppBar extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _SearchAppBarState extends State<SearchAppBar> {
|
||||
late TextEditingController _searchController;
|
||||
void _onSearchChanged() {
|
||||
late final TextEditingController _searchController;
|
||||
late final bool _ownsController;
|
||||
Timer? _debounceTimer;
|
||||
|
||||
void _refreshState() {
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void _handleSearchInput(String value) {
|
||||
_refreshState();
|
||||
|
||||
if (widget.onSearchChanged == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
_debounceTimer?.cancel();
|
||||
_debounceTimer = Timer(widget.searchDebounceDuration, () {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
widget.onSearchChanged?.call(value.trim());
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_searchController = TextEditingController()..addListener(_onSearchChanged);
|
||||
_searchController = widget.controller ?? TextEditingController();
|
||||
_ownsController = widget.controller == null;
|
||||
_searchController.addListener(_refreshState);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.removeListener(_onSearchChanged);
|
||||
_searchController.dispose();
|
||||
_debounceTimer?.cancel();
|
||||
_searchController.removeListener(_refreshState);
|
||||
if (_ownsController) {
|
||||
_searchController.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final AppPalette palette = Theme.of(context).extension<AppPalette>()!;
|
||||
final Widget? leadingControl = widget.leadingWidget ??
|
||||
(widget.showLeadingButton
|
||||
? IconButton(
|
||||
onPressed: widget.onLeadingPressed ?? widget.onMenuPressed,
|
||||
icon: Icon(
|
||||
widget.leadingIcon,
|
||||
color: palette.textSecondary,
|
||||
size: 20,
|
||||
),
|
||||
tooltip: widget.leadingTooltip,
|
||||
splashRadius: 18,
|
||||
constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
|
||||
)
|
||||
: null);
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: palette.transparent,
|
||||
border: Border(bottom: BorderSide(color: palette.border, width: 0.5)),
|
||||
),
|
||||
padding: const EdgeInsets.only(left: 8, right: 20, top: 7, bottom: 7),
|
||||
padding: widget.padding,
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: widget.onLeadingPressed ?? widget.onMenuPressed,
|
||||
icon: Icon(
|
||||
widget.leadingIcon,
|
||||
color: palette.textSecondary,
|
||||
size: 20,
|
||||
),
|
||||
tooltip: widget.leadingTooltip,
|
||||
splashRadius: 18,
|
||||
constraints: const BoxConstraints(minWidth: 40, minHeight: 40),
|
||||
),
|
||||
if (widget.leadingWidget != null) ...[
|
||||
if (leadingControl != null) ...[
|
||||
leadingControl,
|
||||
const SizedBox(width: 8),
|
||||
Center(child: widget.leadingWidget!),
|
||||
],
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: widget.showSearch
|
||||
? Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 600),
|
||||
constraints: BoxConstraints(maxWidth: widget.searchMaxWidth),
|
||||
child: TextField(
|
||||
controller: _searchController,
|
||||
onChanged: widget.onSearchChanged,
|
||||
onChanged: _handleSearchInput,
|
||||
style: TextStyle(
|
||||
color: palette.textPrimary,
|
||||
fontSize: 13,
|
||||
@@ -104,6 +144,7 @@ class _SearchAppBarState extends State<SearchAppBar> {
|
||||
size: 18,
|
||||
),
|
||||
onPressed: () {
|
||||
_debounceTimer?.cancel();
|
||||
_searchController.clear();
|
||||
widget.onSearchChanged?.call('');
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user