4

I have TextFormField, and want do same actions when user stop typing in textfield. Now I am using onchange function, but I want detect when user stop typing.

7
  • and how do you define "stop typing"? if no key was entered after 300 ms, does it mean "stop typing"? or user stopped for a while and wants to type more? Commented Mar 18, 2020 at 13:33
  • Yes, of course. Commented Mar 18, 2020 at 13:35
  • I can use timer in onchange function, and if user not type while timer != 0 then will be do my action.. Commented Mar 18, 2020 at 13:39
  • do you want to perform some "search as you type" functionality ? if so use rx-dart package and its debounce method Commented Mar 18, 2020 at 13:40
  • I tried use stream_transform package , but not found debounce method Commented Mar 18, 2020 at 13:42

2 Answers 2

9

If you want to achieve debounce on textfield for searching, then here you go.

  final _searchQueryController = new TextEditingController();
  Timer _debounce;
  String query = "";
  int _debouncetime = 500;

  @override
  void initState() {
    _searchQueryController.addListener(_onSearchChanged);
    super.initState();
  }

  @override
  void dispose() {
    _searchQueryController.removeListener(_onSearchChanged);
    _searchQueryController.dispose();
    super.dispose();
  }

  _onSearchChanged() {
    if (_debounce?.isActive ?? false) _debounce.cancel();
    _debounce = Timer(Duration(milliseconds: _debouncetime), () {
      if (_searchQueryController.text != "") {
        ///here you perform your search
        performSearch(_searchQueryController.text);
      }
    });
  }
//your textfield

TextField(controller: _searchQueryController,
                    autofocus: true,
                    decoration: InputDecoration(
                      hintText: " Search...",
                      border: InputBorder.none,
                    ),
                    style: TextStyle(fontSize: 14.0),
                  )
Sign up to request clarification or add additional context in comments.

1 Comment

he is using debounce() for search
1

You can do it with flutter_hooks as follows:

class DebounceTextField extends HookWidget {
  ///
  const DebounceTextField({
    Key? key,
    required this.padding,
    required this.onAnswer,
    required this.child,
    this.initialText,
    this.debounceTime,
  }) : super(key: key);

  ///
  final EdgeInsets padding;

  ///
  final String? initialText;

  ///
  final OnAnswer onAnswer;

  ///
  final TextFormField child;

  ///
  final int? debounceTime;

  @override
  Widget build(BuildContext context) {
    final TextEditingController textController =
        useTextEditingController(text: initialText);

    useEffect(
      () {
        Timer? timer;
        void listener() {
          timer?.cancel();
          timer = Timer(
            Duration(milliseconds: debounceTime ?? 500),
            () => onAnswer(textController.text),
          );
        }

        textController.addListener(listener);
        return () {
          timer?.cancel();
          textController.removeListener(listener);
        };
      },
      <TextEditingController>[textController],
    );

    // child.controller = textController;

    return Padding(
      padding: padding,
      child: TextFormField(
        controller: textController,
        validator: _shortAnswerValidator,
        decoration: const InputDecoration(
          hintText: "Cevabı buraya yazınız...",
        ),
      ),
    );
  }
}

We got the inspiration for this one here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.