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

Flutter:封装一个自用的bottom_picker选择器

效果图:单列选择器
在这里插入图片描述
使用bottom_picker: ^2.9.0实现,单列选择器,官方文档

pubspec.yaml

# 底部选择
bottom_picker: ^2.9.0

picker_utils.dart

AppTheme:自定义的颜色
TextWidget.body = Text()
<Widget>[].toRow = Row()

下边代码中用到了一些扩展,需要自行替换这些内容

import 'package:bottom_picker/bottom_picker.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:xiaoshukeji/common/index.dart';class PickerUtils {/// 底部单列选择器/// [context] 上下文/// [title] 选择器标题/// [items] 数据列表 [{'id': 1, 'name': '选项1'}]/// [onConfirm] 确认回调 返回选中项static void showPicker({required BuildContext context,required String title,required List<Map<String, dynamic>> items,required Function(Map<String, dynamic>) onConfirm,}) {BottomPicker(pickerTitle: TextWidget.body(title,weight: FontWeight.w600,size: 30.sp,),items: items.map((item) => Center(child: TextWidget.body(item['name'].toString(),size: 28.sp,),),).toList(),buttonPadding: 0,buttonWidth: 690.w,titleAlignment: Alignment.center,backgroundColor: AppTheme.blockBgColor,pickerTextStyle: TextStyle(fontSize: 28.sp,color: AppTheme.textColorfff,),closeIconColor: AppTheme.textColorfff,buttonAlignment: MainAxisAlignment.center,displaySubmitButton: true,buttonContent: <Widget>[TextWidget.body('确定',weight: FontWeight.w600,size: 26.sp,),].toRow(mainAxisAlignment: MainAxisAlignment.center).card(color: AppTheme.primaryYellow).tight(height: 80.w,),buttonStyle: BoxDecoration(color: Colors.transparent,borderRadius: BorderRadius.circular(44.w),),onSubmit: (index) {onConfirm(items[index]);},).show(context);}
}

使用

// 记得import  PickerUtils 文件// 模型列表数据,默认接口返回了id和name字段
final List<Map<String, dynamic>> modelList = [{'id': 1, 'name': '模型1'},{'id': 2, 'name': '模型2'},{'id': 3, 'name': '模型3'},
];// 选择
void showModelPicker(BuildContext context) {PickerUtils.showPicker(context: context,title: '选择模型',items: modelList,onConfirm: (selected) {modelController.text = selected['name'];update(['strategy_add']);},);
}// 页面中调用
onTap((){controller.showPairPicker(context);
}),
http://www.lryc.cn/news/517775.html

相关文章:

  • Group3r:一款针对活动目录组策略安全的漏洞检测工具
  • 支持向量机算法(一):像讲故事一样讲明白它的原理及实现奥秘
  • 力扣-数组-35 搜索插入位置
  • List ---- 模拟实现LIST功能的发现
  • HashMap和HashTable区别问题
  • mysql -> 达梦数据迁移(mbp大小写问题兼容)
  • leetcode热门100题1-4
  • 作业:IO:day2
  • UVM: TLM机制
  • flink的EventTime和Watermark
  • arcgis的合并、相交、融合、裁剪、联合、标识操作的区别和使用
  • 【Leetcode 热题 100】20. 有效的括号
  • 比较procfs 、 sysctl和Netlink
  • Leetcode 3413. Maximum Coins From K Consecutive Bags
  • MakeFile使用指南
  • 矩阵碰一碰发视频的视频剪辑功能源码搭建,支持OEM
  • VB.NET CRC32 校验
  • 冒充者综合征上线了
  • 【大模型】百度千帆大模型对接LangChain使用详解
  • Redis相关面试
  • 使用强化学习训练神经网络玩俄罗斯方块
  • java中的日期处理:只显示日期,不显示时间的两种处理方式
  • 腾讯云AI代码助手编程挑战赛——贪吃蛇小游戏
  • 水水水水水
  • Spring整合SpringMVC
  • 【Rust自学】10.4. trait Pt.2:trait作为参数和返回类型、trait bound
  • 嵌入式系统 (2.嵌入式硬件系统基础)
  • Linux 下 Vim 环境安装踩坑问题汇总及解决方法(重置版)
  • OpenAI 故障复盘 - 阿里云容器服务与可观测产品如何保障大规模 K8s 集群稳定性
  • 安卓触摸对焦