Flutter---CupertinoPicker滚动选择器

效果图

概念:CupertinoPicker 是 Flutter 框架中非常核心的一个滚动选择器组件(iOS 风格的滚动列表选择控件),广泛用于 iOS 风格的选择界面(比如选择日期、时间、选项等)。

特点:

  • 支持滚轮滑动选择(类似 iPhone 设置里的时间/日期选择器)

  • 样式自动带有 惯性滚动、回弹效果、居中高亮

  • 可自定义内容(不限于文字)

它有好几个参数,可以一个一个去试试,看效果

实现代码

Dart 复制代码
  void showPickerDialog() {
    int selectedIndex = 0;
    List<String> options = ['广州', '惠州', '深圳', '茂名', '东莞',"韶关"];

    final controller = FixedExtentScrollController(initialItem: 2);//初始选中项为深圳

    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('请选择'),
          content: SizedBox(
            height: 200,

            child: CupertinoPicker(
              itemExtent: 40, //每个选项的高度
              //diameterRatio: 1.5, // 默认值 1.1,值越大透视效果越小(滑动效果)
              //magnification: 1, // 中间选项的放大倍数(放大选中的文字)
              //squeeze: 0.8, // 默认值 1.45,值越小选项间距越大
              //useMagnifier: true, // 是否使用放大镜效果(好像肉眼看不出什么效果)
              scrollController: controller,//控制初始选中项
              onSelectedItemChanged: (index) { //在滚动结束后触发
                selectedIndex = index;
              },
              children: options.map((option) {
                return Center(child: Text(option));
              }).toList(),
              //selectionOverlay: null,// 移除选择覆盖层

            ),


          ),

          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('取消'),
            ),
            TextButton(
              onPressed: () {
                print('选择了: ${options[selectedIndex]}');
                Navigator.pop(context);
              },
              child: Text('确定'),
            ),
          ],
        );
      },
    );
  }

代码实例

home_page.dart

Dart 复制代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_flutter/person_information_page.dart';


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

  @override
  State<StatefulWidget> createState() => _HomePageState();

}

class _HomePageState extends State<HomePage> {



  @override
  Widget build(BuildContext context) {
    return Scaffold(

      //2.构建UI
      body: Container(
          height: double.infinity,//覆盖高
          width: double.infinity,//覆盖宽
          padding: const EdgeInsets.symmetric(vertical: 10,horizontal:10),//边内边距
          //主页背景颜色上下渐变
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.centerRight,
              colors: [
                Colors.blue,
                Colors.white,
              ],
            ),
          ),
          child: GestureDetector(
              onTap:showPickerDialog ,//设置按钮的点击事件
              child: Column(
                children: [
                  SizedBox(height: 100,),
                  //按钮
                  Container(
                    //按钮大小
                      height: 50,
                      width: 400,
                      //按钮的样式
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Center(
                        child: Text("CupertinoPicker滚动选择组件"),
                      )
                  ),
                  SizedBox(height: 20,),

                ],
              )
          )
      ),
    );
  }




  void showPickerDialog() {
    int selectedIndex = 0;
    List<String> options = ['广州', '惠州', '深圳', '茂名', '东莞',"韶关"];

    final controller = FixedExtentScrollController(initialItem: 2);//初始选中项为深圳

    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('请选择'),
          content: SizedBox(
            height: 200,

            child: CupertinoPicker(
              itemExtent: 40, //每个选项的高度
              //diameterRatio: 1.5, // 默认值 1.1,值越大透视效果越小(滑动效果)
              //magnification: 1, // 中间选项的放大倍数(放大选中的文字)
              //squeeze: 0.8, // 默认值 1.45,值越小选项间距越大
              //useMagnifier: true, // 是否使用放大镜效果(好像肉眼看不出什么效果)
              scrollController: controller,//控制初始选中项
              onSelectedItemChanged: (index) { //在滚动结束后触发
                selectedIndex = index;
              },
              children: options.map((option) {
                return Center(child: Text(option));
              }).toList(),
              //selectionOverlay: null,// 移除选择覆盖层

            ),


          ),

          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('取消'),
            ),
            TextButton(
              onPressed: () {
                print('选择了: ${options[selectedIndex]}');
                Navigator.pop(context);
              },
              child: Text('确定'),
            ),
          ],
        );
      },
    );
  }

}
相关推荐
计算机毕业论文辅导3 天前
物联网实战:基于MQTT协议的智能家居数据传输系统设计与实现
1024程序员节
开开心心就好3 天前
支持批量处理的视频分割工具推荐
安全·智能手机·rust·pdf·电脑·1024程序员节·lavarel
liuyao_xianhui5 天前
Linux开发工具结尾 _make
linux·运维·服务器·数据结构·哈希算法·宽度优先·1024程序员节
学传打活7 天前
【边打字.边学昆仑正义文化】_21_爱的结晶(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
数据皮皮侠AI14 天前
顶刊同款!中国地级市风灾风险与损失数据集(2000-2022)|灾害 / 环境 / 经济研究必备
大数据·人工智能·笔记·能源·1024程序员节
Fab1an15 天前
Busqueda——Hack The Box 靶机
linux·服务器·学习·1024程序员节
技术专家16 天前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
学传打活18 天前
古代汉语是源,现代汉语是流,源与流一脉相承。
微信公众平台·1024程序员节·汉字·中华文化
学传打活23 天前
【边打字.边学昆仑正义文化】_19_星际生命的生存状况(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
unable code1 个月前
[HNCTF 2022 WEEK2]ez_ssrf
网络安全·web·ctf·1024程序员节