Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom hour of day label formatter #42

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"request": "launch",
"type": "dart",
"program": "example/lib/main.dart"
}
]
}
13 changes: 13 additions & 0 deletions example/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart"
}
]
}
kennethj marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ class _TimetableExampleState extends State<TimetableExample> {
],
),
body: Timetable<BasicEvent>(
theme: TimetableThemeData(
hourTextStyle: TextStyle(fontSize: 9, color: Colors.blueGrey),
formatHour: (time) =>
'${time.hourOf12HourClock} ${time.hourOfDay > 11 ? "PM" : "AM"}',
hourColumnWidth: 40,
),
controller: _controller,
onEventBackgroundTap: (start, isAllDay) {
_showSnackBar('Background tapped $start is all day event $isAllDay');
Expand Down
5 changes: 4 additions & 1 deletion lib/src/content/date_hours_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ class DateHoursPainter extends CustomPainter {
DateHoursPainter({
@required this.textStyle,
@required this.textDirection,
String Function(LocalTime) formatHour,
}) : assert(textStyle != null),
assert(textDirection != null),
_painters = [
for (final h in innerDateHours)
TextPainter(
text: TextSpan(
text: _pattern.format(LocalTime(h, 0, 0)),
text: formatHour != null
? formatHour(LocalTime(h, 0, 0))
: _pattern.format(LocalTime(h, 0, 0)),
style: textStyle,
),
textDirection: textDirection,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/content/timetable_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TimetableContent<E extends Event> extends StatelessWidget {
child: Row(
children: <Widget>[
Container(
width: hourColumnWidth,
width: timetableTheme.hourColumnWidth ?? hourColumnWidth,
padding: EdgeInsets.only(right: 12),
child: CustomPaint(
painter: DateHoursPainter(
Expand All @@ -47,6 +47,7 @@ class TimetableContent<E extends Event> extends StatelessWidget {
color: context.theme.disabledOnBackground,
),
textDirection: context.directionality,
formatHour: timetableTheme.formatHour,
),
size: Size.infinite,
),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/header/timetable_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TimetableHeader<E extends Event> extends StatelessWidget {
return Row(
children: <Widget>[
SizedBox(
width: hourColumnWidth,
width: context?.timetableTheme?.hourColumnWidth ?? hourColumnWidth,
child: ValueListenableBuilder<LocalDate>(
valueListenable: controller.dateListenable,
builder: (context, date, _) {
Expand Down
14 changes: 14 additions & 0 deletions lib/src/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class TimetableThemeData {
this.dateIndicatorTextStyle,
this.allDayEventHeight,
this.hourTextStyle,
this.hourColumnWidth,
this.formatHour,
this.timeIndicatorColor,
this.dividerColor,
this.minimumHourHeight,
Expand Down Expand Up @@ -106,6 +108,14 @@ class TimetableThemeData {
/// [TextStyle] used to display the hours of the day.
final TextStyle hourTextStyle;

/// Width of hour of day column
///
/// Default to 48
final double hourColumnWidth;

/// [HourFormatter] used to format the hours of the day string.
final HourFormatter formatHour;

/// [Color] for painting the current time indicator.
final Color timeIndicatorColor;

Expand Down Expand Up @@ -177,6 +187,8 @@ class TimetableThemeData {
dateIndicatorTextStyle,
allDayEventHeight,
hourTextStyle,
hourColumnWidth,
formatHour,
timeIndicatorColor,
dividerColor,
minimumHourHeight,
Expand Down Expand Up @@ -273,3 +285,5 @@ extension TimetableThemeBuildContext on BuildContext {
/// Shortcut for `TimetableTheme.of(context)`.
TimetableThemeData get timetableTheme => TimetableTheme.of(this);
}

typedef HourFormatter = String Function(LocalTime time);
21 changes: 21 additions & 0 deletions test/content/date_hours_painter_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flutter/painting.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:timetable/src/content/date_hours_painter.dart';

void main() {
testWidgets('Format hour', (tester) async {
final painter = DateHoursPainter(
textDirection: TextDirection.ltr, textStyle: TextStyle());
await tester.pumpWidget(CustomPaint(painter: painter));
// find.text('01:00') does not work with text painters :(
});
testWidgets('Custom hour format', (tester) async {
final painter = DateHoursPainter(
textDirection: TextDirection.ltr,
textStyle: TextStyle(),
formatHour: (x) => '${x.hourOfDay}',
);
await tester.pumpWidget(CustomPaint(painter: painter));
});
}
15 changes: 15 additions & 0 deletions test/theme_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:time_machine/time_machine.dart';
import 'package:timetable/timetable.dart';

void main() {
test('formatHour is null by default', () {
final theme = TimetableThemeData();
expect(theme.formatHour, isNull);
});
test('format hour', () {
final theme = TimetableThemeData(formatHour: (time) => '${time.hourOfDay}');
final hourString = theme.formatHour(LocalTime(9, 42, 00));
expect(hourString, '9');
});
}