mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 18:38:36 +08:00
Split NoteSearchDelegate into its own file
This commit is contained in:
@ -1,14 +1,13 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:gitjournal/core/note.dart';
|
|
||||||
import 'package:gitjournal/core/notes_folder.dart';
|
import 'package:gitjournal/core/notes_folder.dart';
|
||||||
import 'package:gitjournal/screens/note_editor.dart';
|
import 'package:gitjournal/screens/note_editor.dart';
|
||||||
import 'package:gitjournal/state_container.dart';
|
import 'package:gitjournal/state_container.dart';
|
||||||
import 'package:gitjournal/widgets/app_drawer.dart';
|
import 'package:gitjournal/widgets/app_drawer.dart';
|
||||||
import 'package:gitjournal/widgets/app_bar_menu_button.dart';
|
import 'package:gitjournal/widgets/app_bar_menu_button.dart';
|
||||||
import 'package:gitjournal/widgets/journal_list.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/widgets/sync_button.dart';
|
||||||
import 'package:gitjournal/themes.dart';
|
|
||||||
|
|
||||||
class JournalListingScreen extends StatelessWidget {
|
class JournalListingScreen extends StatelessWidget {
|
||||||
final NotesFolder notesFolder;
|
final NotesFolder notesFolder;
|
||||||
@ -54,7 +53,7 @@ class JournalListingScreen extends StatelessWidget {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
showSearch(
|
showSearch(
|
||||||
context: context,
|
context: context,
|
||||||
delegate: NoteSearch(allNotes),
|
delegate: NoteSearchDelegate(allNotes),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -82,75 +81,3 @@ class JournalListingScreen extends StatelessWidget {
|
|||||||
Navigator.of(context).push(route);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
78
lib/widgets/note_search_delegate.dart
Normal file
78
lib/widgets/note_search_delegate.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user