flutter_timetable/lib/src/timetable.dart

215 lines
6.5 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({
2022-08-24 12:03:32 +02:00
this.timeBlocks = const [],
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,
2022-08-24 12:03:32 +02:00
this.blockWidth = 50,
2022-08-24 13:32:35 +02:00
this.blockColor = const Color(0x80FF0000),
this.hourHeight = 80,
this.theme = const TableTheme(),
this.mergeBlocks = false,
this.combineBlocks = true,
2022-08-24 11:01:50 +02:00
Key? key,
}) : super(key: key);
/// 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 width of the block if there is no child
final double blockWidth;
2022-08-24 13:32:35 +02:00
/// The color of the block if there is no child
final Color blockColor;
/// The heigh of one hour in the timetable.
final double hourHeight;
/// 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();
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;
}
2022-08-24 11:01:50 +02:00
return SingleChildScrollView(
physics: widget.scrollPhysics ?? const BouncingScrollPhysics(),
2022-08-24 11:01:50 +02:00
controller: _scrollController,
child: Stack(
children: [
table.Table(
2022-08-24 11:01:50 +02:00
startHour: widget.startHour,
endHour: widget.endHour,
hourHeight: widget.hourHeight,
tableOffset: _calculateTableStart(),
2022-08-24 13:32:35 +02:00
theme: widget.theme,
2022-08-24 11:01:50 +02:00
),
2022-08-24 12:03:32 +02:00
Container(
2022-08-24 13:32:35 +02:00
margin: EdgeInsets.only(
left: _calculateTableStart(),
2022-08-24 13:32:35 +02:00
),
2022-08-24 12:03:32 +02:00
child: SingleChildScrollView(
physics: widget.scrollPhysics ?? const BouncingScrollPhysics(),
2022-08-24 12:03:32 +02:00
scrollDirection: Axis.horizontal,
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (widget.mergeBlocks || widget.combineBlocks) ...[
2022-08-24 17:14:50 +02:00
for (var orderedBlocks in (widget.mergeBlocks)
? mergeBlocksInColumns(blocks)
: groupBlocksById(blocks)) ...[
Stack(
children: [
2022-08-24 17:14:50 +02:00
for (var block in orderedBlocks) ...[
_showBlock(block),
],
],
),
2022-09-01 12:05:37 +02:00
SizedBox(
width: widget.theme.blockPaddingBetween,
),
2022-08-24 16:02:07 +02:00
],
2022-08-24 17:14:50 +02:00
] else ...[
for (var block in blocks) ...[
_showBlock(block),
2022-08-24 16:02:07 +02:00
],
2022-08-24 12:03:32 +02:00
],
SizedBox(
2022-09-01 12:05:37 +02:00
width: max(
widget.theme.tablePaddingEnd -
widget.theme.blockPaddingBetween,
0,
),
2022-08-24 13:32:35 +02:00
height: widget.hourHeight *
(widget.endHour -
widget.startHour +
0.5), // empty halfhour at the end
2022-08-24 12:03:32 +02: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
double _calculateTableStart() {
return _calculateTableTextSize().width +
2022-09-01 12:05:37 +02:00
widget.theme.tablePaddingStart +
widget.theme.tableTextOffset;
}
2022-08-24 17:14:50 +02:00
Widget _showBlock(TimeBlock block) {
return Block(
start: block.start,
end: block.end,
startHour: widget.startHour,
hourHeight: widget.hourHeight,
blockWidth: widget.blockWidth,
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.hourHeight * (widget.endHour - widget.startHour)) *
((earliestStart.hour - widget.startHour) /
(widget.endHour - widget.startHour));
2022-08-24 12:13:24 +02:00
_scrollController.jumpTo(
initialOffset,
);
});
}
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;
}
2022-08-24 09:39:36 +02:00
}