You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用flutter_datetime_picker滚动时触发FixedExtentScrollPhysics错误求助

问题描述

使用flutter_datetime_picker包实现手动日期选择,代码如下:

DatePicker.showDatePicker(
 context,
 showTitleActions: true,
 minTime: DateTime.now(),
 maxTime: DateTime(2024, 12, 31),
 onConfirm: (date) async {
  await FirebaseFirestore.instance
  .collection('orders')
  .doc(order['orderId'])
  .update({
   'deliverystatus': 'shipping',
   'deliverydate': date.toString(),
  });
 },
);

滚动选择器时触发渲染库错误,错误信息:

════════ Exception caught by rendering library ═════════════════════════════════
The following assertion was thrown during performLayout():
FixedExtentScrollPhysics can only be used with Scrollables that uses the FixedExtentScrollController
'package:flutter/src/widgets/list_wheel_scroll_view.dart':
list_wheel_scroll_view.dart:1
Failed assertion: line 464 pos 7: 'position is _FixedExtentScrollPosition'
解决方案

1. 快速修复滚动错误

该错误源于包内部默认使用的FixedExtentScrollPhysics未匹配对应的FixedExtentScrollController,直接指定兼容的滚动物理即可解决:

DatePicker.showDatePicker(
 context,
 showTitleActions: true,
 minTime: DateTime.now(),
 maxTime: DateTime(2024, 12, 31),
 // 添加该行修复滚动异常
 scrollPhysics: const ClampingScrollPhysics(),
 onConfirm: (date) async {
  await FirebaseFirestore.instance
  .collection('orders')
  .doc(order['orderId'])
  .update({
   'deliverystatus': 'shipping',
   'deliverydate': date.toString(),
  });
 },
);

2. 控制器的正确使用方法

若需手动控制滚轮行为(如初始化选中特定日期、监听滚动),需通过自定义PickerModel关联FixedExtentScrollController,步骤如下:

  • 创建自定义日期选择模型,继承DatePickerModel并为年、月、日维度初始化控制器:
class CustomDatePickerModel extends DatePickerModel {
  late FixedExtentScrollController yearController;
  late FixedExtentScrollController monthController;
  late FixedExtentScrollController dayController;

  CustomDatePickerModel(DateTime currentTime, DateTime minTime, DateTime maxTime)
      : super(currentTime, minTime: minTime, maxTime: maxTime) {
    // 计算初始选中项的索引
    int yearIndex = currentTime.year - minTime.year;
    int monthIndex = currentTime.month - 1;
    int dayIndex = currentTime.day - 1;

    // 初始化各维度控制器
    yearController = FixedExtentScrollController(initialItem: yearIndex);
    monthController = FixedExtentScrollController(initialItem: monthIndex);
    dayController = FixedExtentScrollController(initialItem: dayIndex);
  }

  // 重写dispose释放控制器资源
  @override
  void dispose() {
    yearController.dispose();
    monthController.dispose();
    dayController.dispose();
    super.dispose();
  }
}
  • 调用showDatePicker时传入自定义模型:
DatePicker.showDatePicker(
 context,
 showTitleActions: true,
 minTime: DateTime.now(),
 maxTime: DateTime(2024, 12, 31),
 pickerModel: CustomDatePickerModel(DateTime.now(), DateTime.now(), DateTime(2024, 12, 31)),
 onConfirm: (date) async {
  await FirebaseFirestore.instance
  .collection('orders')
  .doc(order['orderId'])
  .update({
   'deliverystatus': 'shipping',
   'deliverydate': date.toString(),
  });
 },
);

注意:自定义模型时需确保控制器初始索引与日期范围匹配,避免出现索引越界问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 15:37:56