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?
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! 馃憤
Most helpful comment
Hi @longdw 馃憢
Thanks for opening an issue!
Regarding your question,
BlocBuilderis 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 thebuildmethod can be called many times. As a result, yourbuildshould 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 useBlocListenerinstead ofBlocBuilder.You can check out the snackbar recipe for more information on using
BlocListenervsBlocBuilder.Hope that helps! 馃憤