1. import 'package:flutter/material.dart';
  2.  
  3. class FormDemo extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. appBar: AppBar(
  8. title: Text('FormDemo'),
  9. elevation: 0.0,
  10. ),
  11. body: Theme(
  12. data: Theme.of(context).copyWith(
  13. primaryColor: Colors.black,
  14. ),
  15. child: Container(
  16. padding: EdgeInsets.all(16.0),
  17. child: Column(
  18. mainAxisAlignment: MainAxisAlignment.center,
  19. children: <Widget>[
  20. RegisterForm(),
  21. ],
  22. ),
  23. ),
  24. ),
  25. );
  26. }
  27. }
  28.  
  29. class RegisterForm extends StatefulWidget {
  30. @override
  31. RegisterFormState createState() => RegisterFormState();
  32. }
  33.  
  34. class RegisterFormState extends State<RegisterForm> {
  35. final registerFormKey = GlobalKey<FormState>();
  36. String username, password;
  37. bool autovalidate = false;
  38.  
  39. void submitRegisterForm() {
  40. if (registerFormKey.currentState.validate()) {
  41. registerFormKey.currentState.save();
  42.  
  43. debugPrint('username: $username');
  44. debugPrint('password: $password');
  45.  
  46. Scaffold.of(context).showSnackBar(
  47. SnackBar(
  48. content: Text('Registering...'),
  49. )
  50. );
  51. } else {
  52. setState(() {
  53. autovalidate = true;
  54. });
  55. }
  56. }
  57.  
  58. String validateUsername(value) {
  59. if (value.isEmpty) {
  60. return 'Username is required.';
  61. }
  62.  
  63. return null;
  64. }
  65.  
  66. String validatePassword(value) {
  67. if (value.isEmpty) {
  68. return 'Password is required.';
  69. }
  70.  
  71. return null;
  72. }
  73.  
  74. @override
  75. Widget build(BuildContext context) {
  76. return Form(
  77. key: registerFormKey,
  78. child: Column(
  79. children: <Widget>[
  80. TextFormField(
  81. decoration: InputDecoration(
  82. labelText: 'Username',
  83. helperText: '',
  84. ),
  85. onSaved: (value) {
  86. username = value;
  87. },
  88. validator: validateUsername,
  89. autovalidate: autovalidate,
  90. ),
  91. TextFormField(
  92. obscureText: true,
  93. decoration: InputDecoration(
  94. labelText: 'Password',
  95. helperText: '',
  96. ),
  97. onSaved: (value) {
  98. password = value;
  99. },
  100. validator: validatePassword,
  101. autovalidate: autovalidate,
  102. ),
  103. SizedBox(height: 32.0,),
  104. Container(
  105. width: double.infinity,
  106. child: RaisedButton(
  107. color: Theme.of(context).accentColor,
  108. child: Text('Register', style: TextStyle(color: Colors.white)),
  109. elevation: 0.0,
  110. onPressed: submitRegisterForm,
  111. ),
  112. ),
  113. ],
  114. ),
  115. );
  116. }
  117. }
  118.  
  119. class TextFieldDemo extends StatefulWidget {
  120. @override
  121. TextFieldDemoState createState() => TextFieldDemoState();
  122. }
  123.  
  124. class TextFieldDemoState extends State<TextFieldDemo> {
  125. final textEditingController = TextEditingController();
  126.  
  127. @override
  128. void dispose() {
  129. textEditingController.dispose();
  130. super.dispose();
  131. }
  132.  
  133. @override
  134. void initState() {
  135. super.initState();
  136. // textEditingController.text = 'hi';
  137. textEditingController.addListener(
  138. () {
  139. debugPrint('input: ${textEditingController.text}');
  140. }
  141. );
  142. }
  143.  
  144. @override
  145. Widget build(BuildContext context) {
  146. return TextField(
  147. controller: textEditingController,
  148. // onChanged: (value) {
  149. // debugPrint('input: $value');
  150. // },
  151. onSubmitted: (value) {
  152. debugPrint('submit: $value');
  153. },
  154. decoration: InputDecoration(
  155. icon: Icon(Icons.subject),
  156. labelText: 'Title',
  157. hintText: 'Enter the post title.',
  158. // border: InputBorder.none,
  159. // border: OutlineInputBorder(),
  160. filled: true,
  161. ),
  162. );
  163. }
  164. }
  165.  
  166. class ThemeDemo extends StatelessWidget {
  167. @override
  168. Widget build(BuildContext context) {
  169. return Container(
  170. color: Theme.of(context).accentColor,
  171. );
  172. }
  173. }

效果:

flutter Form表单的更多相关文章

  1. Flutter Form表单控件超全总结

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 Form.FormField.TextFormField是 ...

  2. form表单验证-Javascript

    Form表单验证: js基础考试内容,form表单验证,正则表达式,blur事件,自动获取数组,以及css布局样式,动态清除等.完整代码如下: <!DOCTYPE html PUBLIC &qu ...

  3. Form 表单提交参数

    今天因为要额外提交参数数组性的参数给form传到后台而苦恼了半天,结果发现,只需要在form表单对应的字段html空间中定义name = 后台参数名 的属性就ok了. 后台本来是只有模型参数的,但是后 ...

  4. form表单 ----在路上(15)

    form 表单就是将用户的信息提交到服务器,服务器会将信息存储活着根据信息查询数据进行增删改查,再将其返回给用户. 基本格式: <form action="" method ...

  5. form表单的字符串进行utf-8编码

    <form>表单有assept-charset属性.该属性规定字符的编码方式,默认是"unknown",与文档的字符集相同. 该属性除了Internet explore ...

  6. 细说 Form (表单)

    细说 Form (表单) Form(表单)对于每个WEB开发人员来说,应该是再熟悉不过的东西了,可它却是页面与WEB服务器交互过程中最重要的信息来源. 虽然Asp.net WebForms框架为了帮助 ...

  7. 通过form表单的形式下载文件。

    在项目中遇到问题,要求动态拼接uri下载文件.但是由于项目的安全拦截导致window.location.href 和 window.open等新建窗口的方法都不行. 无意间百度到了通过form表单来下 ...

  8. form 表单跨域提交

    <!DOCTYPE html><html> <head> <title>form 表单上传文件</title> <script src ...

  9. form表单的属性标签

    form表单的常用标签 表单: <form id="" name="" method="post/get" action=" ...

随机推荐

  1. Bias vs. Variance(1)--diagnosing bias vs. variance

    我们的函数是有high bias problem(underfitting problem)还是 high variance problem(overfitting problem),区分它们很得要, ...

  2. 动态加载 ShellCode绕过杀软

    反病毒解决方案用于检测恶意文件,并且通常使用静态分析技术来区分二进制文件的好坏.如果是恶意文件本身包含恶意内容(ShellCode),那么依靠静态分析技术会非常有效,但如果攻击者使用轻量级的stage ...

  3. Hdfs读写数据出错

    1.Hdfs读数据出错:若在读数据的过程中,客户端和DataNode的通信出现错误,则会尝试连接下一个 包含次文件块的DataNode.同时记录失败的DataNode,此后不再被连接. 2.Hdfs在 ...

  4. [TypeScript] Modifier

    TypeScript 2.8 adds the ability for a mapped type to either add or remove a particular modifier. Spe ...

  5. Apache Solr Velocity模板远程代码执行

    更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. 这里用的docker环境 很简单的 在这里不再介绍 本地搭建好环境然后访问8983端口 网页如下: 查下节点名称 同样名字可以访问http ...

  6. Windows下 Python 2 与 Python 3 共存

    转自:http://lovenight.github.io/2016/09/27/Windows%E4%B8%8B-Python-2-%E4%B8%8E-Python-3-%E5%85%B1%E5%A ...

  7. (10)打鸡儿教你Vue.js

    事件处理器 <div id="app"> <button v-on:click="counter += 1">增加 1</butt ...

  8. 一次 react-router 中遇到的小坑

    react-router Link 标签不生效的问题 废话不多说, 直接上问题, 排解过程和答案 现象: 发现 使用 Link 标签没有 元素的样式和效果, 也不能进行跳转 代码如下: render( ...

  9. Xshell6如何传输文件

    Xshell6如何传输文件  /或者直接在本地用notepad  nftp插件上传本地文件,直观,更方便 上传文件 1.打开xshell6软件,连接服务器. 2.yum安装一款工具.#yum inst ...

  10. Tkinter 之ProgressBar进度条标签

    一.参数说明 参数 作用 cursor 鼠标位于进度条内时的形状 length 进度条长度 maximum 进度条最大刻度值 mode  进度条的模式.有两种:‘determinate’和’indet ...