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

Flutter Form组件的基本使用

Form组件是一个可以包含一个或多个子元素的容器类组件。
TextFormField组件是Form中常用于做用户输入的组件。

GlobalKey的currentState调用validate(),会触发TextFormField组件的validator。然后再调用save(),会触发TextFormField组件的onSaved,这是用变量就会被赋值。

简单示例

请添加图片描述

class MyState extends State {final GlobalKey<FormState> _formKey = GlobalKey<FormState>();late String userName;late String psd;void initState() {super.initState();}Widget build(BuildContext context) {Form from = Form(key: _formKey,child: Column(children: [TextFormField(decoration: InputDecoration(labelText: "用户名"),onSaved: (newValue) {userName = newValue!;},validator: (value) {return value!.trim().isNotEmpty ? null : "用户名不能为空";},),TextFormField(obscureText: true,decoration: InputDecoration(labelText: "密码"),onSaved: (newValue) {psd = newValue!;},),Row(mainAxisAlignment: MainAxisAlignment.spaceAround,children: [TextButton(onPressed: () {if (_formKey.currentState!.validate()) {_formKey.currentState?.save();print(userName);print(psd);}},child: Text("登录"),),TextButton(onPressed: () {_formKey.currentState?.reset();},child: Text("重置"),),],),],),);return Scaffold(appBar: AppBar(title: Text("登录")),body: from,);}
}
http://www.dtcms.com/a/333103.html

相关文章:

  • 【Python办公】Excel转json(极速版)-可自定义累加字段(如有重复KEY)
  • 力扣top100(day04-06)--贪心算法
  • LeetCode 53.最大子数组和:贪心算法下的连续子数组最优解
  • Android项目中Ktor的引入与使用实践
  • mlir clone
  • 【C#补全计划】事件
  • 【C#】 GridControl与GridView、容器和视图
  • Spring事务 概念 配置 隔离级别 脏读幻读不可重复读 传播行为
  • pyinstaller-从安装到高级使用
  • align-content 设置侧轴上的子元素的排列方式(多行)
  • Git代码版本管理
  • OpenCV---getStructuringElement 结构元素获取
  • 设计心得——如何架构选型
  • ffmpeg 安装、配置与使用完全指南
  • 自学大语言模型之Transformer的Tokenizer
  • jenkins 自动部署
  • 开发Chrome/Edge插件基本流程
  • mysql中in 和 exists 区别
  • 从传感器到大模型:Jetson Thor + LLM.VLA + Holoscan 的边缘推理全链路实战
  • 基于改进Apriori算法的Web文档聚类方法研究(一)
  • 20250815给ubuntu22.04.5的系统缩小/home分区
  • Doris FE 应急恢复手册:六大经典故障场景与解决方案
  • WITRAN:基于改进的RNN时间序列预测模型
  • rent8 安装部署教程之 Windows
  • Effective C++ 条款43:学习处理模板化基类内的名称
  • 俄罗斯信封套娃问题-二维最长递增子序列
  • 【JavaEE】多线程 -- 线程安全
  • UI-TARS-Desktop 深度解析:下一代智能自动化桌面平台
  • Stagehand深度解析:从开源自动化工具到企业级RPA平台的演进之路
  • 神经网络 小土堆pytorch记录