calorimeter/lib/perdate/perdate_pageview.dart
2024-12-07 13:39:11 +01:00

53 lines
1.7 KiB
Dart

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
import 'dart:developer';
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
import 'package:calorimeter/perdate/perdate_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class PerDatePageView extends StatelessWidget {
// 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
final DateTime initialDate;
final PageController pageController;
const PerDatePageView({
required this.initialDate,
required this.pageController,
super.key,
});
@override
Widget build(BuildContext context) {
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");
context
.read<PageViewStateProvider>()
.addVisitedindexIfNotVisitedByBackButton(value);
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,
);
});
}
}