Jisho-Study-Tool/lib/screens/search/search_results_page.dart

55 lines
1.5 KiB
Dart
Raw Permalink Normal View History

import 'package:flutter/material.dart';
2021-12-01 23:09:53 +01:00
import '../../components/common/loading.dart';
import '../../components/search/search_result_body.dart';
2022-01-19 02:10:05 +01:00
import '../../models/history/search.dart';
import '../../models/history/word_query.dart';
import '../../services/jisho_api/jisho_search.dart';
2022-01-19 02:10:05 +01:00
// TODO: merge with KanjiResultPage
class SearchResultsPage extends StatefulWidget {
final String searchTerm;
2022-01-19 02:10:05 +01:00
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(),
2021-12-01 23:09:53 +01:00
body: FutureBuilder<JishoAPIResult>(
2022-01-19 02:10:05 +01:00
future: fetchJishoResults(widget.searchTerm),
builder: (context, snapshot) {
2021-12-04 05:13:13 +01:00
if (!snapshot.hasData) return const LoadingScreen();
2021-12-01 23:09:53 +01:00
if (snapshot.hasError || snapshot.data!.data == null)
return ErrorWidget(snapshot.error!);
2021-12-01 23:09:53 +01:00
if (!addedToDatabase) {
2021-12-04 05:13:13 +01:00
Search.store.add(
GetIt.instance.get<Database>(),
Search.fromWordQuery(
timestamp: DateTime.now(),
2022-01-19 02:10:05 +01:00
wordQuery: WordQuery(query: widget.searchTerm),
2021-12-04 05:13:13 +01:00
).toJson(),
);
2021-12-01 23:09:53 +01:00
addedToDatabase = true;
}
return SearchResultsBody(
2021-12-01 23:09:53 +01:00
results: snapshot.data!.data!,
);
},
),
);
}
}