You need to enable JavaScript to run this app. 如何在Flutter中实现适用于久远日期选择的Mobile Input Picker
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter中实现适用于久远日期选择的Mobile Input Picker

滚轮式出生日期选择器实现方案

你需要的滚轮交互日期选择器可通过Flutter官方自带的CupertinoDatePicker组件实现,该组件默认是滚轮选择交互,非常适合选择出生日期这类跨度较大的历史日期,无需额外引入第三方依赖,安卓、iOS平台都可正常渲染。

直接集成实现(表单场景适配)

以下是点击输入框弹出底部选择器的完整示例,可直接嵌入表单使用:

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

class BirthDateForm extends StatefulWidget {
  const BirthDateForm({super.key});

  @override
  State<BirthDateForm> createState() => _BirthDateFormState();
}

class _BirthDateFormState extends State<BirthDateForm> {
  final TextEditingController _birthDateController = TextEditingController();
  // 可自定义日期显示格式
  final DateFormat _dateFormat = DateFormat('yyyy年MM月dd日');
  DateTime? _selectedDate;

  void _showDatePicker() {
    showCupertinoModalPopup(
      context: context,
      builder: (context) => Container(
        height: 260,
        color: Colors.white,
        child: Column(
          children: [
            // 顶部操作栏
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              height: 44,
              alignment: Alignment.centerRight,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  TextButton(
                    onPressed: () => Navigator.pop(context),
                    child: const Text('取消', style: TextStyle(color: Colors.grey)),
                  ),
                  TextButton(
                    onPressed: () {
                      if (_selectedDate != null) {
                        _birthDateController.text = _dateFormat.format(_selectedDate!);
                      }
                      Navigator.pop(context);
                    },
                    child: const Text('确定'),
                  ),
                ],
              ),
            ),
            // 滚轮日期选择区域
            Expanded(
              child: CupertinoDatePicker(
                // 仅显示年月日,隐藏时间选项
                mode: CupertinoDatePickerMode.date,
                // 最早可选日期,可按需调整
                minimumDate: DateTime(1900),
                // 最晚可选日期为当前日期,避免选到未来日期
                maximumDate: DateTime.now(),
                initialDateTime: DateTime(2000, 1, 1),
                onDateTimeChanged: (DateTime value) {
                  _selectedDate = value;
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Form(
      child: TextFormField(
        controller: _birthDateController,
        decoration: const InputDecoration(
          labelText: '出生日期',
          hintText: '请选择出生日期',
          border: OutlineInputBorder(),
        ),
        readOnly: true,
        onTap: _showDatePicker,
        // 表单校验逻辑
        validator: (value) {
          if (value == null || value.isEmpty) {
            return '请填写出生日期';
          }
          return null;
        },
      ),
    );
  }
}

复用现有DateTimePicker代码方案

如果你想沿用已找到的DateTimePicker组件代码,可通过自定义pickerBuilder参数切换为滚轮选择模式:

DateTimePicker(
  initialValue: '',
  firstDate: DateTime(1900),
  lastDate: DateTime.now(),
  dateLabelText: '出生日期',
  type: DateTimePickerType.date,
  // 替换默认选择器为Cupertino滚轮样式
  pickerBuilder: (context, onChanged, child) {
    return CupertinoDatePicker(
      mode: CupertinoDatePickerMode.date,
      minimumDate: DateTime(1900),
      maximumDate: DateTime.now(),
      onDateTimeChanged: onChanged,
    );
  },
  onChanged: (val) => print(val),
  validator: (val) {
    if (val == null || val.isEmpty) {
      return '请选择出生日期';
    }
    return null;
  },
  onSaved: (val) => print(val),
);

如果你需要完全符合Material设计风格的滚轮选择器,可基于Flutter社区开源的滚轮选择组件封装,核心逻辑和上述实现一致。


内容的提问来源于stack exchange,提问作者White Tiger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 02:54:10