Reputation: 2872
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:
TextFormField
initialValue
to pass into the widget the text from the bloc. Doesn't rebuild when the value changes.Key
. Text is updated but focus is lost.I haven't tried managing the TextEditingController in the bloc. It seems like not the right thing to do.
Upvotes: 2
Views: 1998
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
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:
Upvotes: 1