flutter_timetable/lib/src/timetable.dart

331 lines
12 KiB
Dart
Raw Normal View History

2022-11-01 09:46:23 +01:00
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
2022-09-01 12:05:37 +02:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:timetable/src/block_service.dart';
import 'package:timetable/src/models/table_theme.dart';
import 'package:timetable/src/models/time_block.dart';
import 'package:timetable/src/widgets/block.dart';
import 'package:timetable/src/widgets/table.dart' as table;
2022-08-24 09:39:36 +02:00
class Timetable extends StatefulWidget {
/// [Timetable] widget that displays a timetable with [TimeBlock]s.
/// The timetable automatically scrolls to the first item.
/// A [TableTheme] can be provided to customize the look of the timetable.
/// [mergeBlocks] and [combineBlocks] can be used to combine blocks
/// and merge columns of blocks when possible.
2022-08-24 11:01:50 +02:00
const Timetable({
this.tableDirection = Axis.vertical,
2022-08-24 12:03:32 +02:00
this.timeBlocks = const [],
2022-11-18 14:22:05 +01:00
this.size,
2022-08-24 11:01:50 +02:00
this.scrollController,
this.scrollPhysics,
2022-08-24 11:01:50 +02:00
this.startHour = 0,
this.endHour = 24,
this.blockDimension = 50,
2022-08-24 13:32:35 +02:00
this.blockColor = const Color(0x80FF0000),
this.hourDimension = 80,
2022-08-24 13:32:35 +02:00
this.theme = const TableTheme(),
this.mergeBlocks = false,
this.combineBlocks = true,
2022-08-24 11:01:50 +02:00
Key? key,
}) : super(key: key);
/// The Axis in which the table is layed out.
final Axis tableDirection;
2022-11-18 14:22:05 +01:00
/// The [Size] of the timetable.
final Size? size;
2022-08-24 11:01:50 +02:00
/// Hour at which the timetable starts.
final int startHour;
/// Hour at which the timetable ends.
final int endHour;
2022-08-24 12:03:32 +02:00
/// The time blocks that will be displayed in the timetable.
final List<TimeBlock> timeBlocks;
/// The dimension in pixels of the block if there is no child
/// for the direction that the table expands in
final double blockDimension;
2022-08-24 12:03:32 +02:00
2022-08-24 13:32:35 +02:00
/// The color of the block if there is no child
final Color blockColor;
/// The dimension in pixels of one hour in the timetable.
final double hourDimension;
2022-08-24 13:32:35 +02:00
/// The theme of the timetable.
final TableTheme theme;
2022-08-24 11:01:50 +02:00
/// The scroll controller to control the scrolling of the timetable.
final ScrollController? scrollController;
2022-08-24 09:39:36 +02:00
/// The scroll physics used for the SinglechildScrollView.
final ScrollPhysics? scrollPhysics;
/// Whether or not to merge blocks in 1 column that fit below eachother.
final bool mergeBlocks;
/// Whether or not to collapse blocks in 1 column if they have the same id.
2022-08-24 17:14:50 +02:00
/// If blocks have the same id and time they will be combined into one block.
final bool combineBlocks;
2022-08-24 09:39:36 +02:00
@override
State<Timetable> createState() => _TimetableState();
}
class _TimetableState extends State<Timetable> {
2022-08-24 11:01:50 +02:00
late ScrollController _scrollController;
@override
void initState() {
super.initState();
_scrollController =
widget.scrollController ?? ScrollController(initialScrollOffset: 0);
2022-09-01 12:05:37 +02:00
if (widget.timeBlocks.isNotEmpty) {
_scrollToFirstBlock();
}
2022-08-24 11:01:50 +02:00
}
@override
void dispose() {
if (widget.scrollController == null) {
_scrollController.dispose();
}
super.dispose();
}
2022-08-24 09:39:36 +02:00
@override
Widget build(BuildContext context) {
2022-08-24 17:14:50 +02:00
List<TimeBlock> blocks;
if (widget.combineBlocks) {
blocks = combineBlocksWithId(widget.timeBlocks);
2022-08-24 17:14:50 +02:00
} else {
blocks = widget.timeBlocks;
}
var linePadding = _calculateTableTextSize().width;
2022-11-18 14:22:05 +01:00
return SizedBox(
width: widget.size?.width,
height: widget.size?.height,
child: SingleChildScrollView(
physics: widget.scrollPhysics ?? const BouncingScrollPhysics(),
controller: _scrollController,
scrollDirection: widget.tableDirection,
child: Stack(
alignment: Alignment.topLeft,
children: [
table.Table(
tableHeight: widget.tableDirection == Axis.horizontal
? _calculateTableHeight()
: 0,
tableDirection: widget.tableDirection,
startHour: widget.startHour,
endHour: widget.endHour,
hourDimension: widget.hourDimension,
tableOffset: _calculateTableStart(widget.tableDirection).width,
theme: widget.theme,
size: widget.size,
2022-08-24 13:32:35 +02:00
),
2022-11-18 14:22:05 +01:00
Container(
margin: EdgeInsets.only(
top: _calculateTableStart(widget.tableDirection).height,
left: _calculateTableStart(widget.tableDirection).width,
),
child: SingleChildScrollView(
physics: widget.scrollPhysics ?? const BouncingScrollPhysics(),
scrollDirection: widget.tableDirection == Axis.horizontal
? Axis.vertical
: Axis.horizontal,
child: (widget.tableDirection == Axis.horizontal)
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
2022-11-18 14:22:05 +01:00
mainAxisAlignment: MainAxisAlignment.start,
children: [
2022-11-18 14:22:05 +01:00
SizedBox(height: widget.theme.tableTextOffset),
if (widget.mergeBlocks || widget.combineBlocks) ...[
for (var orderedBlocks in (widget.mergeBlocks)
? mergeBlocksInColumns(blocks)
: groupBlocksById(blocks)) ...[
Stack(
children: [
for (var block in orderedBlocks) ...[
_showBlock(block),
],
],
),
SizedBox(
2022-11-18 14:22:05 +01:00
height: widget.theme.blockPaddingBetween,
),
],
] else ...[
for (var block in blocks) ...[
2022-11-18 14:22:05 +01:00
_showBlock(block, linePadding: linePadding),
SizedBox(
height: widget.theme.blockPaddingBetween,
),
],
],
2022-11-18 14:22:05 +01:00
// emtpy block at the end
SizedBox(
2022-11-18 14:22:05 +01:00
height: max(
widget.theme.tablePaddingEnd -
widget.theme.blockPaddingBetween,
0,
),
),
],
2022-11-18 14:22:05 +01:00
)
: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.mergeBlocks || widget.combineBlocks) ...[
for (var orderedBlocks in (widget.mergeBlocks)
? mergeBlocksInColumns(blocks)
: groupBlocksById(blocks)) ...[
Stack(
children: [
for (var block in orderedBlocks) ...[
_showBlock(block),
],
],
),
SizedBox(
width: widget.theme.blockPaddingBetween,
),
],
] else ...[
for (var block in blocks) ...[
_showBlock(block),
],
],
SizedBox(
width: max(
widget.theme.tablePaddingEnd -
widget.theme.blockPaddingBetween,
0,
),
height: widget.hourDimension *
(widget.endHour -
widget.startHour +
0.5), // empty halfhour at the end
),
],
),
2022-09-01 12:05:37 +02:00
),
2022-11-18 14:22:05 +01:00
),
2022-08-24 12:03:32 +02:00
),
2022-11-18 14:22:05 +01:00
],
),
2022-08-24 11:01:50 +02:00
),
);
2022-08-24 09:39:36 +02:00
}
2022-08-24 12:13:24 +02:00
Size _calculateTableStart(Axis axis) {
return Size(
(axis == Axis.horizontal)
2022-11-18 14:22:05 +01:00
? _calculateTableTextSize().width / 2
: _calculateTableTextSize().width +
widget.theme.tablePaddingStart +
widget.theme.tableTextOffset,
2022-11-18 14:22:05 +01:00
(axis == Axis.vertical)
? _calculateTableTextSize().height / 2
: _calculateTableTextSize().height,
);
}
Widget _showBlock(TimeBlock block, {double linePadding = 0}) {
2022-08-24 17:14:50 +02:00
return Block(
blockDirection: widget.tableDirection,
linePadding: linePadding,
2022-08-24 17:14:50 +02:00
start: block.start,
end: block.end,
startHour: widget.startHour,
hourDimension: widget.hourDimension,
blockDimension: widget.blockDimension,
2022-08-24 17:14:50 +02:00
blockColor: widget.blockColor,
child: block.child,
);
}
2022-08-24 12:13:24 +02:00
void _scrollToFirstBlock() {
SchedulerBinding.instance.addPostFrameCallback((_) {
var earliestStart = widget.timeBlocks.map((block) => block.start).reduce(
(a, b) =>
a.hour < b.hour || (a.hour == b.hour && a.minute < b.minute)
? a
: b,
);
2022-08-24 13:32:35 +02:00
var initialOffset =
(widget.hourDimension * (widget.endHour - widget.startHour)) *
((earliestStart.hour - widget.startHour) /
(widget.endHour - widget.startHour)) +
_calculateTableTextSize().width / 2;
2022-08-24 12:13:24 +02:00
_scrollController.jumpTo(
initialOffset,
);
// TODO(freek): stop the controller from resetting
2022-08-24 12:13:24 +02:00
});
}
2022-08-24 13:32:35 +02:00
Size _calculateTableTextSize() {
return (TextPainter(
2022-09-01 12:05:37 +02:00
text: TextSpan(
text: '22:22',
style: widget.theme.timeStyle ?? Theme.of(context).textTheme.bodyText1,
),
2022-08-24 13:32:35 +02:00
maxLines: 1,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: TextDirection.ltr,
)..layout())
.size;
}
double _calculateTableHeight() {
var sum = 0.0;
if (widget.mergeBlocks || widget.combineBlocks) {
for (var orderedBlocks in (widget.mergeBlocks)
? mergeBlocksInColumns(widget.timeBlocks)
: groupBlocksById(widget.timeBlocks)) {
// check if any orderedBlock collides with another orderedBlock
if (orderedBlocks.map((block) => block.id).toSet().isNotEmpty &&
orderedBlocks.any(
(block) => orderedBlocks.any(
(block2) => block != block2 && block.collidesWith(block2),
),
)) {
// the sum is the combination of all the blocks
sum += orderedBlocks
.map((block) => block.childDimension ?? widget.blockDimension)
.reduce((a, b) => a + b);
} else {
sum +=
// get the highest block within the group
orderedBlocks
.map(
(block) => max(
block.childDimension ?? 0,
widget.blockDimension,
),
)
.reduce(max) +
widget.theme.blockPaddingBetween;
}
}
} else {
// sum of all the widget heights
sum = widget.timeBlocks
.map((block) => block.childDimension ?? widget.blockDimension)
.reduce((a, b) => a + b);
sum += widget.timeBlocks.length * widget.theme.blockPaddingBetween;
}
return sum;
}
2022-08-24 09:39:36 +02:00
}