import 'package:flutter/material.dart';

class FormDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('FormDemo'),
elevation: 0.0,
),
body: Theme(
data: Theme.of(context).copyWith(
primaryColor: Colors.black,
),
child: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RegisterForm(),
],
),
),
),
);
}
} class RegisterForm extends StatefulWidget {
@override
RegisterFormState createState() => RegisterFormState();
} class RegisterFormState extends State<RegisterForm> {
final registerFormKey = GlobalKey<FormState>();
String username, password;
bool autovalidate = false; void submitRegisterForm() {
if (registerFormKey.currentState.validate()) {
registerFormKey.currentState.save(); debugPrint('username: $username');
debugPrint('password: $password'); Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('Registering...'),
)
);
} else {
setState(() {
autovalidate = true;
});
}
} String validateUsername(value) {
if (value.isEmpty) {
return 'Username is required.';
} return null;
} String validatePassword(value) {
if (value.isEmpty) {
return 'Password is required.';
} return null;
} @override
Widget build(BuildContext context) {
return Form(
key: registerFormKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'Username',
helperText: '',
),
onSaved: (value) {
username = value;
},
validator: validateUsername,
autovalidate: autovalidate,
),
TextFormField(
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
helperText: '',
),
onSaved: (value) {
password = value;
},
validator: validatePassword,
autovalidate: autovalidate,
),
SizedBox(height: 32.0,),
Container(
width: double.infinity,
child: RaisedButton(
color: Theme.of(context).accentColor,
child: Text('Register', style: TextStyle(color: Colors.white)),
elevation: 0.0,
onPressed: submitRegisterForm,
),
),
],
),
);
}
} class TextFieldDemo extends StatefulWidget {
@override
TextFieldDemoState createState() => TextFieldDemoState();
} class TextFieldDemoState extends State<TextFieldDemo> {
final textEditingController = TextEditingController(); @override
void dispose() {
textEditingController.dispose();
super.dispose();
} @override
void initState() {
super.initState();
// textEditingController.text = 'hi';
textEditingController.addListener(
() {
debugPrint('input: ${textEditingController.text}');
}
);
} @override
Widget build(BuildContext context) {
return TextField(
controller: textEditingController,
// onChanged: (value) {
// debugPrint('input: $value');
// },
onSubmitted: (value) {
debugPrint('submit: $value');
},
decoration: InputDecoration(
icon: Icon(Icons.subject),
labelText: 'Title',
hintText: 'Enter the post title.',
// border: InputBorder.none,
// border: OutlineInputBorder(),
filled: true,
),
);
}
} class ThemeDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Theme.of(context).accentColor,
);
}
}

效果:

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. Kotlin对象表达式深入解析

    嵌套类与内部类巩固: 在上一次https://www.cnblogs.com/webor2006/p/11333101.html学到了Kotlin的嵌套类与内部类,回顾一下: 而对于嵌套类: 归根结底 ...

  2. keras模块学习之model层【重点学习】

    本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! model层是keras模块最重要的一个层,所以单独做下笔记,这块比较难理解,本博主自己还在学习这块,还在迷糊中. model的方法 ...

  3. IOT设备通讯,MQTT物联网协议,MQTTnet

    一.IOT设备的特性 硬件能力差(存储能力基本只有几MB,CPU频率低连使用HTTP请求都很奢侈) 系统千差万别(Brillo,mbedOS,RIOT等) 如使用电池供电,电量消耗敏感 如果是小设备, ...

  4. 从零实现jQuery的extend

    前言 jQuery 的 extend 是 jQuery 中应用非常多的一个函数,今天我们一边看 jQuery 的 extend 的特性,一边实现一个 extend! extend 基本用法 先来看看 ...

  5. 【CSP-S 2019】【洛谷P5665】划分【单调队列dp】

    前言 \(csp\)时发现自己做过类似这道题的题目 : P4954 [USACO09Open] Tower of Hay 干草塔 然后回忆了差不多\(15min\)才想出来... 然后就敲了\(88p ...

  6. Node.js安装,多版本管理以及修改npm下载的镜像源

    注意:在操作之前建议先把整个文章看完,在决定要不要配置!!!!! 1.下载    地址:http://nodejs.cn/download/        根据系统对应版本下载文件 2.安装    下 ...

  7. php使用WebUploader做大文件的分块和断点续传

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  8. learning java identityHashCode

    var S1 = new String("aaaaa"); System.out.println("SI hasCode: " + S1.hashCode()) ...

  9. mongoDB线上数据库连接报错记录

    报错信息提示:无法在第一次连接时连接到服务器 别的一切正常,经过查询得知,是因为如果电脑没设定固定IP,并且重启情况下可能会导致IP地址更改. 解决方法: 将本机ip地址添加到cluster的白名单即 ...

  10. 【一起来烧脑】读懂JQuery知识体系

    背景 在现在就业的过程中,会运用JQuery是你的加分项,那么什么是JQuery,嗯,jquery是JavaScript的函数库,是一种轻量级的JavaScript库,写得少,做的多,导致jQuery ...