zenled / calendar_views

Collection of customisable calendar related widgets for Flutter.
https://pub.dartlang.org/packages/calendar_views
MIT License
102 stars 49 forks source link

is there a way to have your calendar start at 8am instead of the visual starting at midnight and then needing to scroll down? #17

Open bcycgcg opened 4 years ago

bcycgcg commented 4 years ago

i am trying to have the day view in the UI show starting 8am. the calendar by default goes to midnight since that's the beginning of the day view.

allanlaal commented 4 years ago

same issue

stephensamonte commented 4 years ago

You can specify a start offset in the scroll view container to the current time.

In the Init state create a ScrollController

class _DayViewExampleState extends State<DayViewExample> {
  DateTime _day0;
  DateTime _day1;

  // To specify start offset to the current time
  ScrollController _scrollController;

  @override
  void initState() {
    super.initState();

    _day0 = new DateTime.now();
    _day1 = _day0.toUtc().add(new Duration(days: 1)).toLocal();

    // The number of minutes to the current time.
    double nowInMinutes = ((_day0.hour * 60) + _day0.minute).toDouble();

    // Scroll controller offsets a certain number of pixels
    // Each minute has a height of 1 pixel
    // So, to offset to the current time, we offset the number of minutes
    _scrollController = new ScrollController(
      initialScrollOffset: nowInMinutes,
      keepScrollOffset: true,
    );
  }

In the SingleChildScrollView, add the ScrollController to apply the initial offset.

new Expanded(
              child: new SingleChildScrollView(
                controller: _scrollController,
                child: new DayViewSchedule(
                  heightPerMinute: 1.0,
                  components: <ScheduleComponent>[
                    new TimeIndicationComponent.intervalGenerated(
                      generatedTimeIndicatorBuilder:
                          _generatedTimeIndicatorBuilder,
                    ),
                    new SupportLineComponent.intervalGenerated(
                      generatedSupportLineBuilder: _generatedSupportLineBuilder,
                    ),
                    new DaySeparationComponent(
                      generatedDaySeparatorBuilder:
                          _generatedDaySeparatorBuilder,
                    ),
                    new EventViewComponent(
                      getEventsOfDay: _getEventsOfDay,
                    ),
                  ],
                ),
              ),

I made a pull request with the addition to the example project here: #27