Skip to content
Open
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
7 changes: 5 additions & 2 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:intl/intl.dart';

/// Returns the default week days as strings (using intl).
List<String> defaultWeekDays() =>
DateFormat.E().dateSymbols.WEEKDAYS.map((e) => e.substring(0, 3)).toList();
List<String> defaultWeekDays({int lengthOfDateNames = 3}) => DateFormat.E()
.dateSymbols
.WEEKDAYS
.map((e) => e.substring(0, lengthOfDateNames))
.toList();

extension ListUtils on List {
/// Shifts the list by "amount" places
Expand Down
13 changes: 12 additions & 1 deletion lib/src/widgets/date_range_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ class DayNamesRow extends StatelessWidget {
Key? key,
required this.textStyle,
List<String>? weekDays,
int lengthOfDateName = 3,
int firstDayOfWeek = 0,
}) : weekDays = (weekDays ?? defaultWeekDays()).shiftBy(firstDayOfWeek),
}) : weekDays =
(weekDays ?? defaultWeekDays(lengthOfDateNames: lengthOfDateName))
.shiftBy(firstDayOfWeek),
super(key: key);

final TextStyle textStyle;
Expand Down Expand Up @@ -142,12 +145,15 @@ class DateRangePickerWidget extends StatefulWidget {
this.separatorThickness = 1,
this.allowSingleTapDaySelection = false,
this.firstDayOfWeek = 0,
this.lengthOfDateName = 3,
}) : assert(
firstDayOfWeek >= 0 && firstDayOfWeek <= 6,
'firstDayOfWeek must be in the range [0..6].',
),
super(key: key);

final int lengthOfDateName;

/// Called whenever the selected date range is changed.
final ValueChanged<DateRange?> onDateRangeChanged;

Expand Down Expand Up @@ -267,6 +273,7 @@ class DateRangePickerWidgetState extends State<DateRangePickerWidget> {
delta: calendarController
.retrieveDeltaForMonth(widget.firstDayOfWeek),
firstDayOfWeek: widget.firstDayOfWeek,
lengthOfDateName: widget.lengthOfDateName,
),
if (widget.doubleMonth) ...{
if (widget.displayMonthsSeparator)
Expand Down Expand Up @@ -338,11 +345,14 @@ class EnrichedMonthWrapWidget extends StatelessWidget {
required this.days,
required this.delta,
this.firstDayOfWeek = 0,
this.lengthOfDateName = 3,
}) : super(key: key);

/// The theme to use for the calendar.
final CalendarTheme theme;

final int lengthOfDateName;

/// A callback that is called when the selected date changes.
final ValueChanged<DateTime> onDateChanged;

Expand All @@ -364,6 +374,7 @@ class EnrichedMonthWrapWidget extends StatelessWidget {
DayNamesRow(
textStyle: theme.dayNameTextStyle,
firstDayOfWeek: firstDayOfWeek,
lengthOfDateName: lengthOfDateName,
),
const SizedBox(height: 16),
MonthWrapWidget(
Expand Down
38 changes: 36 additions & 2 deletions test/widgets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void main() {
});

group('DateRangePickerWidget', () {
testWidgets('renders correctly and initializes with initial dateRange',
testWidgets(
'renders correctly and initializes with initial dateRange render 2 letters names',
(WidgetTester tester) async {
final initialDateRange =
DateRange(DateTime(2023, 1, 1), DateTime(2023, 1, 5));
Expand All @@ -29,12 +30,13 @@ void main() {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: SizedBox(
height: 334,
height: 340,
child: DateRangePickerWidget(
initialDateRange: initialDateRange,
minDate: minDate,
maxDate: maxDate,
onDateRangeChanged: (DateRange? dateRange) {},
lengthOfDateName: 2,
),
),
),
Expand All @@ -43,6 +45,38 @@ void main() {
expect(find.byType(DateRangePickerWidget), findsOneWidget);
expect(find.byType(MonthSelectorAndDoubleIndicator), findsOneWidget);
expect(find.byType(DayNamesRow), findsNWidgets(2));
expect(find.text('Mo'), findsOneWidget);
expect(find.byType(MonthWrapWidget), findsNWidgets(2));
expect(find.byType(DayTileWidget), findsWidgets);
});

testWidgets(
'renders correctly and initializes with initial dateRange 3 length names',
(WidgetTester tester) async {
final initialDateRange =
DateRange(DateTime(2023, 1, 1), DateTime(2023, 1, 5));
final minDate = DateTime(2022, 1, 1);
final maxDate = DateTime(2023, 12, 31);

await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: SizedBox(
height: 340,
child: DateRangePickerWidget(
initialDateRange: initialDateRange,
minDate: minDate,
maxDate: maxDate,
onDateRangeChanged: (DateRange? dateRange) {},
lengthOfDateName: 3,
),
),
),
));

expect(find.byType(DateRangePickerWidget), findsOneWidget);
expect(find.byType(MonthSelectorAndDoubleIndicator), findsOneWidget);
expect(find.byType(DayNamesRow), findsNWidgets(2));
expect(find.text('Mon'), findsExactly(2));
expect(find.byType(MonthWrapWidget), findsNWidgets(2));
expect(find.byType(DayTileWidget), findsWidgets);
});
Expand Down