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

50 lines
1.5 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-12-04 05:13:13 +01:00
import 'package:get_it/get_it.dart';
import 'package:sembast/sembast.dart';
2021-12-01 23:09:53 +01:00
import '../../../models/history/search.dart';
import '../../../models/history/word_query.dart';
import '../../../services/jisho_api/jisho_search.dart';
import '../../components/common/loading.dart';
import '../../components/search/search_result_body.dart';
class SearchResultsPage extends StatelessWidget {
final String searchTerm;
final Future<JishoAPIResult> results;
bool addedToDatabase = false;
SearchResultsPage({required this.searchTerm, Key? key})
2021-12-01 23:09:53 +01:00
: results = fetchJishoResults(searchTerm),
super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
2021-12-01 23:09:53 +01:00
body: FutureBuilder<JishoAPIResult>(
future: results,
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(),
wordQuery: WordQuery(query: searchTerm),
).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!,
);
},
),
);
}
}