Skip to content

Commit

Permalink
Need to complete Tests, FLineCalendarController removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Daviiddoo committed Oct 30, 2024
1 parent d947aed commit 4347711
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 156 deletions.
2 changes: 1 addition & 1 deletion forui/example/lib/sandbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _SandboxState extends State<Sandbox> {
const FTextField.password(),
FLineCalendar(
controller: FCalendarController.date(
initialSelection: DateTime(2024, 10, 13).toUtc(),
initialSelection: DateTime.utc(2024, 9, 13),
),
),
const SizedBox(height: 20),
Expand Down
63 changes: 34 additions & 29 deletions forui/lib/src/widgets/line_calendar/line_calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:forui/src/widgets/line_calendar/line_calendar_controller.dart';
import 'package:forui/forui.dart';
import 'package:forui/src/widgets/line_calendar/line_calendar_tile.dart';

import 'package:meta/meta.dart';
import 'package:sugar/sugar.dart';

import 'package:forui/forui.dart' hide FLineCalendar, FLineCalendarContentStyle, FLineCalendarStyle;

const _textSpacing = 2.0;

/// A calendar that can be scrolled horizontally.
Expand Down Expand Up @@ -76,9 +74,6 @@ class _FLineCalendarState extends State<FLineCalendar> {
final offset = (value.difference(widget.start.toLocalDate()).inDays - 2) * _size + _style.itemPadding;
_controller = ScrollController(initialScrollOffset: offset);

final textDirection = Directionality.of(context);
widget.controller.addListener(() => _onDateChange(textDirection));

super.didChangeDependencies();
}

Expand All @@ -90,33 +85,43 @@ class _FLineCalendarState extends State<FLineCalendar> {
return dateTextSize + dayTextSize + _textSpacing + (style.content.verticalPadding * 2);
}

void _onDateChange(TextDirection textDirection) {
setState(() {
//TODO: localizations.
SemanticsService.announce(widget.controller.value.toString(), textDirection);
});
}

@override
Widget build(BuildContext context) => SizedBox(
height: _size,
child: ListView.builder(
controller: _controller,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.zero,
itemExtent: _size,
itemBuilder: (context, index) {
final date = widget.start.add(Duration(days: index));
return Container(
padding: EdgeInsets.symmetric(horizontal: _style.itemPadding),
child: FlineCalendarTile(
style: _style,
controller: widget.controller,
date: date,
isToday: widget.today == date,
),
);
child: Focus(
autofocus: true,
onKeyEvent: (node, event) {
if (event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.arrowRight) {
widget.controller.value = widget.controller.value?.add(const Duration(days: 1));
_controller.animateTo(_controller.offset + _size,
duration: const Duration(milliseconds: 100), curve: Curves.easeInOut);
return KeyEventResult.handled;
} else if (event is KeyDownEvent && event.logicalKey == LogicalKeyboardKey.arrowLeft) {
widget.controller.value = widget.controller.value?.subtract(const Duration(days: 1));
_controller.animateTo(_controller.offset - _size,
duration: const Duration(milliseconds: 100), curve: Curves.easeInOut);
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
child: ListView.builder(
controller: _controller,
scrollDirection: Axis.horizontal,
padding: EdgeInsets.zero,
itemExtent: _size,
itemBuilder: (context, index) {
final date = widget.start.add(Duration(days: index));
return Container(
padding: EdgeInsets.symmetric(horizontal: _style.itemPadding),
child: FlineCalendarTile(
style: _style,
controller: widget.controller,
date: date,
isToday: widget.today == date,
),
);
},
),
),
);

Expand Down
122 changes: 0 additions & 122 deletions forui/lib/src/widgets/line_calendar/line_calendar_controller.dart

This file was deleted.

52 changes: 48 additions & 4 deletions forui/test/src/widgets/line_calendar_golden_test.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
@Tags(['golden'])
library;

import 'dart:ui';

import 'package:flutter/widgets.dart';

import 'package:flutter_test/flutter_test.dart';
import 'package:forui/forui.dart';

import 'package:forui/src/widgets/line_calendar/line_calendar.dart';
import 'package:forui/src/widgets/line_calendar/line_calendar_controller.dart';
import '../test_scaffold.dart';

void main() {
Expand All @@ -32,7 +33,7 @@ void main() {

for (final (name, theme, _) in TestScaffold.themes) {
for (final (lineCalendar, controller) in [
('default', FLineCalendarController(today: DateTime(2024, 10, 20))),
('default', FCalendarController.date(initialSelection: DateTime.utc(2024, 10, 20))),
]) {
testWidgets('$name - $lineCalendar', (tester) async {
await tester.pumpWidget(
Expand All @@ -41,15 +42,58 @@ void main() {
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: FLineCalendar(controller: FCalendarController.date()),
child: FLineCalendar(controller: controller),
),
),
),
);

await expectLater(
find.byType(TestScaffold),
matchesGoldenFile('line_calendar/$name-$lineCalendar/default.png'),
);
});

testWidgets('new date selected - $name', (tester) async {
await tester.pumpWidget(
TestScaffold(
theme: theme,
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: FLineCalendar(controller: controller),
),
),
),
);

await tester.tap(find.text('24'));
await tester.pumpAndSettle();

await expectLater(
find.byType(TestScaffold),
matchesGoldenFile('line_calendar/$name-$lineCalendar/new-date.png'),
);
});

testWidgets('unselected - $name', (tester) async {
await tester.pumpWidget(
TestScaffold(
theme: theme,
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: FLineCalendar(controller: controller),
),
),
),
);
await tester.tap(find.text('20'));
await tester.pumpAndSettle();

await expectLater(
find.byType(TestScaffold),
matchesGoldenFile('line_calendar/$name-$lineCalendar.png'),
matchesGoldenFile('line_calendar/$name-$lineCalendar/unselected.png'),
);
});
}
Expand Down

0 comments on commit 4347711

Please sign in to comment.