Advertisement
abimulya_

Untitled

Sep 12th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.09 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_hooks/flutter_hooks.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:get/get_rx/src/rx_workers/utils/debouncer.dart';
  5.  
  6. import 'one_form_component.dart';
  7.  
  8. class SearchFormComponent extends HookWidget {
  9.   const SearchFormComponent({
  10.     Key? key,
  11.     this.isUseBarcodeScanner = true,
  12.     required this.hintText,
  13.     required this.searchCtr,
  14.     this.descScannerText = '',
  15.     required this.onSearchChanged,
  16.   }) : super(key: key);
  17.  
  18.   final bool isUseBarcodeScanner;
  19.   final String hintText;
  20.   final TextEditingController searchCtr;
  21.   final String descScannerText;
  22.   final Function(String value) onSearchChanged;
  23.  
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     final _debouncer = useMemoized(
  27.       () => Debouncer(
  28.         delay: const Duration(milliseconds: 500),
  29.       ),
  30.     );
  31.     final _focusNode = useFocusNode();
  32.     final _isSearchEmpty = useState(true);
  33.  
  34.     useEffect(() {
  35.       searchCtr.addListener(
  36.         () => _isSearchEmpty.value = searchCtr.text.isEmpty,
  37.       );
  38.       return () {};
  39.     }, const []);
  40.  
  41.     return SizedBox(
  42.       width: isUseBarcodeScanner ? 0.8.sw : 0.91.sw,
  43.       child: OneFormComponent(
  44.         formCtr: searchCtr,
  45.         focusNode: _focusNode,
  46.         isUseOutlineBorder: true,
  47.         maxLines: 1,
  48.         textInputType: TextInputType.text,
  49.         hintText: hintText,
  50.         isUseprefix: true,
  51.         prefixIcon: const Icon(
  52.           Icons.search,
  53.           color: Colors.grey,
  54.         ),
  55.         isUseSuffix: !_isSearchEmpty.value,
  56.         suffixIcon: GestureDetector(
  57.           onTap: () {
  58.             searchCtr.clear();
  59.             _debouncer.call(() {
  60.               onSearchChanged(searchCtr.text);
  61.             });
  62.           },
  63.           child: const Icon(
  64.             Icons.cancel,
  65.             color: Colors.grey,
  66.           ),
  67.         ),
  68.         onChanged: (value) {
  69.           _debouncer.call(() {
  70.             onSearchChanged(value!);
  71.           });
  72.  
  73.           return null;
  74.         },
  75.       ),
  76.     );
  77.   }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement