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 all 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
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
16 changes: 8 additions & 8 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ packages:
name: black_hole_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.12"
version: "0.2.15"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.1.0-nullsafety.3"
charcode:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.12"
version: "1.15.0-nullsafety.3"
convert:
dependency: transitive
description:
Expand All @@ -49,7 +49,7 @@ packages:
name: dartx
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
version: "0.5.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -61,7 +61,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.8"
version: "1.3.0-nullsafety.3"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -122,14 +122,14 @@ packages:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.3.0-nullsafety.3"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
version: "2.1.0-nullsafety.3"
sdks:
dart: ">=2.7.0 <3.0.0"
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.17.0"
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);
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ dependencies:

black_hole_flutter: ^0.2.11
collection: ^1.14.11
dartx: ^0.4.0
dartx: ^0.5.0
meta: ^1.1.8
pedantic: ^1.8.0+1
rxdart: ^0.24.0
time_machine: ^0.9.12
time_machine: 0.9.12

dev_dependencies:
flutter_test:
Expand Down
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');
});
}