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

[WIP] Refactoring - Separate and Test Weekdays row #77

Merged
merged 11 commits into from
Mar 1, 2019
Merged
66 changes: 7 additions & 59 deletions lib/flutter_calendar_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_calendar_carousel/classes/event_list.dart';
import 'package:flutter_calendar_carousel/src/default_styles.dart';
import 'package:flutter_calendar_carousel/src/calendar_header.dart';
import 'package:flutter_calendar_carousel/src/weekday_row.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart' show DateFormat;

Expand Down Expand Up @@ -216,13 +217,12 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {
? widget.onHeaderTitlePressed
: () => _selectDateFromPicker(),
),
Container(
child: !widget.showWeekDays
? Container()
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: _renderWeekDays(),
),
WeekdayRow(
showWeekdays: widget.showWeekDays,
weekdayFormat: widget.weekDayFormat,
weekdayMargin: widget.weekDayMargin,
weekdayTextStyle: widget.weekdayTextStyle,
localeDate: _localeDate,
),
Expanded(
child: PageView.builder(
Expand Down Expand Up @@ -788,58 +788,6 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {
}
}

List<Widget> _renderWeekDays() {
List<Widget> list = [];

/// because of number of days in a week is 7, so it would be easier to count it til 7.
for (var i = firstDayOfWeek, count = 0;
count < 7;
i = (i + 1) % 7, count++) {
String weekDay;

switch (widget.weekDayFormat) {
case WeekdayFormat.weekdays:
weekDay = _localeDate.dateSymbols.WEEKDAYS[i];
break;
case WeekdayFormat.standalone:
weekDay = _localeDate.dateSymbols.STANDALONEWEEKDAYS[i];
break;
case WeekdayFormat.short:
weekDay = _localeDate.dateSymbols.SHORTWEEKDAYS[i];
break;
case WeekdayFormat.standaloneShort:
weekDay = _localeDate.dateSymbols.STANDALONESHORTWEEKDAYS[i];
break;
case WeekdayFormat.narrow:
weekDay = _localeDate.dateSymbols.NARROWWEEKDAYS[i];
break;
case WeekdayFormat.standaloneNarrow:
weekDay = _localeDate.dateSymbols.STANDALONENARROWWEEKDAYS[i];
break;
default:
weekDay = _localeDate.dateSymbols.STANDALONEWEEKDAYS[i];
break;
}

list.add(
Expanded(
child: Container(
margin: widget.weekDayMargin,
child: Center(
child: DefaultTextStyle(
style: defaultWeekdayTextStyle,
child: Text(
weekDay,
style: widget.weekdayTextStyle,
),
),
),
)),
);
}
return list;
}

Widget _renderMarked(DateTime now) {
if (widget.markedDates != null && widget.markedDates.length > 0) {
List<DateTime> markedDates = widget.markedDates.map((date) {
Expand Down
75 changes: 75 additions & 0 deletions lib/src/weekday_row.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart';
import 'package:flutter_calendar_carousel/src/default_styles.dart'
show defaultWeekdayTextStyle;
import 'package:intl/intl.dart';

class WeekdayRow extends StatelessWidget {
WeekdayRow(
{@required this.showWeekdays,
@required this.weekdayFormat,
@required this.weekdayMargin,
@required this.weekdayTextStyle,
@required this.localeDate});

final bool showWeekdays;
final WeekdayFormat weekdayFormat;
final EdgeInsets weekdayMargin;
final TextStyle weekdayTextStyle;
final DateFormat localeDate;

Widget _weekdayContainer(String weekDay) => Expanded(
child: Container(
margin: weekdayMargin,
child: Center(
child: DefaultTextStyle(
style: defaultWeekdayTextStyle,
child: Text(
weekDay,
style: weekdayTextStyle,
),
),
),
));

List<Widget> _generateWeekdays() {
switch (weekdayFormat) {
case WeekdayFormat.weekdays:
return localeDate.dateSymbols.WEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
case WeekdayFormat.standalone:
return localeDate.dateSymbols.STANDALONEWEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
case WeekdayFormat.short:
return localeDate.dateSymbols.SHORTWEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
case WeekdayFormat.standaloneShort:
return localeDate.dateSymbols.STANDALONESHORTWEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
case WeekdayFormat.narrow:
return localeDate.dateSymbols.NARROWWEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
case WeekdayFormat.standaloneNarrow:
return localeDate.dateSymbols.STANDALONENARROWWEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
default:
return localeDate.dateSymbols.STANDALONEWEEKDAYS
.map<Widget>(_weekdayContainer)
.toList();
}
}

@override
Widget build(BuildContext context) => showWeekdays
? Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: _generateWeekdays(),
)
: Container();
}
102 changes: 102 additions & 0 deletions test/src/weekday_row_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart'
show WeekdayFormat;
import 'package:intl/intl.dart' show DateFormat;

import 'package:flutter_calendar_carousel/src/weekday_row.dart';
import 'package:flutter/material.dart';

void main() {
final locale = DateFormat.yMMM("en_US");
final margin = const EdgeInsets.only(bottom: 4.0);

testWidgets('test short weekday row', (WidgetTester tester) async {
await tester.pumpWidget(wrapped(
WeekdayRow(
showWeekdays: true,
weekdayFormat: WeekdayFormat.short,
weekdayMargin: margin,
weekdayTextStyle: null,
localeDate: locale,
),
));

expect(find.text('Sun'), findsOneWidget);
expect(find.text('Mon'), findsOneWidget);
expect(find.text('Tue'), findsOneWidget);
expect(find.text('Wed'), findsOneWidget);
expect(find.text('Thu'), findsOneWidget);
expect(find.text('Fri'), findsOneWidget);
expect(find.text('Sat'), findsOneWidget);
});

testWidgets('test narrow weekday row', (WidgetTester tester) async {
await tester.pumpWidget(wrapped(WeekdayRow(
showWeekdays: true,
weekdayFormat: WeekdayFormat.standaloneNarrow,
weekdayMargin: margin,
weekdayTextStyle: null,
localeDate: locale,
)));

// sat and sun
expect(find.text('S'), findsNWidgets(2));
// thurs and tues
expect(find.text('T'), findsNWidgets(2));

expect(find.text('M'), findsOneWidget);
expect(find.text('W'), findsOneWidget);
expect(find.text('F'), findsOneWidget);
});

testWidgets('test standalone weekday row', (WidgetTester tester) async {
await tester.pumpWidget(wrapped(WeekdayRow(
showWeekdays: true,
weekdayFormat: WeekdayFormat.standalone,
weekdayMargin: margin,
weekdayTextStyle: null,
localeDate: locale,
)));

expect(find.text('Sunday'), findsOneWidget);
expect(find.text('Monday'), findsOneWidget);
expect(find.text('Tuesday'), findsOneWidget);
expect(find.text('Wednesday'), findsOneWidget);
expect(find.text('Thursday'), findsOneWidget);
expect(find.text('Friday'), findsOneWidget);
expect(find.text('Saturday'), findsOneWidget);
});

testWidgets('test standalone short weekday row', (WidgetTester tester) async {
await tester.pumpWidget(wrapped(WeekdayRow(
showWeekdays: true,
weekdayFormat: WeekdayFormat.standaloneShort,
weekdayMargin: margin,
weekdayTextStyle: null,
localeDate: locale,
)));

expect(find.text('Sun'), findsOneWidget);
expect(find.text('Mon'), findsOneWidget);
expect(find.text('Tue'), findsOneWidget);
expect(find.text('Wed'), findsOneWidget);
expect(find.text('Thu'), findsOneWidget);
expect(find.text('Fri'), findsOneWidget);
expect(find.text('Sat'), findsOneWidget);
});

testWidgets('test row does not render', (WidgetTester tester) async {
final emptyContainer = WeekdayRow(showWeekdays: false);

await tester.pumpWidget(emptyContainer);

expect(find.byType(Container), findsOneWidget);

expect(find.byType(Row), findsNothing);
});
}

Widget wrapped(Widget widget) => Directionality(
textDirection: TextDirection.ltr,
child: widget,
);