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

119 lines
3.4 KiB
Dart
Raw 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/history_entry.dart';
2022-01-25 20:44:00 +01:00
import '../../routing/routes.dart';
import '../../services/datetime.dart';
import '../../services/snackbar.dart';
2022-01-23 23:56:26 +01:00
import '../../settings.dart';
import '../common/loading.dart';
2022-01-25 20:44:00 +01:00
import 'kanji_box.dart';
2022-01-19 02:10:05 +01:00
class HistoryEntryItem extends StatelessWidget {
final HistoryEntry entry;
2022-01-19 02:10:05 +01:00
final int objectKey;
final void Function()? onDelete;
final void Function()? onFavourite;
const HistoryEntryItem({
required this.entry,
2022-01-19 02:10:05 +01:00
required this.objectKey,
this.onDelete,
this.onFavourite,
Key? key,
}) : super(key: key);
Widget get _child => (entry.isKanji)
? KanjiBox(kanji: entry.kanji!)
: Text(entry.word!);
2022-01-25 20:44:00 +01:00
void Function() _onTap(context) => entry.isKanji
2022-01-25 20:44:00 +01:00
? () => Navigator.pushNamed(
context,
Routes.kanjiSearch,
arguments: entry.kanji,
2022-01-25 20:44:00 +01:00
)
: () => Navigator.pushNamed(
context,
Routes.search,
arguments: entry.word,
2022-01-25 20:44:00 +01:00
);
MaterialPageRoute get timestamps => MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(title: const Text('Last searched')),
body: FutureBuilder<List<DateTime>>(
future: entry.timestamps,
builder: (context, snapshot) {
// TODO: provide proper error handling
if (snapshot.hasError)
return ErrorWidget(snapshot.error!);
if (!snapshot.hasData) return const LoadingScreen();
return ListView(
children: snapshot.data!
.map(
(ts) => ListTile(
title: Text('${formatDate(ts)} ${formatTime(ts)}'),
),
)
.toList(),
);
},
2022-01-25 20:44:00 +01:00
),
),
);
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: (_) {
showSnackbar(context, 'TODO: implement favourites');
2022-01-25 20:44:00 +01:00
onFavourite?.call();
},
),
SlidableAction(
backgroundColor: Colors.red,
icon: Icons.delete,
onPressed: (_) async {
await entry.delete();
2022-01-25 20:44:00 +01:00
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:44:00 +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:44:00 +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),
child: Text(formatTime(entry.lastTimestamp)),
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:44:00 +01:00
child: _child,
2022-01-23 23:56:26 +01:00
),
2022-01-19 02:10:05 +01:00
],
),
),
),
);
}
}