aleksanderwozniak / table_calendar

Highly customizable, feature-packed calendar widget for Flutter
Apache License 2.0
1.77k stars 974 forks source link

Wrong order of calendar formats and its implication inheritance #869

Open xxwag opened 2 months ago

xxwag commented 2 months ago

Describe the bug I initialize the calendar format as explained in the examples, yet the reading of the format change button is incorrect (2 weeks for month layout etc..) Simply renaming the calendar formats name using availablecalendarformats wont work as there is are hidden properties behind it (for example if i try to get the render box constraints of the calendar table it results incorrectly). Altrought the rest of the behaviour is smooth and im not getting any overflows or anything...

To reproduce

inside the buildcalendar: onFormatChanged: (format) { WidgetsBinding.instance .addPostFrameCallback(_afterLayout); if (_calendarFormat != format) { setState(() { _calendarFormat = format; }); } else { print('Format unchanged, no state update needed.'); } }, inside the widget build: SliverToBoxAdapter( child: LayoutBuilder( builder: (context, constraints) { return ConstrainedBox( constraints: BoxConstraints( maxHeight: constraints.maxHeight, // Use parent max height minHeight: 200, ), child: buildCalendar(), ); }, ), ),

Steps to reproduce the behavior: I feel like it missbehaved since the beggining ..

Expected behavior The calendar format to match the actual layout

Screenshots image

Output of flutter doctor Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.19.5, on Microsoft Windows [Version 10.0.22621.1413], locale en-US) [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.6) [√] Android Studio (version 2022.3) [√] VS Code (version 1.88.1) [√] Connected device (3 available) [√] Network resources

Additional context Add any other context about the problem here.

xxwag commented 2 months ago

Sorry i found this and it solves that https://github.com/aleksanderwozniak/table_calendar/issues/199

, but id still like to ask whether the Calendar Table shouldnt be ready to emit onhandleupdate behaviour for multiple daycell selection or am i wrong? I have to hard code something like this to retrieve the daycell positions and its annoying. Also the usage of the range selection is pretty unclear from my POV.

DateTime _calculateDateFromGesture(Offset localPosition) { final RenderBox? box = _calendarKey.currentContext?.findRenderObject() as RenderBox?; if (box != null) { double cellWidth = box.size.width / 7; double rowHeight = _getRowHeight();

  int column = (localPosition.dx / cellWidth).floor();
  int row = (localPosition.dy / rowHeight).floor();

  DateTime firstVisibleDay =
      _focusedDay.subtract(Duration(days: _focusedDay.weekday - 1));
  DateTime calculatedDate =
      firstVisibleDay.add(Duration(days: row * 7 + column));

  print("First Visible Day: $firstVisibleDay");
  print("Calculated Date: $calculatedDate");

  return calculatedDate;
}
return _focusedDay; // Fallback if box is not found

}

Somtobro commented 2 months ago

Bro, totally random but, any idea on how to implement day to day swiping?

xxwag commented 2 months ago

Yes, so you need to create an handlepan function or similar and call it from the table calendar gesture detector to interact with the whole table. Not sure if thats what you looking for.

Somtobro commented 2 months ago

Yeah, that's what I'm looking for . But there's no built bin function to call next day. For example: If I'm on the last day of the month eg 30th of April. Swiping again would go to 31 and then 32. Do you understand what I'm tryna say?

xxwag commented 2 months ago

Try the simpleSwipeConfig = const SimpleSwipeConfig( verticalThreshold: 25.0, swipeDetectionBehavior: SwipeDetectionBehavior.continuousDistinct, ),

By now you should already have something like an focused day datetime and you initialize it using date.now right? 

Swipe config would iterate based on the direction, increment the focused day and change its state to represent the change. 
Somtobro commented 2 months ago

Yeah I've figured it out. The only problem now Is I can't select too far forward or behind. For example Currently I can't scroll past August 1st Today is May 1st

xxwag commented 2 months ago

Im doing this to calculate the surrounding days and fill the exit voids, maybe you can use this substract in combination with focused month +-1 to fill the available dates to get swiped on.

  DateTime firstDayOfMonth = DateTime(_focusedDay.year, _focusedDay.month, 1);
    int daysToSubtract = (firstDayOfMonth.weekday - 1) % 7;
    DateTime firstVisibleDay =
        firstDayOfMonth.subtract(Duration(days: daysToSubtract));

    DateTime calculatedDate =
        firstVisibleDay.add(Duration(days: row * 7 + column));
Somtobro commented 2 months ago

Thanks guys I've fixed it. I came up with a very brilliant solution. I wrapped the body around a Swipe widget and I used the flutters add and subtract to go back and forth to the next or previous (changing selected and focused days) day that handles it perfectly as it checks if the the next day exists and if it's the last day of the year it goes to the new year as well. Thanks once more