Skip to content

Commit

Permalink
Search examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Yustas committed Jan 29, 2025
1 parent d101e58 commit 9e83fe6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/i18n/ua.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const String appTitle = "Правопис";
const String searchTitle = "Пошук";
const String searchHint = "літера або слово";
const String searchNotFond = "поки нічого не зайдено";
const String searchNotFond = "нічого не знайдено";
const String searchTry = "Cпробуйте:";
const searchExamples = ["архі", "ї", "апостроф", "чергування"];
47 changes: 40 additions & 7 deletions lib/utils/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import '../models/content.dart';
class ContentSearchDelegate extends SearchDelegate {
ContentSearchDelegate() : super(searchFieldLabel: searchHint);

setQuery(String text) {
query = text;
}

@override
List<Widget>? buildActions(BuildContext context) {
return [
Expand All @@ -36,7 +40,7 @@ class ContentSearchDelegate extends SearchDelegate {

@override
Widget buildResults(BuildContext context) {
return emptySearchResults(context);
return SearchHint(context, setQuery);
}

@override
Expand All @@ -49,8 +53,10 @@ class ContentSearchDelegate extends SearchDelegate {
if (snapshot.hasData) {
if (snapshot.data!.isNotEmpty) {
return SearchList(results: snapshot.data!);
} else if (query.isNotEmpty) {
return EmptySearchResults(context);
} else {
return emptySearchResults(context);
return SearchHint(context, setQuery);
}
} else if (snapshot.hasError) {
return Error(message: 'Error: ${snapshot.error}');
Expand All @@ -67,14 +73,41 @@ void search(BuildContext context, String needle) {
context: context, delegate: ContentSearchDelegate(), query: needle);
}

Widget emptySearchResults(BuildContext context) {
Widget SearchHint(BuildContext context, callback) {
const examples = searchExamples;

return Center(
child: Column(
children:[
const SizedBox(height: 20,),
const Text(searchTry),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
...examples.map((text) => SearchExample(context, text, callback))
],
)
]
),
);
}

Widget SearchExample(BuildContext context, text, onTap) {
return GestureDetector(
child: Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(text),
),
),
onTap: () => onTap(text),
);
}

Widget EmptySearchResults(BuildContext context) {
return const Center(
child: Text(
searchNotFond,
style: TextStyle(
fontSize: Theme.of(context).textTheme.bodyMedium!.fontSize,
color: Colors.red,
),
),
);
}
1 change: 1 addition & 0 deletions lib/widgets/loading.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Loading extends StatelessWidget {
height: 60,
child: CircularProgressIndicator(
color: Theme.of(context).hintColor,
strokeWidth: 1,
),
),
);
Expand Down

0 comments on commit 9e83fe6

Please sign in to comment.