flutter 实现表单的封装包含下拉框和输入框

ops/2024/10/22 10:56:25/

一、表单封装组件实现效果

在这里插入图片描述

//表单组件
Widget buildFormWidget(List<InputModel> formList,{required GlobalKey<FormState> formKey}) {return Form(key: formKey,child: Column(children: formList.map((item) {return Column(crossAxisAlignment: CrossAxisAlignment.start,children: [Row(children: [item.isRequired? Icon(Icons.star,size: 10,color: Theme.of(Get.context!).colorScheme.error): SizedBox(),Text(item.label,style:Theme.of(Get.context!).inputDecorationTheme.labelStyle,)],),SizedBox(height: 16,),GestureDetector(onTap: item.type == 'select'? () {showBottomSheet(item.bottomSheetList!, item.label,selectProp: item.selectProp,selectController: item.selectController,controller: item.controller);}: null,child: TextFormField(controller: item.controller,enabled: item.type == 'text',keyboardType: item.keyboardType,validator: (value) {// 添加表单验证if (item.isRequired && (value == null || value.isEmpty)) {return '请${item.type == 'select' ? '选择' : '输入'}${item.label}';}//正则表达式验证if (item.pattern.isEmpty &&(value == null || value.isEmpty)) {RegExp regex = RegExp(item.pattern);if (!regex.hasMatch(value!)) {return '请输入正确的${item.label}';}}return null;},decoration: InputDecoration(suffixIcon: item.type == 'select'? Icon(Icons.arrow_forward_ios,color: Color(0x6615171E)): null,focusedBorder: Theme.of(Get.context!).inputDecorationTheme.focusedBorder,disabledBorder: Theme.of(Get.context!).inputDecorationTheme.disabledBorder,enabledBorder: Theme.of(Get.context!).inputDecorationTheme.enabledBorder,errorBorder:Theme.of(Get.context!).inputDecorationTheme.errorBorder,errorStyle:Theme.of(Get.context!).inputDecorationTheme.errorStyle,hintText:'请${item.type == 'select' ? '选择' : '输入'}${item.label}',isDense: true,filled: true,fillColor:Theme.of(Get.context!).inputDecorationTheme.fillColor,),),),SizedBox(height: 16,),],);}).toList(),));
}//bottomSheet
void showBottomSheet(List<Map<String, dynamic>> list, String title,{Map? selectProp,RxMap<String, dynamic>? selectController,TextEditingController? controller}) {showGenderPanel(title,buildCheckList(list, (item) {controller?.text = item[selectProp?['label']];Get.back();}, props: selectProp, selected: selectController));
}// 底部弹出层
void showGenderPanel(String title, Widget sheetContent) {showModalBottomSheet(context: Get.context!,builder: (context) {return Container(height: 800,child: Column(children: [Container(// height: 100,padding: Theme.of(Get.context!).dialogTheme.actionsPadding,child: Stack(children: [Row(mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[Text(title,overflow: TextOverflow.ellipsis, // 显示省略号style: Theme.of(Get.context!).dialogTheme.titleTextStyle,),],),Positioned(right: 20,// top: 14,child: GestureDetector(onTap: () {Navigator.pop(context);},child: Icon(Icons.cancel_outlined,color: Theme.of(Get.context!).dialogTheme.iconColor),),),],)),Divider(height: 1,// color: Theme.of(Get.context!).dividerColor,),Container(// padding: EdgeInsets.all(16),child: sheetContent,)],));});
}//单选列表
Widget buildCheckList(List<Map<String, dynamic>> list, Function? onChanged,{Map? props, RxMap<String, dynamic>? selected}) {props ??= {'label': 'label', 'value': 'value'};String label = props['label'] ?? 'label';String value = props['value'] ?? 'value';return Obx(() => Container(width: Get.width,child: Column(children: list.asMap().entries.map((entry) {int index = entry.key;dynamic item = entry.value;print('渲染');return Column(children: [GestureDetector(onTap: () {selected?.value = item;if (onChanged != null) {onChanged(item);}},child: Container(width: Get.width,decoration: BoxDecoration(color: Colors.blue.withOpacity(0),),padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 16),child: Row(children: [Icon((selected?.value[value] ?? '') == item[value]? Icons.check_circle: Icons.circle_outlined,size: 22,color: (selected?.value[value] ?? '') == item[value]? Color.fromRGBO(50, 73, 223, 1): Color.fromRGBO(21, 23, 30, 0.40)),SizedBox(width: 6),Text(item[label],style: TextStyle(fontSize: 16,),),],),)),Divider(height: 1,color: index + 1 == list.length? Color.fromRGBO(128, 130, 145, 0): Color.fromRGBO(128, 130, 145, 0.20),),],);}).toList(),)));
}

二、调用方法:

 buildFormWidget(formList, formKey: formKey),

三、数据格式:

Map<String, dynamic> controllers = {'phone': TextEditingController(text: '仓库1'),'phoneSelect': <String, dynamic>{'id': '18', 'name': '仓库1'}.obs,'code': TextEditingController(text: '123'),};formList = [InputModel(label: '入库仓库',isRequired: true,type: 'select',controller: controllers['phone'],selectController: controllers['phoneSelect'],bottomSheetList: bottomSheetList,selectProp: {'label': 'name', 'value': 'id'}),InputModel(label: '入库数量',isRequired: true,keyboardType: TextInputType.number,controller: controllers['code']),];

http://www.ppmy.cn/ops/7449.html

相关文章

AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘.

问题1&#xff1a; AttributeError: module backend_interagg has no attribute FigureCanvas. 解决方案&#xff1a; import matplotlib matplotlib.use(Agg) # 选择合适的后端&#xff0c;如Aggimport matplotlib.pyplot as plt在你的代码开头加上这两行代码&#xff0c;…

Redis 逻辑过期策略设计思路

引言&#xff1a; 当我们平常使用Redis缓存的时候&#xff0c;会出现一种场景&#xff0c; redis的key到过期时间了&#xff0c;总是需要到数据库里面去查一遍数据再set回redis&#xff0c;这个时候如果数据库响应比较慢&#xff0c;那么就会造成用户等待&#xff0c;如果刚好…

Ant Design中Tree使用defaultExpandAll属性后不会默认展开所有节点怎么办?

最近做前端项目时&#xff0c;使用到了 tree 组件&#xff0c;选择使用 Ant Design 中的 tree 组件&#xff0c;默认所有节点初始时全部展开&#xff0c;使用 defaultExpandAll 属性。但是显示的时候&#xff0c;一个节点都没展开。于是调研了一下这个问题。发现有以下问题&…

深入解析Tomcat的工作流程

tomcat解析 Tomcat是一个广泛使用的开源Servlet容器&#xff0c;用于托管Java Web应用程序。理解Tomcat的工作流程对于开发人员和系统管理员来说是非常重要的。本文将深入探讨Tomcat的工作原理&#xff0c;包括请求处理、线程池管理、类加载、以及与Web服务器之间的通信。 ###…

安全访问服务边缘(SASE):网络新时代的安全与连接解决方案

随着信息技术的飞速发展&#xff0c;在企业纷纷拥抱数字业务的过程中&#xff0c;由于边缘计算、云服务、混合网络的逐渐兴起&#xff0c;使得本就漏洞百出的传统网络安全架构更加岌岌可危&#xff0c;企业和组织面临着日益复杂且多变的网络安全挑战。传统的网络安全解决方案往…

Flutter - iOS 开发者速成篇

首先 安装FLutter开发环境&#xff1a;M1 Flutter SDK的安装和环境配置 然后了解Flutter和Dart 开源电子书&#xff1a;Flutter实战 将第一章初略看一下&#xff0c;你就大概了解一下Flutter和Dart这门语言 开始学习Dart语言 作为有iOS经验的兄弟们&#xff0c;学习Dart最快…

每日一题(4.17)

目录 Leecode-16-最接近的三数之和题目示例解题思路代码实现 Leecode-面试题01.07-旋转矩阵题目示例解题思路代码实现 Leecode-16-最接近的三数之和 题目 给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数&#xff0c;使它们的和与 targe…

软件设计:UML 模型图总结

1. 相关链接 参考教程&#xff1a; https://sparxsystems.com/resources/tutorials/ https://sparxsystems.com/enterprise_architect_user_guide/15.2/model_domains/whatisuml.html Unified Modeling Language (UML) description, UML diagram examples, tutorials and r…