Skip to content

Commit 8008eac

Browse files
Added DatePickerController, updated Readme.
1 parent 49abdde commit 8008eac

File tree

11 files changed

+194
-56
lines changed

11 files changed

+194
-56
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## [1.2.0] - 31/03/2020
2+
3+
* This version is breaking backward compatibility
4+
* Added option to show past dates
5+
* Added option to change the selected date text color separately
6+
* Added a controller to the control the DatePicker widget
7+
* Added option to change the width of the Date Widget Item
8+
19
## [1.1.3] - 30/09/2019
210

311
* Fixed issue with Locale not being initialized

README.md

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ import 'package:date_picker_timeline/date_picker_timeline.dart';
1818

1919
## Usage
2020

21-
Use the `DatePickerTimeline` Widget
21+
This version is breaking backwards compatibility
22+
23+
Use the `DatePicker` Widget
2224

2325
```dart
2426
Column(
2527
mainAxisAlignment: MainAxisAlignment.center,
2628
children: <Widget>[
27-
DatePickerTimeline(
29+
DatePicker(
2830
DateTime.now(),
31+
initialSelectedDate: DateTime.now(),
32+
selectionColor: Colors.black,
33+
selectedTextColor: Colors.white,
2934
onDateChange: (date) {
3035
// New date selected
31-
print(date.day.toString());
36+
setState(() {
37+
_selectedValue = date;
38+
});
3239
},
3340
),
3441
],
@@ -38,18 +45,21 @@ Column(
3845
##### Constructor:
3946

4047
```dart
41-
DatePickerTimeline(
42-
this.currentDate, {
43-
Key key,
44-
this.width,
45-
this.height = 80,
46-
this.monthTextStyle = defaultMonthTextStyle,
47-
this.dayTextStyle = defaultDayTextStyle,
48-
this.dateTextStyle = defaultDateTextStyle,
49-
this.selectionColor = AppColors.defaultSelectionColor,
50-
this.daysCount = 50000,
51-
this.onDateChange,
52-
this.locale,
48+
DatePicker(
49+
this.startDate, {
50+
Key key,
51+
this.width = 60,
52+
this.height = 80,
53+
this.controller,
54+
this.monthTextStyle = defaultMonthTextStyle,
55+
this.dayTextStyle = defaultDayTextStyle,
56+
this.dateTextStyle = defaultDateTextStyle,
57+
this.selectedTextColor = Colors.white,
58+
this.selectionColor = AppColors.defaultSelectionColor,
59+
this.initialSelectedDate,
60+
this.daysCount = 500,
61+
this.onDateChange,
62+
this.locale = "en_US",
5363
}) : super(key: key);
5464
```
5565

example/android/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.gradle.jvmargs=-Xmx1536M
22

3+
android.enableR8=true

example/lib/main.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,25 @@ class MyHomePage extends StatefulWidget {
2525
}
2626

2727
class _MyHomePageState extends State<MyHomePage> {
28+
DatePickerController _controller = DatePickerController();
29+
2830
DateTime _selectedValue = DateTime.now();
2931

32+
33+
@override
34+
void initState() {
35+
super.initState();
36+
}
37+
3038
@override
3139
Widget build(BuildContext context) {
3240
return Scaffold(
41+
floatingActionButton: FloatingActionButton(
42+
child: Icon(Icons.replay),
43+
onPressed: () {
44+
_controller.animateToSelection();
45+
},
46+
),
3347
appBar: AppBar(
3448
title: Text(widget.title),
3549
),
@@ -49,10 +63,11 @@ class _MyHomePageState extends State<MyHomePage> {
4963
),
5064
Container(
5165
child: DatePicker(
52-
DateTime.now(),
66+
DateTime.now().add(Duration(days: -3)),
5367
width: 60,
5468
height: 80,
55-
startDate: DateTime.now().add(Duration(days: -30)),
69+
controller: _controller,
70+
initialSelectedDate: DateTime.now(),
5671
selectionColor: Colors.black,
5772
selectedTextColor: Colors.white,
5873
onDateChange: (date) {

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ packages:
7070
path: ".."
7171
relative: true
7272
source: path
73-
version: "1.1.3"
73+
version: "1.2.0"
7474
flutter:
7575
dependency: "direct main"
7676
description: flutter

fonts/RobotoBold.ttf

-166 KB
Binary file not shown.

fonts/RobotoRegular.ttf

-167 KB
Binary file not shown.

lib/date_picker_widget.dart

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ import 'package:flutter/material.dart';
66
import 'package:intl/date_symbol_data_local.dart';
77

88
class DatePicker extends StatefulWidget {
9+
/// Start Date in case user wants to show past dates
10+
/// If not provided calendar will start from the initialSelectedDate
11+
final DateTime startDate;
12+
913
/// Width of the selector
1014
final double width;
1115

1216
/// Height of the selector
1317
final double height;
1418

19+
/// DatePicker Controller
20+
final DatePickerController controller;
21+
1522
/// Text color for the selected Date
1623
final Color selectedTextColor;
1724

@@ -33,28 +40,26 @@ class DatePicker extends StatefulWidget {
3340
/// Callback function for when a different date is selected
3441
final DateChangeListener onDateChange;
3542

36-
/// Start Date in case user wants to show past dates
37-
/// If not provided calendar will start from the initialSelectedDate
38-
final DateTime startDate;
39-
40-
/// Max limit up to which the dates are shown
43+
/// Max limit up to which the dates are shown.
44+
/// Days are counted from the startDate
4145
final int daysCount;
4246

4347
/// Locale for the calendar default: en_us
4448
final String locale;
4549

4650
DatePicker(
47-
this.initialSelectedDate, {
51+
this.startDate, {
4852
Key key,
49-
this.width,
53+
this.width = 60,
5054
this.height = 80,
55+
this.controller,
5156
this.monthTextStyle = defaultMonthTextStyle,
5257
this.dayTextStyle = defaultDayTextStyle,
5358
this.dateTextStyle = defaultDateTextStyle,
5459
this.selectedTextColor = Colors.white,
5560
this.selectionColor = AppColors.defaultSelectionColor,
56-
this.startDate,
57-
this.daysCount = 50000,
61+
this.initialSelectedDate,
62+
this.daysCount = 500,
5863
this.onDateChange,
5964
this.locale = "en_US",
6065
}) : super(key: key);
@@ -66,6 +71,8 @@ class DatePicker extends StatefulWidget {
6671
class _DatePickerState extends State<DatePicker> {
6772
DateTime _currentDate;
6873

74+
ScrollController _controller = ScrollController();
75+
6976
TextStyle selectedDateStyle;
7077
TextStyle selectedMonthStyle;
7178
TextStyle selectedDayStyle;
@@ -74,9 +81,13 @@ class _DatePickerState extends State<DatePicker> {
7481
void initState() {
7582
// Init the calendar locale
7683
initializeDateFormatting(widget.locale, null);
77-
7884
// Set initial Values
7985
_currentDate = widget.initialSelectedDate;
86+
87+
if (widget.controller != null) {
88+
widget.controller.setDatePickerState(this);
89+
}
90+
8091
this.selectedDateStyle = createTextStyle(widget.dateTextStyle);
8192
this.selectedMonthStyle = createTextStyle(widget.monthTextStyle);
8293
this.selectedDayStyle = createTextStyle(widget.dayTextStyle);
@@ -107,26 +118,24 @@ class _DatePickerState extends State<DatePicker> {
107118
child: ListView.builder(
108119
itemCount: widget.daysCount,
109120
scrollDirection: Axis.horizontal,
121+
controller: _controller,
110122
itemBuilder: (context, index) {
111123
// get the date object based on the index position
112124
// if widget.startDate is null then use the initialDateValue
113125
DateTime date;
114-
if (widget.startDate != null) {
115-
DateTime _date = widget.startDate.add(Duration(days: index));
116-
date = new DateTime(_date.year, _date.month, _date.day);
117-
} else {
118-
DateTime _date = widget.initialSelectedDate.add(Duration(days: index));
119-
date = new DateTime(_date.year, _date.month, _date.day);
120-
}
126+
DateTime _date = widget.startDate.add(Duration(days: index));
127+
date = new DateTime(_date.year, _date.month, _date.day);
121128

122129
// Check if this date is the one that is currently selected
123-
bool isSelected = compareDate(date, _currentDate);
130+
bool isSelected = _compareDate(date, _currentDate);
124131

125132
// Return the Date Widget
126133
return DateWidget(
127134
date: date,
128-
monthTextStyle: isSelected ? selectedMonthStyle : widget.monthTextStyle,
129-
dateTextStyle: isSelected ? selectedDateStyle : widget.dateTextStyle,
135+
monthTextStyle:
136+
isSelected ? selectedMonthStyle : widget.monthTextStyle,
137+
dateTextStyle:
138+
isSelected ? selectedDateStyle : widget.dateTextStyle,
130139
dayTextStyle: isSelected ? selectedDayStyle : widget.dayTextStyle,
131140
width: widget.width,
132141
locale: widget.locale,
@@ -147,9 +156,59 @@ class _DatePickerState extends State<DatePicker> {
147156
);
148157
}
149158

150-
bool compareDate(DateTime date1, DateTime date2) {
159+
/// Helper function to compare two dates
160+
/// Returns True if both dates are the same
161+
bool _compareDate(DateTime date1, DateTime date2) {
151162
return date1.day == date2.day &&
152163
date1.month == date2.month &&
153164
date1.year == date2.year;
154165
}
155166
}
167+
168+
class DatePickerController {
169+
_DatePickerState _datePickerState;
170+
171+
void setDatePickerState(_DatePickerState state) {
172+
_datePickerState = state;
173+
}
174+
175+
void jumpToSelection() {
176+
assert(_datePickerState != null,
177+
'DatePickerController is not attached to any DatePicker View.');
178+
179+
// jump to the current Date
180+
_datePickerState._controller
181+
.jumpTo(_calculateDateOffset(_datePickerState._currentDate));
182+
}
183+
184+
/// This function will animate the Timeline to the currently selected Date
185+
void animateToSelection(
186+
{duration = const Duration(milliseconds: 500), curve = Curves.linear}) {
187+
assert(_datePickerState != null,
188+
'DatePickerController is not attached to any DatePicker View.');
189+
190+
// animate to the current date
191+
_datePickerState._controller.animateTo(
192+
_calculateDateOffset(_datePickerState._currentDate),
193+
duration: duration,
194+
curve: curve);
195+
}
196+
197+
/// This function will animate to any date that is passed as a parameter
198+
/// In case a date is out of range nothing will happen
199+
void animateToDate(DateTime date,
200+
{duration = const Duration(milliseconds: 500), curve = Curves.linear}) {
201+
assert(_datePickerState != null,
202+
'DatePickerController is not attached to any DatePicker View.');
203+
204+
_datePickerState._controller.animateTo(_calculateDateOffset(date),
205+
duration: duration, curve: curve);
206+
}
207+
208+
/// Calculate the number of pixels that needs to be scrolled to go to the
209+
/// date provided in the argument
210+
double _calculateDateOffset(DateTime date) {
211+
int offset = date.difference(_datePickerState.widget.startDate).inDays + 1;
212+
return (offset * _datePickerState.widget.width) + (offset * 6);
213+
}
214+
}

lib/extra/style.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ import 'package:date_picker_timeline/extra/dimen.dart';
55
const TextStyle defaultMonthTextStyle = TextStyle(
66
color: AppColors.defaultMonthColor,
77
fontSize: Dimen.monthTextSize,
8-
fontFamily: 'Roboto',
98
fontWeight: FontWeight.w500,
109
);
1110

1211
const TextStyle defaultDateTextStyle = TextStyle(
1312
color: AppColors.defaultDateColor,
1413
fontSize: Dimen.dateTextSize,
15-
fontFamily: 'Roboto',
1614
fontWeight: FontWeight.w500,
1715
);
1816

1917
const TextStyle defaultDayTextStyle = TextStyle(
2018
color: AppColors.defaultDayColor,
2119
fontSize: Dimen.dayTextSize,
22-
fontFamily: 'Roboto',
2320
fontWeight: FontWeight.w500,
2421
);

0 commit comments

Comments
 (0)