diff --git a/lib/src/blocs/new_task_bloc.dart b/lib/src/blocs/new_task_bloc.dart index 604d2ab..4fd2959 100644 --- a/lib/src/blocs/new_task_bloc.dart +++ b/lib/src/blocs/new_task_bloc.dart @@ -13,12 +13,18 @@ class NewTaskBloc { final AuthService _auth = authService; final FirestoreProvider _firestore = firestoreProvider; final _user = BehaviorSubject(); + final _eventName = BehaviorSubject(); String text = ''; TaskPriority priority = TaskPriority.high; String event = ''; + //Stream getters. Observable get userModelStream => _user.stream; + Observable 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(); + } } diff --git a/lib/src/screens/new_task_screen.dart b/lib/src/screens/new_task_screen.dart index 961ee0f..7e4baf6 100644 --- a/lib/src/screens/new_task_screen.dart +++ b/lib/src/screens/new_task_screen.dart @@ -17,7 +17,6 @@ class NewTaskScreen extends StatefulWidget { class _NewTaskScreenState extends State { final NewTaskBloc bloc = NewTaskBloc(); - String dropdownValue; Widget build(BuildContext context) { return Scaffold( @@ -51,7 +50,7 @@ class _NewTaskScreenState extends State { SizedBox( height: 15, ), - buildPrioritySelectorSection(bloc), + buildPrioritySelectorSection(), SizedBox( height: 20, ), @@ -75,8 +74,6 @@ class _NewTaskScreenState extends State { ); } - // TODO: Refactor to avoid using the dropdownValue property and instead use - // a stream from the bloc. Widget buildDropdownSection(List events) { return Row( children: [ @@ -87,27 +84,32 @@ class _NewTaskScreenState extends State { 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 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: [ Text( @@ -125,15 +127,13 @@ class _NewTaskScreenState extends State { ); } - 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(); + } }