-
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 #73 from edTheGuy00/refactoring
[WIP] Refactoring - Separate and Test Header
- Loading branch information
Showing
3 changed files
with
183 additions
and
56 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,63 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'default_styles.dart' show defaultHeaderTextStyle; | ||
|
||
class CalendarHeader extends StatelessWidget { | ||
CalendarHeader( | ||
{@required this.headerTitle, | ||
this.headerMargin, | ||
this.showHeader, | ||
this.headerTextStyle, | ||
this.showHeaderButtons, | ||
this.headerIconColor, | ||
@required this.onLeftButtonPressed, | ||
@required this.onRightButtonPressed, | ||
this.isTitleTouchable, | ||
@required this.onHeaderTitlePressed}); | ||
|
||
final String headerTitle; | ||
final EdgeInsetsGeometry headerMargin; | ||
final bool showHeader; | ||
final TextStyle headerTextStyle; | ||
final bool showHeaderButtons; | ||
final Color headerIconColor; | ||
final VoidCallback onLeftButtonPressed; | ||
final VoidCallback onRightButtonPressed; | ||
final bool isTitleTouchable; | ||
final VoidCallback onHeaderTitlePressed; | ||
|
||
TextStyle get getTextStyle => | ||
headerTextStyle != null ? headerTextStyle : defaultHeaderTextStyle; | ||
|
||
Widget _leftButton() => IconButton( | ||
onPressed: onLeftButtonPressed, | ||
icon: Icon(Icons.chevron_left, color: headerIconColor), | ||
); | ||
|
||
Widget _rightButton() => IconButton( | ||
onPressed: onRightButtonPressed, | ||
icon: Icon(Icons.chevron_right, color: headerIconColor), | ||
); | ||
|
||
Widget _headerTouchable() => FlatButton( | ||
onPressed: onHeaderTitlePressed, | ||
child: Text(headerTitle, style: getTextStyle), | ||
); | ||
|
||
@override | ||
Widget build(BuildContext context) => showHeader | ||
? Container( | ||
margin: headerMargin, | ||
child: DefaultTextStyle( | ||
style: getTextStyle, | ||
child: Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: <Widget>[ | ||
showHeaderButtons ? _leftButton() : Container(), | ||
isTitleTouchable | ||
? _headerTouchable() | ||
: Text(headerTitle, style: getTextStyle), | ||
showHeaderButtons ? _rightButton() : Container(), | ||
])), | ||
) | ||
: 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,101 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:flutter_calendar_carousel/src/calendar_header.dart'; | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
void main() { | ||
final title = "Test title"; | ||
final margin = const EdgeInsets.symmetric(vertical: 16.0); | ||
final iconColor = Colors.blueAccent; | ||
|
||
testWidgets('Verify Header Defaults', (WidgetTester tester) async { | ||
var headerTapped = false; | ||
var leftPressed = false; | ||
var rightPressed = false; | ||
|
||
await tester.pumpWidget(wrapped(CalendarHeader( | ||
headerTitle: title, | ||
headerMargin: margin, | ||
showHeader: true, | ||
showHeaderButtons: true, | ||
headerIconColor: iconColor, | ||
onHeaderTitlePressed: () => headerTapped = true, | ||
onRightButtonPressed: () => rightPressed = true, | ||
onLeftButtonPressed: () => leftPressed = true, | ||
isTitleTouchable: true, | ||
))); | ||
|
||
expect(find.text(title), findsOneWidget); | ||
|
||
await tester.tap(find.byType(FlatButton)); | ||
|
||
await tester.pump(); | ||
|
||
expect(headerTapped, equals(true)); | ||
|
||
await tester.tap(find.widgetWithIcon(IconButton, Icons.chevron_right)); | ||
|
||
await tester.pump(); | ||
|
||
expect(rightPressed, equals(true)); | ||
|
||
await tester.tap(find.widgetWithIcon(IconButton, Icons.chevron_left)); | ||
|
||
await tester.pump(); | ||
|
||
expect(leftPressed, equals(true)); | ||
}); | ||
|
||
testWidgets('Verify No header Renders', (WidgetTester tester) async { | ||
final noHeaderEmpty = CalendarHeader(showHeader: false); | ||
|
||
await tester.pumpWidget(Container(child: noHeaderEmpty)); | ||
|
||
expect(find.byWidget(noHeaderEmpty), findsOneWidget); | ||
}); | ||
|
||
testWidgets('Verify Header Is Not Touchable', (WidgetTester tester) async { | ||
await tester.pumpWidget(wrapped(CalendarHeader( | ||
headerTitle: title, | ||
headerMargin: margin, | ||
showHeader: true, | ||
showHeaderButtons: true, | ||
headerIconColor: iconColor, | ||
onHeaderTitlePressed: () {}, | ||
onRightButtonPressed: () {}, | ||
onLeftButtonPressed: () {}, | ||
isTitleTouchable: false, | ||
))); | ||
|
||
// the header FlatButton Should not render | ||
final touchableHeader = find.byType(FlatButton); | ||
|
||
expect(touchableHeader, findsNothing); | ||
}); | ||
|
||
testWidgets('Verify No Header Buttons', (WidgetTester tester) async { | ||
await tester.pumpWidget(wrapped(CalendarHeader( | ||
headerTitle: title, | ||
headerMargin: margin, | ||
showHeader: true, | ||
showHeaderButtons: false, | ||
headerIconColor: iconColor, | ||
onHeaderTitlePressed: () {}, | ||
onRightButtonPressed: () {}, | ||
onLeftButtonPressed: () {}, | ||
isTitleTouchable: true, | ||
))); | ||
|
||
// the header IconButtons Should not render | ||
final headerButton = find.byType(IconButton); | ||
|
||
expect(headerButton, findsNothing); | ||
}); | ||
} | ||
|
||
// header uses Row which requires MaterialApp as an ancestor | ||
Widget wrapped(Widget widget) => MaterialApp( | ||
home: Container( | ||
child: Material(child: widget), | ||
), | ||
); |