Changed the dropdown in the task screen to listen to a stream for its value instean of using setState

This commit is contained in:
Mariano Uvalle 2019-04-04 02:24:07 -06:00
parent db489e79c6
commit bc51a48b6a
2 changed files with 36 additions and 25 deletions

View file

@ -13,12 +13,18 @@ class NewTaskBloc {
final AuthService _auth = authService;
final FirestoreProvider _firestore = firestoreProvider;
final _user = BehaviorSubject<UserModel>();
final _eventName = BehaviorSubject<String>();
String text = '';
TaskPriority priority = TaskPriority.high;
String event = '';
//Stream getters.
Observable<UserModel> get userModelStream => _user.stream;
Observable<String> get eventName => _eventName.stream;
//Sinks getters.
Function(String) get changeEventName => _eventName.sink.add;
NewTaskBloc() {
setCurrentUser();
@ -52,4 +58,9 @@ class NewTaskBloc {
final userModel = await _firestore.getUser(username: user.email);
_user.add(userModel);
}
void dispose() {
_user.close();
_eventName.close();
}
}

View file

@ -17,7 +17,6 @@ class NewTaskScreen extends StatefulWidget {
class _NewTaskScreenState extends State<NewTaskScreen> {
final NewTaskBloc bloc = NewTaskBloc();
String dropdownValue;
Widget build(BuildContext context) {
return Scaffold(
@ -51,7 +50,7 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
SizedBox(
height: 15,
),
buildPrioritySelectorSection(bloc),
buildPrioritySelectorSection(),
SizedBox(
height: 20,
),
@ -75,8 +74,6 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
);
}
// TODO: Refactor to avoid using the dropdownValue property and instead use
// a stream from the bloc.
Widget buildDropdownSection(List<String> events) {
return Row(
children: <Widget>[
@ -87,27 +84,32 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
Spacer(),
FractionallyScreenSizedBox(
widthFactor: 0.6,
child: CustomDropdownButton(
isExpanded: true,
value: dropdownValue,
onChanged: (String value) => onDropdownChanged(bloc, value),
hint: Text('Event'),
items: events.map((String name) {
return CustomDropdownMenuItem(
value: name,
child: Text(
name,
style: TextStyle(color: Colors.white),
),
child: StreamBuilder(
stream: bloc.eventName,
builder: (BuildContext context, AsyncSnapshot<String> snap) {
return CustomDropdownButton(
isExpanded: true,
value: snap.data,
onChanged: bloc.changeEventName,
hint: Text('Event'),
items: events.map((String name) {
return CustomDropdownMenuItem(
value: name,
child: Text(
name,
style: TextStyle(color: Colors.white),
),
);
}).toList(),
);
}).toList(),
},
),
),
],
);
}
Widget buildPrioritySelectorSection(NewTaskBloc bloc) {
Widget buildPrioritySelectorSection() {
return Row(
children: <Widget>[
Text(
@ -125,15 +127,13 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
);
}
void onDropdownChanged(NewTaskBloc bloc, String newValue) {
bloc.setEvent(newValue);
setState(() {
dropdownValue = newValue;
});
}
void onSubmit(BuildContext context, NewTaskBloc bloc) async {
await bloc.submit();
Navigator.of(context).pop();
}
void dispose() {
bloc.dispose();
super.dispose();
}
}