calorimeter/lib/perdate/perdate_pageview.dart

47 lines
1.5 KiB
Dart
Raw Normal View History

2024-10-06 23:44:19 +00:00
import 'dart:developer';
import 'package:calorimeter/perdate/perdate_pageview_controller.dart';
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-06 23:44:19 +00:00
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
2024-10-06 23:44:19 +00:00
final DateTime initialDate;
final PageController pageController;
2024-10-06 23:44:19 +00:00
const PerDatePageView({
required this.initialDate,
required this.pageController,
super.key,
});
@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");
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,
);
});
}
}