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


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

    在这里插入图片描述

    //表单组件
    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(),
          )));
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223

    二、调用方法:

     buildFormWidget(formList, formKey: formKey),
    
    • 1

    三、数据格式:

    
      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']),
        ];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    (附源码)springboot学生宿舍管理系统 毕业设计 211955
    20.Memento备忘录(行为型模式)
    基于c++的简易web服务器搭建(初尝socket编程)
    Java计算机毕业设计铜仁学院毕业就业管理系统源码+系统+数据库+lw文档
    黑客嫌100万美元太少,上市企业敏感数据遭泄露
    【JavaScript 漫游】【036】CORS 通信总结
    我用 GitHub 9.8k 的 Go 语言 2D 游戏引擎写了个游戏
    Spring的声明式事务
    新版Java面试专题视频教程——多线程篇②
    基于PHP的幸福街道的宠物管理平台的设计与开发
  • 原文地址:https://blog.csdn.net/weixin_40466351/article/details/137929059