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

43 lines
1.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-12-01 23:09:53 +01:00
import '../../../bloc/database/database_bloc.dart';
import '../../../models/history/kanji_query.dart';
import '../../../models/history/search.dart';
import '../../../services/jisho_api/kanji_search.dart';
import '../../components/common/loading.dart';
import '../../components/kanji/kanji_result_body.dart';
class KanjiResultPage extends StatelessWidget {
final String kanjiSearchTerm;
bool addedToDatabase = false;
2021-12-01 23:09:53 +01:00
KanjiResultPage({required this.kanjiSearchTerm, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
2021-12-01 23:09:53 +01:00
body: FutureBuilder<KanjiResult>(
future: fetchKanji(kanjiSearchTerm),
builder: ( context, snapshot) {
if (!snapshot.hasData) return LoadingScreen();
if (snapshot.hasError) return ErrorWidget(snapshot.error!);
2021-12-01 23:09:53 +01:00
if (!addedToDatabase) {
(BlocProvider.of<DatabaseBloc>(context).state as DatabaseConnected)
.database
.box<Search>()
.put(Search(timestamp: DateTime.now())
..kanjiQuery.target = KanjiQuery(
2021-12-01 23:09:53 +01:00
kanji: kanjiSearchTerm,
),);
addedToDatabase = true;
}
2021-12-01 23:09:53 +01:00
return KanjiResultBody(result: snapshot.data!);
},
),
);
}
}