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

59 lines
1.4 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) => BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final colors = state.theme.menuGreyNormal;
return Container(
decoration: BoxDecoration(color: colors.background),
padding: const EdgeInsets.symmetric(
vertical: 5,
horizontal: 10,
),
child: DefaultTextStyle.merge(
child: (text != null)
? Text(text!)
: Text(getHumanReadableDate(date!)),
style: TextStyle(color: colors.foreground),
),
);
},
);
2021-08-03 22:02:42 +02:00
}