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() .addVisitedindexIfNotVisitedByBackButton(value); context.read().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, ); }); } }