Skip to content

Cupertino date picker does not work correctly when using minuteInterval and scrolling past the endDate #97010

@ansolantz

Description

@ansolantz

Steps to Reproduce

The Cupertino date picker does not work correctly when using minuteInterval and scrolling past the endDate

What happens: In the attached example, when scrolling past the end time of 40 minutes, it scrolls back to minute 20.

Expected: If endDate is at minute 40 and I scroll to minute 45, it should scroll back to 40.
The reason is that the scroll controller does not take the minuteInterval into account when scrolling back.

I have opened a PR here.

Code sample
class _MyHomePageState extends State<MyHomePage> {
  DateTime? selectedDate;
  DateTime now = DateTime.now();
  late DateTime startDate = DateTime(now.year, now.month, now.day, 0, 5, 0);
  late DateTime endDate = DateTime(now.year, now.month, now.day, 19, 40, 0);

  int minuteInterval = 5;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter Cupertino DatePicker"),
      ),
      body: Container(
        margin: EdgeInsets.all(40),
        width: double.infinity,
        height: MediaQuery.of(context).size.height * 0.25,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ElevatedButton(
              child: Text("Show DatePicker"),
              onPressed: () {
                showDatePicker();
              },
            ),
            Text(selectedDate == null ? "" : "$selectedDate")
          ],
        ),
      ),
    );
  }

  void showDatePicker() {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext builder) {
        return Container(
          height: MediaQuery.of(context).copyWith().size.height * 0.30,
          color: Colors.white,
          child: CupertinoDatePicker(
            initialDateTime: _adjustDateTime(DateTime.now()),
            onDateTimeChanged: (newDate) {},
            use24hFormat: false,
            minuteInterval: minuteInterval,
            minimumDate: _adjustDateTime(startDate),
            maximumDate: _adjustDateTime(endDate),
            mode: CupertinoDatePickerMode.dateAndTime,
          ),
        );
      },
    );
  }

  DateTime? _adjustDateTime(DateTime? date) {
    return date == null
        ? null
        : DateTime(date.year, date.month, date.day, date.hour,
            date.minute - (date.minute % minuteInterval), 0, 0, 0);
  }
Logs
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel unknown, 2.9.0-1.0.pre.433, on macOS 11.5.2 20G95 darwin-arm, locale en-ES)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] VS Code (version 1.63.2)
[✓] VS Code (version 1.64.0-insider)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: cupertinoflutter/packages/flutter/cupertino repositoryf: date/time pickerDate or time picker widgetsfound in release: 2.8Found to occur in 2.8found in release: 2.9Found to occur in 2.9frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions