Split NoteSearchDelegate into its own file

This commit is contained in:
Vishesh Handa
2020-01-31 16:12:55 +01:00
parent 23c6192162
commit 51e42780d6
2 changed files with 80 additions and 75 deletions

View File

@ -1,14 +1,13 @@
import 'package:flutter/material.dart';
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/notes_folder.dart';
import 'package:gitjournal/screens/note_editor.dart';
import 'package:gitjournal/state_container.dart';
import 'package:gitjournal/widgets/app_drawer.dart';
import 'package:gitjournal/widgets/app_bar_menu_button.dart';
import 'package:gitjournal/widgets/journal_list.dart';
import 'package:gitjournal/widgets/note_search_delegate.dart';
import 'package:gitjournal/widgets/sync_button.dart';
import 'package:gitjournal/themes.dart';
class JournalListingScreen extends StatelessWidget {
final NotesFolder notesFolder;
@ -54,7 +53,7 @@ class JournalListingScreen extends StatelessWidget {
onPressed: () {
showSearch(
context: context,
delegate: NoteSearch(allNotes),
delegate: NoteSearchDelegate(allNotes),
);
},
),
@ -82,75 +81,3 @@ class JournalListingScreen extends StatelessWidget {
Navigator.of(context).push(route);
}
}
class NoteSearch extends SearchDelegate<Note> {
final List<Note> notes;
NoteSearch(this.notes);
// Workaround because of https://github.com/flutter/flutter/issues/32180
@override
ThemeData appBarTheme(BuildContext context) {
var theme = Theme.of(context);
if (theme.brightness == Brightness.light) {
return theme;
}
return theme.copyWith(
primaryColor: Themes.dark.primaryColor,
);
}
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.close),
onPressed: () {
query = '';
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return buildJournalList(context, query);
}
@override
Widget buildSuggestions(BuildContext context) {
return buildJournalList(context, query);
}
JournalList buildJournalList(BuildContext context, String query) {
// TODO: This should be made far more efficient
var q = query.toLowerCase();
var filteredNotes = notes.where((note) {
return note.body.toLowerCase().contains(q);
}).toList();
Widget journalList = JournalList(
notes: filteredNotes,
noteSelectedFunction: (noteIndex) {
var note = filteredNotes[noteIndex];
var route = MaterialPageRoute(
builder: (context) => NoteEditor.fromNote(note),
);
Navigator.of(context).push(route);
},
emptyText: "No Search Results Found",
);
return journalList;
}
}

View File

@ -0,0 +1,78 @@
import 'package:flutter/material.dart';
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/screens/note_editor.dart';
import 'package:gitjournal/themes.dart';
import 'package:gitjournal/widgets/journal_list.dart';
class NoteSearchDelegate extends SearchDelegate<Note> {
final List<Note> notes;
NoteSearchDelegate(this.notes);
// Workaround because of https://github.com/flutter/flutter/issues/32180
@override
ThemeData appBarTheme(BuildContext context) {
var theme = Theme.of(context);
if (theme.brightness == Brightness.light) {
return theme;
}
return theme.copyWith(
primaryColor: Themes.dark.primaryColor,
);
}
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.close),
onPressed: () {
query = '';
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return buildJournalList(context, query);
}
@override
Widget buildSuggestions(BuildContext context) {
return buildJournalList(context, query);
}
JournalList buildJournalList(BuildContext context, String query) {
// TODO: This should be made far more efficient
var q = query.toLowerCase();
var filteredNotes = notes.where((note) {
return note.body.toLowerCase().contains(q);
}).toList();
Widget journalList = JournalList(
notes: filteredNotes,
noteSelectedFunction: (noteIndex) {
var note = filteredNotes[noteIndex];
var route = MaterialPageRoute(
builder: (context) => NoteEditor.fromNote(note),
);
Navigator.of(context).push(route);
},
emptyText: "No Search Results Found",
);
return journalList;
}
}