当前位置: 首页 > news >正文

Flutter 手搓日期选择

时间选择器: 

时间段选择 在实际开发过程中还是挺常见的。Flutter 本身自带选 时间选择器器 CupertinoDatePicker,样式也是可以定义的,但是他 只提供三种时间的选择 自定义有局限性。后来开了一下 代码,实际上 内部使用的是 CupertinoPicker 实现的, 下面我们先介绍下 CupertinoDatePicker, 然后在基于 CupertinoPicker  实现一个自定义的 时间选择器:

CupertinoDatePicker:

CupertinoDatePicker 是一个仿 iOS 风格的日期选择器,属于 Cupertino(iOS 风格)组件库的一部分。它提供了一个优雅的滚动选择器

定义选择器的模式,支持以下选项:

  • CupertinoDatePickerMode.date:仅日期(年、月、日)。
  • CupertinoDatePickerMode.time:仅时间(小时、分钟)。
  • CupertinoDatePickerMode.dateAndTime:日期和时间组合。
Expanded(
  child: CupertinoTheme( //自定义样式 颜色已经字体大小
    data: CupertinoThemeData(
      barBackgroundColor: Colors.white30,
      textTheme: CupertinoTextThemeData(
        dateTimePickerTextStyle: TextStyle(
          color: CupertinoColors.white,
          fontSize: 18,
        ),
      ),
    ),
    child: CupertinoDatePicker(
      mode: CupertinoDatePickerMode.date,
      // backgroundColor: Colors.black87,//背景颜色
      initialDateTime: now,
      onDateTimeChanged: (DateTime newDate) {
        // setState(() {
        //   selectedDate = newDate;
        // });
      },
    ),
  ),
),

CupertinoPicker

基于 CupertinoPicker 滚动选择器组件的封装,样式可以高度自定义。

  • CupertinoPicker 是一个垂直滚动的选择器,用户可以通过滑动来选择其中的一项。
  • 它通常与 showCupertinoModalPopup 或其他布局结合使用,常用于弹窗或底部抽屉中。
  • 相比于 CupertinoDatePicker(专注于日期和时间选择),CupertinoPicker 更加通用,允许开发者自定义选项内容。
  • 支持无限滚动(通过 looping 属性)。
  • 支持自定义每个选项的高度和样式。
  • 提供选中项的回调函数,方便处理用户选择。
  • 内置放大镜效果(magnifier),突出显示当前选中项。
Expanded(
  child: Stack(
    children: [
      Row(
        children: [
          Expanded(
            child: CupertinoPicker(
              useMagnifier: false,
              scrollController: _scrollYearController,
              itemExtent: 100.cale,
              onSelectedItemChanged: (index) {
                //print('当前选中 年项: $index');
                _indexYear = index;
              },
              selectionOverlay: null,
              children: [
                Center(
                  child: Text(
                    '2025年',
                    style: AppTextStyle.textStyle_28_FFFFFF,
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: CupertinoPicker(
              useMagnifier: false,
              itemExtent: 100.cale,
              scrollController: _scrollMonthController,
              onSelectedItemChanged: (int value) {
                // print('当前选中 月:$value');
                _indexMonth = value;
              },
              selectionOverlay: null,
              children: List.generate(12, (index) {
                return Center(
                  child: Text(
                    '${index + 1}月',
                    style: AppTextStyle.textStyle_28_FFFFFF,
                  ),
                );
              }),
            ),
          )
        ],
      ),
      Positioned(              //选中的样式
        child: Center(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 32.cale),
            height: 100.cale,
            decoration: BoxDecoration(
              border: Border(
                top: AppWidget.borderSideLine(),
                bottom: AppWidget.borderSideLine(),
              ),
            ),
          ),
        ),
      )
    ],
  ),
)

http://www.dtcms.com/a/111487.html

相关文章:

  • 浅析联咏NT9856X各种LCD显示屏接口技术
  • 操作系统(三):FreeRTOS实时性机制分析
  • 音视频(四)android编译
  • 【2019】【论文笔记】高resolution无透镜的THz成像和测距——
  • antvX6节点全选后鼠标通过拖拉调整视图的展示位置
  • 基于springboot微信小程序的旅游攻略系统(源码+lw+部署文档+讲解),源码可白嫖!
  • 码曰编程大模型-学编程的好工具
  • 【嵌入式学习4】模块、包、内置模块、异常
  • CherryStudio MCP实战(一)filesystem篇
  • Cmake:Win10 如何编译 midifile C++应用程序
  • leetcode 数组总结篇
  • 湖北师范大学计信学院研究生课程《工程伦理》12.6章节练习
  • 离线部署kubesphere(已有k8s和私有harbor的基础上)
  • 鸿蒙 harmonyOS 网络请求
  • AWS云安全基线:构建企业级安全防护体系的完整指南
  • Ubuntu 安装 JMeter:为你的服务器配置做好准备
  • C++ - 宏基础(简单常量替换宏、函数样式的宏、多行宏、预定义宏、字符串化宏、连接宏、可变参数日志宏)
  • 深入理解C++引用:从基础到现代编程实践
  • 【设计模式】代理模式
  • 如何计算财富自由所需要的价格?
  • mapbox进阶,使用本地dem数据,加载hillshade山体阴影图层
  • 什么是缓存穿透、缓存雪崩、缓存击穿?
  • 使用VS2022远程调试Linux项目问题
  • Linux / Windows 下 Mamba / Vim / Vmamba 安装教程及安装包索引
  • 程序化广告行业(54/89):人群标签、用户标签与Look Alike原理详解
  • 鸿蒙NEXT开发随机工具类(ArkTs)
  • 【大模型基础_毛玉仁】6.5 实践与应用--RAG、Agent、LangChain
  • FPGA--HDLBits网站练习
  • 思维链、思维树、思维图与思维森林在医疗AI编程中的应用蓝图
  • ARXML文件解析-1