import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:sonnat/core/extensions/context_extension.dart'; import 'package:sonnat/core/extensions/string_extension.dart'; import 'package:sonnat/core/language/translator.dart'; import 'package:sonnat/core/utils/app_constants.dart'; class SearchWidget extends StatefulWidget { final Function(String query) search; const SearchWidget({super.key, required this.search}); @override State createState() => _SearchWidgetState(); } class _SearchWidgetState extends State { final TextEditingController _controller = TextEditingController(); @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return SizedBox( height: 49 * context.height / AppConstants.instance.appHeight, child: TextFormField( autofocus: false, maxLength: 300, controller: _controller, onChanged: (value) { Debounce.debounce('search_query', const Duration(milliseconds: 1000), () { widget.search(_controller.text.trim()); }); }, maxLines: 10, minLines: 4, style: const TextStyle( color: Color(0xff8D95AB), fontSize: 10, ), decoration: InputDecoration( fillColor: const Color(0xffF4F4F8), contentPadding: const EdgeInsets.symmetric( vertical: 15, horizontal: 18, ), hintText: Translator.translate('search'), filled: true, counterText: '', hintStyle: const TextStyle( color: Color(0xff8D95AB), fontSize: 10, ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(19), borderSide: const BorderSide(color: Colors.transparent), ), suffixIcon: GestureDetector( onTap: () { _controller.text = ''; widget.search.call(_controller.text.trim()); }, child: Padding( padding: const EdgeInsetsDirectional.only(end: 3, bottom: 4, top: 4), child: SvgPicture.asset('ic_clear'.svgPath), ), ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(19), borderSide: const BorderSide(color: Colors.transparent), ), ), ), ); } } class Debounce { static final Map _timers = {}; static void debounce(String tag, Duration duration, Function onExecute) { if (duration == Duration.zero) { _timers[tag]?.cancel(); _timers.remove(tag); onExecute(); } else { _timers[tag]?.cancel(); _timers[tag] = Timer(duration, () { _timers[tag]?.cancel(); _timers.remove(tag); onExecute(); }); } } static void cancel(String tag) { _timers[tag]?.cancel(); _timers.remove(tag); } }