search_bar.dart 914 B

12345678910111213141516171819202122232425262728293031323334
  1. import 'package:flutter/material.dart';
  2. class SearchBarWidget extends StatelessWidget {
  3. final String hint;
  4. final ValueChanged<String> onChanged;
  5. final VoidCallback? onClear;
  6. final TextEditingController? controller;
  7. const SearchBarWidget({
  8. super.key,
  9. this.hint = 'Cerca...',
  10. required this.onChanged,
  11. this.onClear,
  12. this.controller,
  13. });
  14. @override
  15. Widget build(BuildContext context) {
  16. return Padding(
  17. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  18. child: TextField(
  19. controller: controller,
  20. decoration: InputDecoration(
  21. hintText: hint,
  22. prefixIcon: const Icon(Icons.search),
  23. suffixIcon: controller != null && controller!.text.isNotEmpty
  24. ? IconButton(icon: const Icon(Icons.clear), onPressed: onClear)
  25. : null,
  26. ),
  27. onChanged: onChanged,
  28. ),
  29. );
  30. }
  31. }