1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-22 13:57:29 +01:00
Jisho-Study-Tool/lib/screens/search/search_results_page.dart
2022-01-19 02:10:05 +01:00

55 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
import '../../components/common/loading.dart';
import '../../components/search/search_result_body.dart';
import '../../models/history/search.dart';
import '../../models/history/word_query.dart';
import '../../services/jisho_api/jisho_search.dart';
// TODO: merge with KanjiResultPage
class SearchResultsPage extends StatefulWidget {
final String searchTerm;
const SearchResultsPage({
Key? key,
required this.searchTerm,
}) : super(key: key);
@override
_SearchResultsPageState createState() => _SearchResultsPageState();
}
class _SearchResultsPageState extends State<SearchResultsPage> {
bool addedToDatabase = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder<JishoAPIResult>(
future: fetchJishoResults(widget.searchTerm),
builder: (context, snapshot) {
if (!snapshot.hasData) return const LoadingScreen();
if (snapshot.hasError || snapshot.data!.data == null)
return ErrorWidget(snapshot.error!);
if (!addedToDatabase) {
Search.store.add(
GetIt.instance.get<Database>(),
Search.fromWordQuery(
timestamp: DateTime.now(),
wordQuery: WordQuery(query: widget.searchTerm),
).toJson(),
);
addedToDatabase = true;
}
return SearchResultsBody(
results: snapshot.data!.data!,
);
},
),
);
}
}