diff --git a/lib/src/blocs/home_bloc.dart b/lib/src/blocs/home_bloc.dart index c8d8fb2..933bdda 100644 --- a/lib/src/blocs/home_bloc.dart +++ b/lib/src/blocs/home_bloc.dart @@ -25,10 +25,14 @@ class HomeBloc { /// A subject of list of task model. final _tasks = BehaviorSubject>(); + /// Current text from the search box. + String _searchBoxText = ''; + // Stream getters. /// An observalbe of the taks of a user. - Observable> get userTasks => - _tasks.stream.transform(prioritySortTransformer()); + Observable> get userTasks => _tasks.stream + .transform(prioritySortTransformer()) + .transform(searchBoxTransformer()); /// An observable of the current logged in user. Observable get userStream => _auth.userStream; @@ -43,6 +47,32 @@ class HomeBloc { }); } + /// Returns a stream transformer that filters the task with the text from + /// the search box. + StreamTransformer, List> searchBoxTransformer() { + return StreamTransformer.fromHandlers( + handleData: (taskList, sink) { + sink.add( + taskList.where( + (TaskModel task) { + if (_searchBoxText == '') { + return true; + } + // Return true if the text in the search box matches the title + // or the text of the task. + return task.event + .toLowerCase() + .contains(_searchBoxText.toLowerCase()) || + task.text + .toLowerCase() + .contains(_searchBoxText.toLowerCase()); + }, + ).toList(), + ); + }, + ); + } + /// Fetches the tasks for the current user. Future fetchTasks() async { final user = await _auth.currentUser; @@ -74,6 +104,11 @@ class HomeBloc { _taskService.setTask(task); } + /// Updates the serach box text. + void updateSearchBoxText(String newText) { + _searchBoxText = newText; + } + void dispose() { _tasks.close(); } diff --git a/lib/src/screens/home_screen.dart b/lib/src/screens/home_screen.dart index 3fb264a..460a498 100644 --- a/lib/src/screens/home_screen.dart +++ b/lib/src/screens/home_screen.dart @@ -68,6 +68,7 @@ class _HomeScreenState extends State { color: Theme.of(context).cardColor, ), SearchBox( + onChanged: bloc.updateSearchBoxText, height: 50.0, ), ], diff --git a/lib/src/widgets/search-box.dart b/lib/src/widgets/search-box.dart index 3add018..d7e4a8a 100644 --- a/lib/src/widgets/search-box.dart +++ b/lib/src/widgets/search-box.dart @@ -10,7 +10,13 @@ class SearchBox extends StatelessWidget { /// Height of the sarch box. final double height; - SearchBox({@required this.height}) : assert(height >= 50); + /// Function to be called when the text changes. + final Function(String) onChanged; + + SearchBox({ + @required this.height, + @required this.onChanged, + }) : assert(height >= 50); Widget build(BuildContext context) { return Row( @@ -41,6 +47,7 @@ class SearchBox extends StatelessWidget { ), Expanded( child: TextField( + onChanged: onChanged, decoration: InputDecoration( border: InputBorder.none, hintText: 'Search...',