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 Header #73

Merged
merged 18 commits into from
Feb 23, 2019
Merged
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
75 changes: 19 additions & 56 deletions lib/flutter_calendar_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import 'package:date_utils/date_utils.dart';
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:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart' show DateFormat;

typedef MarkedDateIconBuilder<T> = Widget Function(T event);

class CalendarCarousel<T> extends StatefulWidget {

final double viewportFraction;
final TextStyle prevDaysTextStyle;
final TextStyle daysTextStyle;
Expand Down Expand Up @@ -188,16 +188,6 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {

@override
Widget build(BuildContext context) {
Widget headerText = DefaultTextStyle(
style: TextStyle(fontSize: 16.0, color: Colors.black),
child: widget.headerText != null
? widget.headerText
: Text(
widget.weekFormat
? '${_localeDate.format(_weeks[1].first)}'
: '${_localeDate.format(this._dates[1])}',
style: widget.headerTextStyle,
));
if (_isReloadSelectedDate) {
if (widget.selectedDateTime != null)
_selectedDate = widget.selectedDateTime;
Expand All @@ -210,42 +200,22 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {
height: widget.height,
child: Column(
children: <Widget>[
widget.showHeader
? Container(
margin: widget.headerMargin,
child: DefaultTextStyle(
style: widget.headerTextStyle != null
? widget.headerTextStyle
: defaultHeaderTextStyle,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
widget.showHeaderButton
? IconButton(
onPressed: () => _setDate(0),
icon: Icon(Icons.chevron_left,
color: widget.iconColor),
)
: Container(),
widget.headerTitleTouchable
? FlatButton(
onPressed:
widget.onHeaderTitlePressed != null
? widget.onHeaderTitlePressed
: () => _selectDateFromPicker(),
child: headerText,
)
: headerText,
widget.showHeaderButton
? IconButton(
onPressed: () => _setDate(2),
icon: Icon(Icons.chevron_right,
color: widget.iconColor),
)
: Container(),
])),
)
: Container(),
CalendarHeader(
showHeader: widget.showHeader,
headerMargin: widget.headerMargin,
headerTitle: widget.weekFormat
? '${_localeDate.format(_weeks[1].first)}'
: '${_localeDate.format(this._dates[1])}',
headerTextStyle: widget.headerTextStyle,
showHeaderButtons: widget.showHeaderButton,
headerIconColor: widget.iconColor,
onLeftButtonPressed: () => _setDate(0),
onRightButtonPressed: () => _setDate(2),
isTitleTouchable: widget.headerTitleTouchable,
onHeaderTitlePressed: widget.onHeaderTitlePressed != null
? widget.onHeaderTitlePressed
: () => _selectDateFromPicker(),
),
Container(
child: !widget.showWeekDays
? Container()
Expand Down Expand Up @@ -965,28 +935,21 @@ class _CalendarState<T> extends State<CalendarCarousel<T>> {
);
}
} else {


//max 5 dots
if(event_index < 5) {
if (event_index < 5) {
hyochan marked this conversation as resolved.
Show resolved Hide resolved
if (widget.markedDateIconBuilder != null) {
tmp.add(widget.markedDateIconBuilder(event));
}
else {
} else {
if (widget.markedDateWidget != null) {
tmp.add(widget.markedDateWidget);
} else {
tmp.add(defaultMarkedDateWidget);
}
}
}



}

event_index++;

});
return tmp;
}
Expand Down
63 changes: 63 additions & 0 deletions lib/src/calendar_header.dart
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();
}
101 changes: 101 additions & 0 deletions test/src/header_test.dart
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';
hyochan marked this conversation as resolved.
Show resolved Hide resolved

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),
),
);