Lucas
Lucas

Reputation: 2872

clearing text field when using flutter_bloc

I have a simple chat page with a ChatBloc and a ReplyBox widget, a message box with a send button.

The I would like the ReplyBox to be reusable, so not to have specific knowledge of the ChatBloc. However, I'd like to be able to control the text in the ReplyBox from the bloc. This is proving very difficult because the text field expects to use a TextEditingController.

What I've tried:

I haven't tried managing the TextEditingController in the bloc. It seems like not the right thing to do.

Upvotes: 2

Views: 1998

Answers (2)

Shahzad Umar Baig
Shahzad Umar Baig

Reputation: 172

I have asked a similar question on the repository of BLoC library. Quoting the answer provided to me by Felix Angelov, the creator itself:

I don't recommend that. The proper way imo is to maintain a TextEditingController and call clear when you want to clear the input.

You can read the answer here plus you can also check the entire question.

Upvotes: 1

Chop TRAN
Chop TRAN

Reputation: 529

I see the 3rd method is promising with StatefullWdiget and TextEditingController to solve your problem. I would have another bloc for the ReplyBox with events like Reset, Update, etc. In the widget I would connect the state of the ChatBloc to ReplyBloc.

In drawing may be something like this: enter image description here

Upvotes: 1

Related Questions