Sitelet https://github.com/omidh28/flutter_inline_calendar/commit/986cf524e733cef215321dfaa0a7d6554a655ae0
Skip to content

Commit 986cf52

Browse files
Fix bug with selected date change
1 parent 8a75c69 commit 986cf52

5 files changed

Lines changed: 11 additions & 16 deletions

File tree

‎lib/inline_calendar.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ class InlineCalendar extends StatelessWidget {
1313
Key? key,
1414
this.onChange,
1515
this.controller,
16-
this.selectedDate
16+
this.selectedDate,
1717
}) : super(key: key);
1818

1919
@override
2020
Widget build(BuildContext context) {
21+
print(selectedDate);
2122
final controller = this.controller;
2223
if (controller == null) {
2324
return BlocProvider(
24-
create: (_) => CalendarCubit(),
25+
create: (_) => CalendarCubit(
26+
initialDate: selectedDate ?? DateTime.now()
27+
),
2528
child: _buildInlineCalendar(),
2629
);
2730
}
@@ -37,7 +40,6 @@ class InlineCalendar extends StatelessWidget {
3740
key: key,
3841
onChange: this.onChange,
3942
maxWeeks: this.maxWeeks,
40-
selectedDate: selectedDate,
4143
height: 80,
4244
middleWeekday: 4,
4345
);

‎lib/src/calendar_day_tile.dart‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@ class InlineCalendarTile extends StatelessWidget {
1111
final bool isToday;
1212
final String title;
1313
final DateTime tileDate;
14-
final DateTime? selectedDate;
1514

1615
const InlineCalendarTile({
1716
Key? key,
1817
required this.onTap,
1918
required this.monthDay,
2019
required this.tileDate,
2120
this.isToday = false,
22-
this.selectedDate,
2321
this.title = '',
2422
}) : super(key: key);
2523

2624
@override
2725
Widget build(BuildContext context) {
2826
return BlocBuilder<CalendarCubit, CalendarState>(
2927
builder: (context, state) {
30-
bool isSelected = selectedDate != null
31-
? isSameDate(selectedDate!, tileDate)
32-
: isSameDate(state.selectedDate, tileDate);
28+
bool isSelected = isSameDate(state.selectedDate, tileDate);
3329
return Material(
3430
color: Colors.transparent,
3531
child: InkWell(

‎lib/src/calendar_days_row.dart‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class InlineCalendarRows extends StatelessWidget {
1111
final DateTime middleDate;
1212
final int pageNumber;
1313
final Locale locale;
14-
final DateTime? selectedDate;
1514
final void Function(DateTime) onChange;
1615

1716
InlineCalendarRows({
@@ -20,7 +19,6 @@ class InlineCalendarRows extends StatelessWidget {
2019
required this.pageNumber,
2120
required this.locale,
2221
required this.onChange,
23-
this.selectedDate,
2422
}) : super(key: key);
2523

2624
@override
@@ -52,7 +50,6 @@ class InlineCalendarRows extends StatelessWidget {
5250
onChange(dateTime);
5351
}
5452
},
55-
selectedDate: selectedDate,
5653
monthDay: dayOfMonth,
5754
isToday: isSameDate(dateTime, DateTime.now()),
5855
tileDate: removeTimeFrom(dateTime),

‎lib/src/cubit/calendar_cubit.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ class CalendarCubit extends Cubit<CalendarState> {
88
static const centralPage = 6;
99
final PageController pageController =
1010
PageController(initialPage: centralPage);
11+
final DateTime initialDate;
1112

1213
/// Creates a controller for an inline calendar.
13-
CalendarCubit() : super(CalendarState(selectedDate: DateTime.now()));
14+
CalendarCubit({required this.initialDate})
15+
: super(CalendarState(selectedDate: initialDate));
1416

1517
set selectedDate(DateTime selectedDate) {
1618
emit(CalendarState(

‎lib/src/inline_calendar_page_view.dart‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ class InlineCalendarPageView extends StatelessWidget {
99
final double height;
1010
final int maxWeeks;
1111
final int middleWeekday;
12-
final DateTime? selectedDate;
1312
final void Function(DateTime)? onChange;
1413

1514
const InlineCalendarPageView({
1615
Key? key,
1716
required this.height,
1817
required this.maxWeeks,
1918
required this.middleWeekday,
20-
this.selectedDate,
2119
this.onChange,
2220
}) : super(key: key);
2321

2422
@override
2523
Widget build(BuildContext context) {
2624
final selectedDateFromState =
27-
selectedDate ?? context.watch<CalendarCubit>().state.selectedDate;
25+
context.watch<CalendarCubit>().state.selectedDate;
26+
print(selectedDateFromState);
2827
final firstWeekMiddleDate = _firstWeekMiddleDate(selectedDateFromState);
2928
final controller = context.read<CalendarCubit>().pageController;
3029
return Container(
@@ -65,7 +64,6 @@ class InlineCalendarPageView extends StatelessWidget {
6564
controller: controller,
6665
itemBuilder: (context, index) {
6766
return InlineCalendarRows(
68-
selectedDate: selectedDateFromState,
6967
middleDate: safeAdd(
7068
firstWeekMiddleDate, Duration(days: (index * 7))),
7169
onChange: onChange!,

0 commit comments

Comments
 (0)