1+ import 'dart:async' ;
2+
13import 'package:flowy_infra/size.dart' ;
24import 'package:flutter/material.dart' ;
35import 'package:flutter/services.dart' ;
@@ -13,6 +15,7 @@ class FlowyTextField extends StatefulWidget {
1315 final int ? maxLength;
1416 final TextEditingController ? controller;
1517 final bool autoClearWhenDone;
18+ final Duration ? debounceDuration;
1619
1720 const FlowyTextField ({
1821 this .hintText = "" ,
@@ -24,6 +27,7 @@ class FlowyTextField extends StatefulWidget {
2427 this .maxLength,
2528 this .controller,
2629 this .autoClearWhenDone = false ,
30+ this .debounceDuration,
2731 Key ? key,
2832 }) : super (key: key);
2933
@@ -34,6 +38,7 @@ class FlowyTextField extends StatefulWidget {
3438class FlowyTextFieldState extends State <FlowyTextField > {
3539 late FocusNode focusNode;
3640 late TextEditingController controller;
41+ Timer ? _debounceOnChanged;
3742
3843 @override
3944 void initState () {
@@ -54,22 +59,40 @@ class FlowyTextFieldState extends State<FlowyTextField> {
5459 super .initState ();
5560 }
5661
62+ void _debounceOnChangedText (Duration duration, String text) {
63+ _debounceOnChanged? .cancel ();
64+ _debounceOnChanged = Timer (duration, () async {
65+ if (mounted) {
66+ _onChanged (text);
67+ }
68+ });
69+ }
70+
71+ void _onChanged (String text) {
72+ widget.onChanged? .call (text);
73+ setState (() {});
74+ }
75+
76+ void _onSubmitted (String text) {
77+ widget.onSubmitted? .call (text);
78+ if (widget.autoClearWhenDone) {
79+ controller.text = "" ;
80+ }
81+ }
82+
5783 @override
5884 Widget build (BuildContext context) {
5985 return TextField (
6086 controller: controller,
6187 focusNode: focusNode,
6288 onChanged: (text) {
63- widget.onChanged? .call (text);
64- setState (() {});
65- },
66- onSubmitted: (text) {
67- widget.onSubmitted? .call (text);
68-
69- if (widget.autoClearWhenDone) {
70- controller.text = "" ;
89+ if (widget.debounceDuration != null ) {
90+ _debounceOnChangedText (widget.debounceDuration! , text);
91+ } else {
92+ _onChanged (text);
7193 }
7294 },
95+ onSubmitted: (text) => _onSubmitted (text),
7396 maxLines: 1 ,
7497 maxLength: widget.maxLength,
7598 maxLengthEnforcement: MaxLengthEnforcement .truncateAfterCompositionEnds,
0 commit comments