flutter_timetable/lib/src/widgets/block.dart

91 lines
2.6 KiB
Dart
Raw Normal View History

2022-11-01 09:46:23 +01:00
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
import 'package:flutter/material.dart';
2022-08-24 12:03:32 +02:00
class Block extends StatelessWidget {
/// The [Block] to create a Widget or container in a [TimeTable].
2022-08-24 12:03:32 +02:00
const Block({
required this.start,
required this.end,
required this.startHour,
required this.blockDimension,
required this.hourDimension,
required this.blockDirection,
this.blockColor = Colors.blue,
2022-08-24 12:03:32 +02:00
this.linePadding = 8,
this.child,
Key? key,
}) : super(key: key);
/// The [Axis] along which the [Block] will be displayed.
final Axis blockDirection;
2022-08-24 12:03:32 +02:00
/// The start time of the block in 24 hour format
final TimeOfDay start;
/// The end time of the block in 24 hour format
final TimeOfDay end;
/// Widget that will be displayed within the block
2022-08-24 12:03:32 +02:00
final Widget? child;
/// The start hour of the timetable.
final int startHour;
/// The dimension in pixels of one hour in the timetable
final double hourDimension;
2022-08-24 13:32:35 +02:00
/// The dimension in pixels of the block if there is no child
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;
2022-08-24 12:03:32 +02:00
/// The padding between the lines and the numbers
final double linePadding;
@override
Widget build(BuildContext context) {
return Container(
color: blockColor,
2022-08-24 12:03:32 +02:00
margin: EdgeInsets.only(
top: (blockDirection == Axis.vertical)
? (((start.hour - startHour) * Duration.minutesPerHour) +
start.minute) *
_sizePerMinute() +
linePadding
: 0,
left: (blockDirection == Axis.horizontal)
? ((((start.hour - startHour) * Duration.minutesPerHour) +
start.minute) *
_sizePerMinute() +
linePadding)
: 0,
2022-08-24 12:03:32 +02:00
),
height: (blockDirection == Axis.vertical)
? (((end.hour - start.hour) * Duration.minutesPerHour) +
end.minute -
start.minute) *
_sizePerMinute()
: null,
width: (blockDirection == Axis.horizontal)
? (((end.hour - start.hour) * Duration.minutesPerHour) +
end.minute -
start.minute) *
_sizePerMinute()
: null,
2022-08-24 12:03:32 +02:00
child: child ??
SizedBox(
height: (blockDirection == Axis.horizontal) ? blockDimension : 0,
width: (blockDirection == Axis.vertical) ? blockDimension : 0,
2022-08-24 12:03:32 +02:00
),
);
}
double _sizePerMinute() {
return hourDimension / Duration.minutesPerHour;
2022-08-24 12:03:32 +02:00
}
}