Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.0 KiB

1 year ago
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_svg/flutter_svg.dart';
  4. import 'package:sonnat/core/extensions/context_extension.dart';
  5. import 'package:sonnat/core/extensions/string_extension.dart';
  6. import 'package:sonnat/core/language/translator.dart';
  7. import 'package:sonnat/core/utils/app_constants.dart';
  8. class SearchWidget extends StatefulWidget {
  9. final Function(String query) search;
  10. const SearchWidget({super.key, required this.search});
  11. @override
  12. State<SearchWidget> createState() => _SearchWidgetState();
  13. }
  14. class _SearchWidgetState extends State<SearchWidget> {
  15. final TextEditingController _controller = TextEditingController();
  16. @override
  17. void dispose() {
  18. _controller.dispose();
  19. super.dispose();
  20. }
  21. @override
  22. Widget build(BuildContext context) {
  23. return SizedBox(
  24. height: 49 * context.height / AppConstants.instance.appHeight,
  25. child: TextFormField(
  26. autofocus: false,
  27. maxLength: 300,
  28. controller: _controller,
  29. onChanged: (value) {
  30. Debounce.debounce('search_query', const Duration(milliseconds: 1000), () {
  31. widget.search(_controller.text.trim());
  32. });
  33. },
  34. maxLines: 10,
  35. minLines: 4,
  36. style: const TextStyle(
  37. color: Color(0xff8D95AB),
  38. fontSize: 10,
  39. ),
  40. decoration: InputDecoration(
  41. fillColor: const Color(0xffF4F4F8),
  42. contentPadding: const EdgeInsets.symmetric(
  43. vertical: 15,
  44. horizontal: 18,
  45. ),
  46. hintText: Translator.translate('search'),
  47. filled: true,
  48. counterText: '',
  49. hintStyle: const TextStyle(
  50. color: Color(0xff8D95AB),
  51. fontSize: 10,
  52. ),
  53. enabledBorder: OutlineInputBorder(
  54. borderRadius: BorderRadius.circular(19),
  55. borderSide: const BorderSide(color: Colors.transparent),
  56. ),
  57. suffixIcon: GestureDetector(
  58. onTap: () {
  59. _controller.text = '';
  60. widget.search.call(_controller.text.trim());
  61. },
  62. child: Padding(
  63. padding: const EdgeInsetsDirectional.only(end: 3, bottom: 4, top: 4),
  64. child: SvgPicture.asset('ic_clear'.svgPath),
  65. ),
  66. ),
  67. focusedBorder: OutlineInputBorder(
  68. borderRadius: BorderRadius.circular(19),
  69. borderSide: const BorderSide(color: Colors.transparent),
  70. ),
  71. ),
  72. ),
  73. );
  74. }
  75. }
  76. class Debounce {
  77. static final Map<String, Timer> _timers = {};
  78. static void debounce(String tag, Duration duration, Function onExecute) {
  79. if (duration == Duration.zero) {
  80. _timers[tag]?.cancel();
  81. _timers.remove(tag);
  82. onExecute();
  83. } else {
  84. _timers[tag]?.cancel();
  85. _timers[tag] = Timer(duration, () {
  86. _timers[tag]?.cancel();
  87. _timers.remove(tag);
  88. onExecute();
  89. });
  90. }
  91. }
  92. static void cancel(String tag) {
  93. _timers[tag]?.cancel();
  94. _timers.remove(tag);
  95. }
  96. }