Added the search functionality to the main screen
This commit is contained in:
parent
006261698d
commit
d59e7368c5
3 changed files with 46 additions and 3 deletions
|
|
@ -25,10 +25,14 @@ class HomeBloc {
|
||||||
/// A subject of list of task model.
|
/// A subject of list of task model.
|
||||||
final _tasks = BehaviorSubject<List<TaskModel>>();
|
final _tasks = BehaviorSubject<List<TaskModel>>();
|
||||||
|
|
||||||
|
/// Current text from the search box.
|
||||||
|
String _searchBoxText = '';
|
||||||
|
|
||||||
// Stream getters.
|
// Stream getters.
|
||||||
/// An observalbe of the taks of a user.
|
/// An observalbe of the taks of a user.
|
||||||
Observable<List<TaskModel>> get userTasks =>
|
Observable<List<TaskModel>> get userTasks => _tasks.stream
|
||||||
_tasks.stream.transform(prioritySortTransformer());
|
.transform(prioritySortTransformer())
|
||||||
|
.transform(searchBoxTransformer());
|
||||||
|
|
||||||
/// An observable of the current logged in user.
|
/// An observable of the current logged in user.
|
||||||
Observable<FirebaseUser> get userStream => _auth.userStream;
|
Observable<FirebaseUser> 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<TaskModel>, List<TaskModel>> 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.
|
/// Fetches the tasks for the current user.
|
||||||
Future<void> fetchTasks() async {
|
Future<void> fetchTasks() async {
|
||||||
final user = await _auth.currentUser;
|
final user = await _auth.currentUser;
|
||||||
|
|
@ -74,6 +104,11 @@ class HomeBloc {
|
||||||
_taskService.setTask(task);
|
_taskService.setTask(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates the serach box text.
|
||||||
|
void updateSearchBoxText(String newText) {
|
||||||
|
_searchBoxText = newText;
|
||||||
|
}
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_tasks.close();
|
_tasks.close();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
color: Theme.of(context).cardColor,
|
color: Theme.of(context).cardColor,
|
||||||
),
|
),
|
||||||
SearchBox(
|
SearchBox(
|
||||||
|
onChanged: bloc.updateSearchBoxText,
|
||||||
height: 50.0,
|
height: 50.0,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,13 @@ class SearchBox extends StatelessWidget {
|
||||||
/// Height of the sarch box.
|
/// Height of the sarch box.
|
||||||
final double height;
|
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) {
|
Widget build(BuildContext context) {
|
||||||
return Row(
|
return Row(
|
||||||
|
|
@ -41,6 +47,7 @@ class SearchBox extends StatelessWidget {
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
|
onChanged: onChanged,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
border: InputBorder.none,
|
border: InputBorder.none,
|
||||||
hintText: 'Search...',
|
hintText: 'Search...',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue