fooder-app/lib/components/date_picker.dart

155 lines
4.1 KiB
Dart
Raw Permalink Normal View History

2024-03-28 16:25:49 +01:00
import 'package:flutter/material.dart';
class FDateItemWidget extends StatelessWidget {
final DateTime date;
final bool picked;
final Function(DateTime) onDatePicked;
2024-03-30 14:07:10 +01:00
const FDateItemWidget(
{super.key,
required this.date,
required this.onDatePicked,
this.picked = false});
2024-03-28 16:25:49 +01:00
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
var dayOfTheWeekMap = {
1: 'Mon',
2: 'Tue',
3: 'Wed',
4: 'Thu',
5: 'Fri',
6: 'Sat',
7: 'Sun',
};
return GestureDetector(
onTap: () {
onDatePicked(date);
},
child: Container(
2024-03-30 14:07:10 +01:00
width: picked ? 100 : 50,
2024-03-28 16:25:49 +01:00
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(
2024-03-29 16:47:25 +01:00
// color: picked ? colorScheme.onPrimary : Colors.transparent,
color: Colors.transparent,
2024-03-28 16:25:49 +01:00
width: 2,
),
2024-03-30 14:07:10 +01:00
color: picked
2024-04-04 19:03:41 +02:00
? colorScheme.onSurfaceVariant.withOpacity(0.25)
2024-03-30 14:07:10 +01:00
: Colors.transparent,
2024-03-28 16:25:49 +01:00
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
dayOfTheWeekMap[date.weekday]!,
style: TextStyle(
2024-04-04 19:03:41 +02:00
color: colorScheme.onSurfaceVariant,
2024-03-30 14:07:10 +01:00
fontSize: picked ? 24 : 12,
2024-03-28 16:25:49 +01:00
fontWeight: FontWeight.bold,
),
),
Text(
'${date.day}.${date.month}',
style: TextStyle(
2024-04-04 19:03:41 +02:00
color: colorScheme.onSurfaceVariant,
2024-03-30 14:07:10 +01:00
fontSize: picked ? 24 : 12,
2024-03-28 16:25:49 +01:00
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}
class FDatePickerWidget extends StatefulWidget {
final DateTime date;
final Function(DateTime) onDatePicked;
2024-03-30 14:07:10 +01:00
const FDatePickerWidget(
{super.key, required this.date, required this.onDatePicked});
2024-03-28 16:25:49 +01:00
@override
State<FDatePickerWidget> createState() => _FDatePickerWidgetState();
}
class _FDatePickerWidgetState extends State<FDatePickerWidget> {
DateTime date = DateTime.now();
@override
void initState() {
super.initState();
setState(() {
date = widget.date;
});
}
Future<void> onDatePicked(DateTime date) async {
setState(() {
this.date = date;
});
await widget.onDatePicked(date);
}
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var colorScheme = theme.colorScheme;
return SizedBox(
height: 100,
child: Center(
child: ListView(
scrollDirection: Axis.horizontal,
2024-03-29 16:47:25 +01:00
padding: const EdgeInsets.symmetric(horizontal: 20),
2024-03-28 16:25:49 +01:00
shrinkWrap: true,
children: <Widget>[
2024-03-30 14:07:10 +01:00
FDateItemWidget(
date: date.add(const Duration(days: -2)),
onDatePicked: onDatePicked),
FDateItemWidget(
date: date.add(const Duration(days: -1)),
onDatePicked: onDatePicked),
FDateItemWidget(
date: date, onDatePicked: onDatePicked, picked: true),
FDateItemWidget(
date: date.add(const Duration(days: 1)),
onDatePicked: onDatePicked),
SizedBox(
2024-03-29 16:47:25 +01:00
width: 50,
2024-03-28 16:25:49 +01:00
child: IconButton(
icon: Icon(
Icons.calendar_month,
2024-04-04 19:03:41 +02:00
color: colorScheme.onSurfaceVariant,
2024-03-29 16:47:25 +01:00
size: 20,
2024-03-28 16:25:49 +01:00
),
onPressed: () {
2024-03-29 16:47:25 +01:00
// open date picker
showDatePicker(
context: context,
initialDate: date,
2024-03-30 14:07:10 +01:00
firstDate: date.add(const Duration(days: -365)),
lastDate: date.add(const Duration(days: 365)),
2024-03-29 16:47:25 +01:00
).then((value) {
if (value != null) {
onDatePicked(value);
}
});
2024-03-28 16:25:49 +01:00
},
),
),
],
),
),
);
}
}