Jisho-Study-Tool/lib/components/history/search_item.dart

109 lines
3.0 KiB
Dart
Raw Permalink Normal View History

2022-01-19 02:10:05 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import '../../models/history/search.dart';
2022-01-25 20:41:53 +01:00
import '../../routing/routes.dart';
import '../../services/datetime.dart';
2022-01-23 23:56:26 +01:00
import '../../settings.dart';
2022-01-25 20:41:53 +01:00
import 'kanji_box.dart';
2022-01-19 02:10:05 +01:00
class SearchItem extends StatelessWidget {
2022-01-25 20:41:53 +01:00
final Search search;
// final Widget search;
2022-01-19 02:10:05 +01:00
final int objectKey;
final void Function()? onDelete;
final void Function()? onFavourite;
const SearchItem({
required this.search,
required this.objectKey,
this.onDelete,
this.onFavourite,
Key? key,
}) : super(key: key);
2022-01-25 20:41:53 +01:00
Widget get _child => (search.isKanji)
? KanjiBox(kanji: search.kanjiQuery!.kanji)
: Text(search.wordQuery!.query);
void Function() _onTap(context) => search.isKanji
? () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: search.kanjiQuery!.kanji,
)
: () => Navigator.pushNamed(
context,
Routes.search,
arguments: search.wordQuery!.query,
);
MaterialPageRoute get timestamps => MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(title: const Text('Last searched')),
body: ListView(
children: [
for (final ts in search.timestamps.reversed)
ListTile(title: Text('${formatDate(ts)} ${formatTime(ts)}'))
],
),
),
);
List<SlidableAction> _actions(context) => [
SlidableAction(
backgroundColor: Colors.blue,
icon: Icons.access_time,
onPressed: (_) => Navigator.push(context, timestamps),
),
SlidableAction(
backgroundColor: Colors.yellow,
icon: Icons.star,
onPressed: (_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('TODO: implement favourites')),
);
onFavourite?.call();
},
),
SlidableAction(
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) {
final Database db = GetIt.instance.get<Database>();
Search.store.record(objectKey).delete(db);
onDelete?.call();
},
),
];
2022-01-19 02:10:05 +01:00
@override
Widget build(BuildContext context) {
return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
2022-01-25 20:41:53 +01:00
children: _actions(context),
2022-01-19 02:10:05 +01:00
),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 10),
child: ListTile(
2022-01-25 20:41:53 +01:00
onTap: _onTap(context),
2022-01-19 02:10:05 +01:00
contentPadding: EdgeInsets.zero,
title: Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
2022-01-25 20:41:53 +01:00
child: Text(formatTime(search.timestamp)),
2022-01-19 02:10:05 +01:00
),
2022-01-23 23:56:26 +01:00
DefaultTextStyle.merge(
style: japaneseFont.textStyle,
2022-01-25 20:41:53 +01:00
child: _child,
2022-01-23 23:56:26 +01:00
),
2022-01-19 02:10:05 +01:00
],
),
),
),
);
}
}