|
| 1 | +import 'package:flutter/cupertino.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:tform/form/form.dart'; |
| 4 | + |
| 5 | +import 'package:tform/tform.dart'; |
| 6 | + |
| 7 | +import '../utils.dart'; |
| 8 | + |
| 9 | +import 'package:tform_example/widgets/photos_cell.dart'; |
| 10 | +import 'package:tform_example/widgets/verifitionc_code_button.dart'; |
| 11 | + |
| 12 | +class FormPage extends StatelessWidget { |
| 13 | + FormPage({Key key}) : super(key: key); |
| 14 | + final GlobalKey _formKey = GlobalKey<TFormState>(); |
| 15 | + |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return Scaffold( |
| 19 | + appBar: AppBar( |
| 20 | + title: Text("表单"), |
| 21 | + actions: [ |
| 22 | + FlatButton( |
| 23 | + child: Text( |
| 24 | + "提交", |
| 25 | + style: TextStyle(color: Colors.white, fontSize: 16), |
| 26 | + ), |
| 27 | + onPressed: () { |
| 28 | + //校验 |
| 29 | + List errors = (_formKey.currentState as TFormState).validate(); |
| 30 | + if (errors.length > 0) { |
| 31 | + showToast(errors.first); |
| 32 | + return; |
| 33 | + } |
| 34 | + //通过 |
| 35 | + showToast("提交成功"); |
| 36 | + }, |
| 37 | + ), |
| 38 | + ], |
| 39 | + ), |
| 40 | + body: TForm.builder( |
| 41 | + key: _formKey, |
| 42 | + rows: buildFormRows(), |
| 43 | + readOnly: false, |
| 44 | + divider: Divider( |
| 45 | + height: 0.5, |
| 46 | + thickness: 0.5, |
| 47 | + ), |
| 48 | + ), |
| 49 | + ); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +List<TFormRow> buildFormRows() { |
| 54 | + return [ |
| 55 | + TFormRow.input( |
| 56 | + enabled: false, |
| 57 | + title: "姓名", |
| 58 | + placeholder: "请输入姓名", |
| 59 | + value: "张二蛋", |
| 60 | + fieldConfig: TFormFieldConfig( |
| 61 | + height: 100, style: TextStyle(color: Colors.red, fontSize: 20)), |
| 62 | + ), |
| 63 | + TFormRow.input( |
| 64 | + title: "身份证号", |
| 65 | + placeholder: "请输入身份证号", |
| 66 | + value: "4101041991892382938293", |
| 67 | + clearButtonMode: OverlayVisibilityMode.editing, |
| 68 | + ), |
| 69 | + TFormRow.input( |
| 70 | + keyboardType: TextInputType.number, |
| 71 | + title: "预留手机号", |
| 72 | + placeholder: "请输入手机号", |
| 73 | + maxLength: 11, |
| 74 | + requireMsg: "请输入正确的手机号", |
| 75 | + requireStar: true, |
| 76 | + validator: (row) { |
| 77 | + return RegExp( |
| 78 | + r'^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$') |
| 79 | + .hasMatch(row.value); |
| 80 | + }, |
| 81 | + ), |
| 82 | + TFormRow.input( |
| 83 | + title: "验证码", |
| 84 | + placeholder: "请输入验证码", |
| 85 | + suffixWidget: (context, row) { |
| 86 | + return VerifitionCodeButton( |
| 87 | + title: "获取验证码", |
| 88 | + seconds: 60, |
| 89 | + onPressed: () { |
| 90 | + showToast("验证码已发送"); |
| 91 | + }, |
| 92 | + ); |
| 93 | + }, |
| 94 | + ), |
| 95 | + TFormRow.selector( |
| 96 | + title: "学历", |
| 97 | + placeholder: "请选择", |
| 98 | + options: ["小学", "初中", "高中", "专科", "本科", "硕士及以上"], |
| 99 | + ), |
| 100 | + TFormRow.multipleSelector( |
| 101 | + title: "家庭成员", |
| 102 | + placeholder: "请选择", |
| 103 | + options: ["父亲", "母亲", "女儿", "儿子"], |
| 104 | + ), |
| 105 | + TFormRow.customSelector( |
| 106 | + title: "婚姻状况", |
| 107 | + placeholder: "请选择", |
| 108 | + state: [ |
| 109 | + ["未婚", "已婚"], |
| 110 | + [ |
| 111 | + TFormRow.input( |
| 112 | + title: "配偶姓名", placeholder: "请输入配偶姓名", requireStar: true), |
| 113 | + TFormRow.input( |
| 114 | + title: "配偶电话", placeholder: "请输入配偶电话", requireStar: true) |
| 115 | + ] |
| 116 | + ], |
| 117 | + onTap: (context, row) async { |
| 118 | + String value = await showPicker(row.state[0], context); |
| 119 | + if (value == "已婚") { |
| 120 | + TForm.of(context).insert(row, row.state[1]); |
| 121 | + } else { |
| 122 | + TForm.of(context).delete(row.state[1]); |
| 123 | + } |
| 124 | + return value; |
| 125 | + }, |
| 126 | + ), |
| 127 | + TFormRow.customSelector( |
| 128 | + title: "出生年月", |
| 129 | + placeholder: "请选择", |
| 130 | + onTap: (context, row) async { |
| 131 | + return showPickerDate(context); |
| 132 | + }, |
| 133 | + ), |
| 134 | + TFormRow.customCell( |
| 135 | + widget: Container( |
| 136 | + color: Colors.grey[200], |
| 137 | + height: 48, |
| 138 | + width: double.infinity, |
| 139 | + alignment: Alignment.center, |
| 140 | + child: Text("------ 我是自定义的Cell ------")), |
| 141 | + ), |
| 142 | + TFormRow.customCellBuilder( |
| 143 | + title: "房屋照片", |
| 144 | + state: [ |
| 145 | + {"picurl": ""}, |
| 146 | + {"picurl": ""}, |
| 147 | + {"picurl": ""} |
| 148 | + ], |
| 149 | + requireMsg: "请完成上传房屋照片", |
| 150 | + validator: (row) { |
| 151 | + return row.state.every((element) => (element["picurl"].length > 0)); |
| 152 | + }, |
| 153 | + widgetBuilder: (context, row) { |
| 154 | + return CustomPhotosWidget(row: row); |
| 155 | + }, |
| 156 | + ), |
| 157 | + ]; |
| 158 | +} |
0 commit comments