ValueListenableBuilder does not work in the required manner

Viewed 35

This is my code:

@override
  Widget build(BuildContext context) {
    print('It is being rebulid');
    print(_value.value);
    return Scaffold(
        backgroundColor: ColorManager.white,
        body: ValueListenableBuilder<int>(
          valueListenable: _value,
          builder: (context, value, child) {
            return StreamBuilder<FlowState>(
              stream: _forgotEmailViewModel.outputState,
              builder: (context, snapshot) {
                print(snapshot.data);
                return snapshot.data?.getScreenWidget(
                        context, _getContentWidget(), () {}) ??
                    _getContentWidget();
              },
            );
          },
        ));
  }

As I can understand about ValueListenableBuilder, it only builds the method inside it when the value changes. In my application there is a textfield. As the keyboard pops up or closes, the build method is called and I get the two print statements in the console. What I don't understand is that since I do not change the value then why does the StreamBuilder is also rebuild (because I also got the value of my snapshot in the console).

I do not want my stream builder to rebuild every time the keyboard is opened or closed.

Can someone please help me here?

1 Answers

Your understanding is right ValueListenableBuilder will only rebuild when there is a change in the value. But, when you tap on the text field it will show keyboard. Now your UI will need to adapt to it. That's why your build method is being called.

Related