flutter_timetable/lib/src/timetable.dart

148 lines
4.1 KiB
Dart
Raw Normal View History

2022-08-24 09:39:36 +02:00
part of timetable;
class Timetable extends StatefulWidget {
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.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.tablePaddingStart = 10,
this.tablePaddingEnd = 15,
this.theme = const TableTheme(),
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 padding between the table markings and the first block.
final double tablePaddingStart;
/// The padding between the last block and the end of the table.
final double tablePaddingEnd;
/// 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
@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-08-24 12:13:24 +02:00
_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 11:01:50 +02:00
return SingleChildScrollView(
physics: const BouncingScrollPhysics(),
controller: _scrollController,
child: Stack(
children: [
Table(
startHour: widget.startHour,
endHour: widget.endHour,
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: _calculateTableTextSize().width +
widget.tablePaddingStart +
5,
),
2022-08-24 12:03:32 +02:00
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (var block in widget.timeBlocks) ...[
Block(
start: block.start,
end: block.end,
startHour: widget.startHour,
2022-08-24 13:32:35 +02:00
hourHeight: widget.hourHeight,
2022-08-24 12:03:32 +02:00
blockWidth: widget.blockWidth,
child: block.child,
2022-08-24 13:32:35 +02:00
),
2022-08-24 12:03:32 +02:00
],
SizedBox(
2022-08-24 13:32:35 +02:00
width: widget.tablePaddingEnd,
height: widget.hourHeight *
(widget.endHour - widget.startHour + 0.5),
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
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
/// Calculates the width of 22:22
Size _calculateTableTextSize() {
return (TextPainter(
text: TextSpan(text: '22:22', style: widget.theme.timeStyle),
maxLines: 1,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: TextDirection.ltr,
)..layout())
.size;
}
2022-08-24 09:39:36 +02:00
}