From 5c47eeac59efe3bbbcd5b5e09b57564c008a8da5 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Wed, 25 Sep 2019 17:00:06 +0200 Subject: [PATCH] Implement the most basic search functionality --- lib/screens/home_screen.dart | 83 ++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 4b5b8112..c5d891b1 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,5 +1,6 @@ import 'package:badges/badges.dart'; import 'package:flutter/material.dart'; +import 'package:journal/note.dart'; import 'package:journal/apis/git.dart'; import 'package:journal/screens/note_editor.dart'; import 'package:journal/screens/note_viewer.dart'; @@ -26,9 +27,9 @@ class HomeScreen extends StatelessWidget { noteSelectedFunction: (noteIndex) { var route = MaterialPageRoute( builder: (context) => NoteBrowsingScreen( - notes: appState.notes, - noteIndex: noteIndex, - ), + notes: appState.notes, + noteIndex: noteIndex, + ), ); Navigator.of(context).push(route); }, @@ -50,6 +51,17 @@ class HomeScreen extends StatelessWidget { appBar: AppBar( title: Text('GitJournal'), leading: appBarMenuButton, + actions: [ + IconButton( + icon: Icon(Icons.search), + onPressed: () { + showSearch( + context: context, + delegate: NoteSearch(), + ); + }, + ) + ], ), floatingActionButton: createButton, body: Center( @@ -74,3 +86,68 @@ class HomeScreen extends StatelessWidget { Navigator.of(context).push(route); } } + +class NoteSearch extends SearchDelegate { + @override + List buildActions(BuildContext context) { + return [ + IconButton( + icon: Icon(Icons.close), + onPressed: () { + query = ''; + }, + ), + ]; + } + + @override + Widget buildLeading(BuildContext context) { + return IconButton( + icon: Icon(Icons.arrow_back), + onPressed: () { + close(context, null); + }, + ); + } + + @override + Widget buildResults(BuildContext context) { + return buildJournalList(context, query); + } + + @override + Widget buildSuggestions(BuildContext context) { + if (query.isEmpty) { + return Container(); + } + return buildJournalList(context, query); + } + + JournalList buildJournalList(BuildContext context, String query) { + final container = StateContainer.of(context); + final appState = container.appState; + + // TODO: This should be made far more efficient + List filteredNotes = []; + var q = query.toLowerCase(); + appState.notes.forEach((note) { + if (note.body.toLowerCase().contains(query)) { + filteredNotes.add(note); + } + }); + + Widget journalList = JournalList( + notes: filteredNotes, + noteSelectedFunction: (noteIndex) { + var route = MaterialPageRoute( + builder: (context) => NoteBrowsingScreen( + notes: filteredNotes, + noteIndex: noteIndex, + ), + ); + Navigator.of(context).push(route); + }, + ); + return journalList; + } +}