aleksanderwozniak / table_calendar

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

Focused date behaviour inconsistent if `firstDay` changes #866

Open hrafnthor opened 5 months ago

hrafnthor commented 5 months ago

Describe the bug In cases where the firstDay can change from initial value (for example when fetching the calendar range over the network, and initial range is given as [today, today]), the focusedDay will be overlooked and the focused moved to the period where the new firstDay is.

The following code snippet will showcase the behaviour:


class Data {
  Data({
    required this.focusedDay,
    required this.firstDay,
    required this.lastDay,
  });

  final DateTime focusedDay;

  final DateTime firstDay;

  final DateTime lastDay;
}

class WidgetTest extends StatelessWidget {
  const WidgetTest({super.key});

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Data>(
        initialData: Data(
          focusedDay: DateTime.now(),
          firstDay: DateTime.now(),
          lastDay: DateTime.now()
        ),
        future: Future.delayed(
          const Duration(seconds: 5),
          () => Data(
            focusedDay: DateTime.now(),
            firstDay: DateTime(2024, 1, 1),
            lastDay: DateTime(2024, 12, 31),
          ),
        ),
        builder: (c, s) {
          if (s.hasData) {
            final data = s.requireData;
            return TableCalendar(
              headerVisible: true,
              firstDay: data.firstDay,
              lastDay: data.lastDay,
              focusedDay: data.focusedDay,
              selectedDayPredicate: (day) => day.sameDate(data.focusedDay),
              calendarFormat: CalendarFormat.week,
            );
          }
          return SizedBox();
        });
  }
}

extension DateTimeExtension on DateTime {
  bool sameDate(DateTime other) =>
      year == other.year && month == other.month && day == other.day;
}

This will display a TableCalendar with today's date focused, until after 5 seconds when the future delivers values with different start and end dates (but the same focused date). At that point the calendar jumps to the starting date rather than using the focused date.

Expected behavior The set focusedDay should always be the controlling factor for where the focus is at, no matter what values are given by firstDay or lastDay.

System

Flutter: 3.19.5 (stable) table_calendar: 3.1.1

niemsie commented 4 months ago

This is the same issue as we are having here: https://github.com/aleksanderwozniak/table_calendar/issues/842