2024-12-07 12:29:34 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
2024-12-07 12:39:11 +00:00
|
|
|
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
|
2024-10-06 23:44:19 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
|
2024-10-03 21:21:13 +00:00
|
|
|
import 'package:calorimeter/perdate/perdate_widget.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-10-06 23:44:19 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2024-10-03 21:21:13 +00:00
|
|
|
|
2024-10-06 23:44:19 +00:00
|
|
|
class PerDatePageView extends StatelessWidget {
|
2024-10-03 21:21:13 +00:00
|
|
|
// this is the date for which the PerDate widget will be shown on screen
|
|
|
|
// left of it will be yesterday's PerDate widget
|
|
|
|
// right of it will be tomorrow's PerDate widget
|
2024-10-06 23:44:19 +00:00
|
|
|
final DateTime initialDate;
|
|
|
|
final PageController pageController;
|
2024-10-03 21:21:13 +00:00
|
|
|
|
2024-10-06 23:44:19 +00:00
|
|
|
const PerDatePageView({
|
|
|
|
required this.initialDate,
|
|
|
|
required this.pageController,
|
|
|
|
super.key,
|
|
|
|
});
|
2024-10-03 21:21:13 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-10-06 23:44:19 +00:00
|
|
|
log("PerDatePageView's build()");
|
|
|
|
return PageView.builder(
|
|
|
|
reverse: true,
|
|
|
|
controller: pageController,
|
|
|
|
onPageChanged: (value) {
|
|
|
|
log("onPageChanged() with value $value");
|
|
|
|
|
|
|
|
var diff = value - pageController.initialPage;
|
|
|
|
var newDate = initialDate.subtract(Duration(days: diff));
|
|
|
|
log("newDate = $newDate");
|
2024-10-18 00:44:43 +00:00
|
|
|
|
|
|
|
context
|
|
|
|
.read<PageViewStateProvider>()
|
|
|
|
.addVisitedindexIfNotVisitedByBackButton(value);
|
2024-10-06 23:44:19 +00:00
|
|
|
context.read<PageViewStateProvider>().setDisplayedDate(newDate);
|
|
|
|
},
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
log("itemBuilder() called with index $index");
|
|
|
|
var dateToBuildWidgetFor = initialDate
|
|
|
|
.subtract(Duration(days: index - pageController.initialPage));
|
|
|
|
|
|
|
|
return PerDateWidget(
|
|
|
|
key: ValueKey(dateToBuildWidgetFor.toString()),
|
|
|
|
date: dateToBuildWidgetFor,
|
|
|
|
);
|
|
|
|
});
|
2024-10-03 21:21:13 +00:00
|
|
|
}
|
|
|
|
}
|