简体   繁体   English

Flutter:使用 Cupertino DatePicker 的问题

[英]Flutter: Issues in using Cupertino DatePicker

I am trying to check the user's device OS and display the Cupertino date picker top iOS users, and Material date picker to android users.我正在尝试检查用户的设备操作系统并显示 Cupertino 日期选择器顶部 iOS 用户,以及材料日期选择器到 android 用户。 Below is my code下面是我的代码

Future<void> _selectDate(BuildContext context) async {
    DateTime? picked;

    if (Platform.isIOS) {
      showCupertinoModalPopup(
        context: context,
        builder: (context) {
          return Theme(
            data: ThemeData.dark(),
            child: Container(
              height: 300,
              color: Color.fromARGB(255, 255, 255, 255),
              child: CupertinoDatePicker(
                  mode: CupertinoDatePickerMode.date,
                  initialDateTime: DateTime.now(),
                  onDateTimeChanged: (val) {
                    setState(() {
                      picked = val;
                    });
                  }),
            ), // This will change to light theme.
          );
        },
      );
    } else if (Platform.isAndroid) {
      picked = await showDatePicker(
          context: context,
          initialDate: selectedDate,
          firstDate: DateTime(2015, 8),
          lastDate: DateTime(2101),
          builder: (context, child) {
            return Theme(
                data: ThemeData.dark(), // This will change to light theme.
                child: child!);
          });
    }

    if (picked != null && picked != selectedDate)
      setState(() {
        selectedDate = picked!;
        formattedSelectedDate = picked.toString();
      });
  }

My Android code is working fine.我的 Android 代码工作正常。 However I have the following issues in my iOS code.但是,我的 iOS 代码中存在以下问题。

  1. I can't close the popup我无法关闭弹出窗口
  2. This opens as a bottom sheet, but I prefer as a dialog box like in Android version.这作为底页打开,但我更喜欢像 Android 版本那样作为对话框。

How can I fix these issues?我该如何解决这些问题?

Try below code hope its helpful to you.试试下面的代码希望它对你有帮助。

Your android datePicker method:您的 android datePicker 方法:

androidDatePicker(BuildContext context) async {
    chosenDateTime = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
    );
    print(chosenDateTime);
  }

Your iOS date picker method您的 iOS 日期选择器方法

iosDatePicker(BuildContext context) {
    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext builder) {
          return Container(
            height: MediaQuery.of(context).copyWith().size.height * 0.25,
            color: Colors.white,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.date,
              onDateTimeChanged: (value) {
                chosenDateTime = value;
                print(chosenDateTime);
              },
              initialDateTime: DateTime.now(),
              minimumYear: 2000,
              maximumYear: 3000,
            ),
          );
        });
  }

Your Widget:你的小工具:

ElevatedButton(
    onPressed: () {
      if (defaultTargetPlatform == TargetPlatform.macOS ||
          defaultTargetPlatform == TargetPlatform.iOS) {
        iosDatePicker(context);
      } else {
        androidDatePicker(context);
      }
    },
    child: Text('Pick Date'),
  ),

Full example:完整示例:

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class HomeScreen extends StatelessWidget {
  DateTime? chosenDateTime;
  //android datepicker
  androidDatePicker(BuildContext context) async {
    chosenDateTime = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
    );
    print(chosenDateTime);
  }
//ios date picker
  iosDatePicker(BuildContext context) {
    showCupertinoModalPopup(
        context: context,
        builder: (BuildContext builder) {
          return Container(
            height: MediaQuery.of(context).copyWith().size.height * 0.25,
            color: Colors.white,
            child: CupertinoDatePicker(
              mode: CupertinoDatePickerMode.date,
              onDateTimeChanged: (value) {
                chosenDateTime = value;
                print(chosenDateTime);
              },
              initialDateTime: DateTime.now(),
              minimumYear: 2000,
              maximumYear: 3000,
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            if (defaultTargetPlatform == TargetPlatform.macOS ||
                defaultTargetPlatform == TargetPlatform.iOS) {
              iosDatePicker(context);
            } else {
              androidDatePicker(context);
            }
          },
          child: const Text('Pick Date'),
        ),
      ),
    );
  }
}

Refer (Android) Date Picker showDatePicker and flutter_datetime_picker参考(Android)日期选择器showDatePickerflutter_datetime_picker

Refer some packages for (iOS) Cupertino Datepicker and Time Picker aslo flutter_cupertino_date_picker and flutter_cupertino_datetime_picker参考一些包 (iOS) Cupertino Datepicker 和 Time Picker 以及flutter_cupertino_date_pickerflutter_cupertino_datetime_picker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM