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

59 lines
1.3 KiB
Dart
Raw Permalink Normal View History

2021-08-03 22:02:42 +02:00
import 'package:flutter/material.dart';
2021-12-01 23:09:53 +01:00
2022-01-19 02:10:05 +01:00
import '../../bloc/theme/theme_bloc.dart';
2021-08-03 22:02:42 +02:00
class DateDivider extends StatelessWidget {
final String? text;
final DateTime? date;
2021-08-08 23:16:54 +02:00
const DateDivider({
this.text,
this.date,
Key? key,
2022-01-19 02:10:05 +01:00
}) : assert((text == null) ^ (date == null)),
super(key: key);
2021-08-03 22:02:42 +02:00
String getHumanReadableDate(DateTime date) {
2022-01-19 02:10:05 +01:00
const List<String> monthTable = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
2021-08-03 22:02:42 +02:00
2021-08-08 23:16:54 +02:00
final int day = date.day;
2022-01-19 02:10:05 +01:00
final String month = monthTable[date.month - 1];
2021-08-08 23:16:54 +02:00
final int year = date.year;
2021-12-01 23:09:53 +01:00
return '$day. $month $year';
2021-08-03 22:02:42 +02:00
}
@override
Widget build(BuildContext context) {
2022-01-19 02:10:05 +01:00
final Widget header =
(text != null) ? Text(text!) : Text(getHumanReadableDate(date!));
2021-08-03 22:02:42 +02:00
2021-08-08 23:16:54 +02:00
final ColorSet _menuColors =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyNormal;
2021-08-03 22:02:42 +02:00
return Container(
2021-08-08 23:16:54 +02:00
decoration: BoxDecoration(color: _menuColors.background),
2021-12-01 23:09:53 +01:00
padding: const EdgeInsets.symmetric(
2021-08-03 22:02:42 +02:00
vertical: 5,
horizontal: 10,
),
2021-12-01 23:09:53 +01:00
child: DefaultTextStyle.merge(
child: header,
style: TextStyle(color: _menuColors.foreground),
),
2021-08-03 22:02:42 +02:00
);
}
}