android - 如何仅在 Flutter 的 datepicker 中显示年份?

标签 android ios flutter dart mobile

我希望程序只要求用户输入年份。但如您所知,日期选择器要求提供完整的日期,包括月份和日期。有没有办法让用户只被问到这一年?

最佳答案

Afaik,您不能使用 datePicker 仅选择年份。但是你可以通过使用对话框和 YearPicker 来做到这一点。小部件。
这是一个示例代码,用于显示带有 YearPicker 的对话框(阅读评论):

showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text("Select Year"),
          content: Container( // Need to use container to add size constraint.
            width: 300,
            height: 300,
            child: YearPicker(
              firstDate: DateTime(DateTime.now().year - 100, 1),
              lastDate: DateTime(DateTime.now().year + 100, 1),
              initialDate: DateTime.now(),
              // save the selected date to _selectedDate DateTime variable.
              // It's used to set the previous selected date when
              // re-showing the dialog.
              selectedDate: _selectedDate,
              onChanged: (DateTime dateTime) {
                // close the dialog when year is selected.
                Navigator.pop(context);

                // Do something with the dateTime selected.
                // Remember that you need to use dateTime.year to get the year
              },
            ),
          ),
        );
      },
    );

关于android - 如何仅在 Flutter 的 datepicker 中显示年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62022135/

相关文章:

Android Studio 库项目引用未解析的符号

ios - 使用 Coredata Swift 发送到实例的无法识别的选择器

ios - 从 Swift 3 中的 Dictionary<String, Any> 中获取值

flutter - 我们可以为三星电视构建 Flutter 应用程序吗?

flutter - 检查对象是否有空值

android - 避免 Twilio SMS 中的小 url

Android::使用其他 InputFilter 以编程方式设置 EditText 的最大长度

java - 为什么 Android Volley 响应字符串这么短

ios - 仅更改 NSAttributedString 的字体大小

firebase - Cloud Firestore 不等式运算符异常抖动