Bloc: Call FocusScope.of(context).requestFocus(FocusNode()),entry the BlocWidgetBuilder method.

Created on 8 Aug 2019  路  1Comment  路  Source: felangel/bloc

I add FocusScope.of(context).requestFocus(FocusNode()) in root widget like this to hide keyboard or let all textfield lose focuse.

Scaffold(
      appBar: CustomAppBar(),
      body: Builder(builder: (context) {

        this.ctx = context;
        return GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: xxx,
    );

But when this method called, the BlocWidgetBuilder is triggered.

BlocBuilder(
      bloc: actionBloc,
      builder: (_, ServiceActionState state) {

       ///coming to this method

        return widget;
      }
    )

how can I prevent this behavior from happening?

question

Most helpful comment

Hi @longdw 馃憢
Thanks for opening an issue!

Regarding your question, BlocBuilder is triggered either by state changes in your bloc or by Flutter itself. Things like rotating the device, focusing on input fields, showing/hiding the keyboard, etc... naturally trigger widget's to re-render themselves. This is working as expected and you should write your widgets expecting that the build method can be called many times. As a result, your build should be a pure function with no side-effects. If you want a piece of code to execute a single time per state change in a bloc, then you should use BlocListener instead of BlocBuilder.

You can check out the snackbar recipe for more information on using BlocListener vs BlocBuilder.

Hope that helps! 馃憤

>All comments

Hi @longdw 馃憢
Thanks for opening an issue!

Regarding your question, BlocBuilder is triggered either by state changes in your bloc or by Flutter itself. Things like rotating the device, focusing on input fields, showing/hiding the keyboard, etc... naturally trigger widget's to re-render themselves. This is working as expected and you should write your widgets expecting that the build method can be called many times. As a result, your build should be a pure function with no side-effects. If you want a piece of code to execute a single time per state change in a bloc, then you should use BlocListener instead of BlocBuilder.

You can check out the snackbar recipe for more information on using BlocListener vs BlocBuilder.

Hope that helps! 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shawnchan2014 picture shawnchan2014  路  3Comments

hivesey picture hivesey  路  3Comments

komapeb picture komapeb  路  3Comments

nerder picture nerder  路  3Comments

krusek picture krusek  路  3Comments