calorimeter/lib/perdate/perdate_pageview.dart
Marco cb18e1d1f0 Introduce PageView with manual switching of dates
1. Show PerDate widgets inside of an PageView
2. Introduce GoRouter so we can intercept back button taps with
   BackButtonListener
3. Implement rudimentary navigation
4. Fix bug that still showed a spinner event when the barcode was not
   found.
2024-10-06 02:20:08 +02:00

77 lines
2.2 KiB
Dart

import 'package:calorimeter/perdate/perdate_widget.dart';
import 'package:flutter/material.dart';
class PerDatePageview extends StatefulWidget {
// 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 initalDate;
const PerDatePageview({required this.initalDate, super.key});
@override
State<PerDatePageview> createState() => _PerDatePageviewState();
}
class _PerDatePageviewState extends State<PerDatePageview> {
late PageController pageController;
late DateTime displayedDate;
late List<int> visitedIndexes = [];
final int initialOffset = 36500000;
//TODO: that is just ugly
bool backButtonWasPressed = false;
@override
void initState() {
super.initState();
pageController = PageController(initialPage: initialOffset);
displayedDate = widget.initalDate;
visitedIndexes.add(initialOffset);
}
@override
Widget build(BuildContext context) {
return BackButtonListener(
onBackButtonPressed: () async {
if (visitedIndexes.length == 1) {
return false;
}
visitedIndexes.removeLast();
backButtonWasPressed = true;
pageController.jumpToPage(visitedIndexes.last);
return true;
},
child: PageView.builder(
reverse: true,
controller: pageController,
onPageChanged: (value) {
if (backButtonWasPressed) {
backButtonWasPressed = false;
return;
}
visitedIndexes.add(value);
},
itemBuilder: (context, index) {
var dateToBuildWidgetFor =
displayedDate.subtract(Duration(days: index - initialOffset));
return PerDateWidget(
key: ValueKey(dateToBuildWidgetFor.toString()),
date: dateToBuildWidgetFor,
onDateSelected: (dateSelected) {
if (dateSelected == null) return;
var diff = dateSelected.difference(dateToBuildWidgetFor);
var newIndex = index - diff.inDays;
pageController.jumpToPage(newIndex);
});
}),
);
}
}