144 lines
4.2 KiB
Dart
144 lines
4.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../../../core/theme/app_colors.dart';
|
|
|
|
class SearchInputBar extends StatefulWidget {
|
|
final ValueChanged<String> onSearch;
|
|
final VoidCallback onClear;
|
|
final bool isLoading;
|
|
|
|
const SearchInputBar({
|
|
super.key,
|
|
required this.onSearch,
|
|
required this.onClear,
|
|
this.isLoading = false,
|
|
});
|
|
|
|
@override
|
|
State<SearchInputBar> createState() => _SearchInputBarState();
|
|
}
|
|
|
|
class _SearchInputBarState extends State<SearchInputBar> {
|
|
final TextEditingController _controller = TextEditingController();
|
|
final FocusNode _focusNode = FocusNode();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_focusNode.requestFocus();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
_focusNode.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _submit() {
|
|
final text = _controller.text.trim();
|
|
if (text.isNotEmpty) {
|
|
widget.onSearch(text);
|
|
}
|
|
}
|
|
|
|
void _clear() {
|
|
_controller.clear();
|
|
widget.onClear();
|
|
_focusNode.requestFocus();
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: AppColors.background,
|
|
borderRadius: BorderRadius.circular(14),
|
|
border: Border.all(color: AppColors.border),
|
|
),
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 4),
|
|
child: Row(
|
|
children: [
|
|
const Icon(
|
|
Icons.search_rounded,
|
|
color: AppColors.primary,
|
|
size: 22,
|
|
),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _controller,
|
|
focusNode: _focusNode,
|
|
textInputAction: TextInputAction.search,
|
|
onSubmitted: (_) => _submit(),
|
|
onChanged: (_) => setState(() {}),
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
color: AppColors.textPrimary,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
decoration: const InputDecoration(
|
|
hintText: 'Nombre, apellido, cédula o teléfono...',
|
|
hintStyle: TextStyle(
|
|
fontSize: 13,
|
|
color: AppColors.textMuted,
|
|
fontWeight: FontWeight.w400,
|
|
),
|
|
border: InputBorder.none,
|
|
focusedBorder: InputBorder.none,
|
|
enabledBorder: InputBorder.none,
|
|
isDense: true,
|
|
contentPadding: EdgeInsets.symmetric(vertical: 12),
|
|
),
|
|
),
|
|
),
|
|
if (_controller.text.isNotEmpty)
|
|
IconButton(
|
|
icon: const Icon(Icons.close_rounded, size: 18, color: AppColors.textMuted),
|
|
tooltip: 'Limpiar',
|
|
onPressed: _clear,
|
|
),
|
|
const SizedBox(width: 6),
|
|
ElevatedButton(
|
|
onPressed: widget.isLoading ? null : _submit,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.primary,
|
|
foregroundColor: Colors.white,
|
|
elevation: 0,
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
),
|
|
child: widget.isLoading
|
|
? const SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
|
|
),
|
|
)
|
|
: const Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
'Buscar',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
SizedBox(width: 6),
|
|
Icon(Icons.keyboard_return_rounded, size: 15),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|