-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from edTheGuy00/refactoring
[WIP] Refactoring - Separate and Test Weekdays row
- Loading branch information
Showing
3 changed files
with
184 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |