如需转载,请注明出处:Flutter学习笔记(21)--TextField文本框组件和Card卡片组件

今天来学习下TextField文本框组件和Card卡片组件。

只要是应用程序就少不了交互,基本上所有的应用程序都会有用户名、密码输入框,搜索框等等,前面我们有写过一篇基于Form表单的输入功能,今天来看一下TextField文本框组件,文本输入是最常见的一种交互方式,TextField组件就是用来做文本输入的组件。注意这个要和Text组件区分开来,Text组件主要用于显示文本,并不能接受输入文本。

  • TextField文本框组件

TextField组件构造方法:

const TextField({
Key key,
// controller是TextField的控制器,当TextField在编辑时回调,
// 如果不设置则TextField默认创建自己的controller,重点是如果两个TextField使用一个controller,那么在一个中输入内容,另一个会同步
this.controller,
this.focusNode,//焦点控制
this.decoration = const InputDecoration(),//TextField装饰器,主要用于控制TextField的外观及提示等
TextInputType keyboardType,//键盘类型,有number、phone、emailAddress、text等
this.textInputAction,//键盘事件类型,有send、search等
this.textCapitalization = TextCapitalization.none,//
this.style,//输入文本的样式
this.strutStyle,
this.textAlign = TextAlign.start,//对齐方式,默认开始位置对齐
this.textDirection,//文本方向,默认从左到右
this.autofocus = false,//是否自动获得焦点,默认false
this.obscureText = false,//文本内容是否加密,默认false
this.autocorrect = true,//是否自动更正
this.maxLines = ,//最大行数
this.minLines,//最小行数
this.expands = false,
this.maxLength,//最大长度
this.maxLengthEnforced = true,//超过最大长度输入,是否提示错误,默认true,如果设置了false,可以继续输入但是会提示错误
this.onChanged,//内容改变时回调
this.onEditingComplete,//编辑完成时回调
this.onSubmitted,//提交时回调
this.inputFormatters,//允许输入的格式,比如只能输入数字或字母
this.enabled,//是否禁用
this.cursorWidth = 2.0,//光标宽度
this.cursorRadius,//光标圆角
this.cursorColor,//光标颜色
this.keyboardAppearance,
this.scrollPadding = const EdgeInsets.all(20.0),
this.dragStartBehavior = DragStartBehavior.start,
this.enableInteractiveSelection,
this.onTap,//点击控件时调用
this.buildCounter,
this.scrollPhysics,
})

简单实现一个登陆的功能,限制用户名输入框输入的内容长度为10位,不到10位长度,给toast提示,Demo示例:

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; void main() => runApp(DemoApp()); class DemoApp extends StatelessWidget{
final TextEditingController _useController = new TextEditingController();
final TextEditingController _pwdController = new TextEditingController();
@override
Widget build(BuildContext context) {
_useController.addListener((){
Fluttertoast.showToast(msg: '您输入的内容为:${_useController.text}');
});
return new MaterialApp(
title: 'TextField And Card Demo',
debugShowCheckedModeBanner: false,
home: new Scaffold(
appBar: AppBar(
title: new Text('TextField And Card Demo'),
),
body: new Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: ,top: ,right: ,bottom: ),
child: TextField(
controller: _useController,//绑定controller
maxLines: ,//最多一行
maxLength: ,//最多输入10个字符
autofocus: true,//自动获取焦点
textAlign: TextAlign.left,//从左到右对齐
style: new TextStyle(color: Colors.white,fontSize: 20.0),//输入内容颜色和字体大小
cursorColor: Colors.deepPurple,//光标颜色
keyboardType: TextInputType.phone,
decoration: InputDecoration(
//添加装饰效果
filled: true,//背景是否填充
fillColor: Colors.redAccent,//添加黄色填充色(filled: true必须设置,否则单独设置填充色不会生效)
helperText: '用户名',
prefixIcon: Icon(Icons.person_add),//左侧图标
suffixText: '用户名',//右侧文本提示
suffixStyle: new TextStyle(fontSize: ),//右侧提示文案字体大小
hintText: 'input user name',//hint提示文案
hintStyle: new TextStyle(color: Colors.amber),//hint提示文案字体颜色
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),//添加圆角
),
)
),
),
Padding(
padding: EdgeInsets.only(left: ,top: ,right: ,bottom: ),
child: TextField(
controller: _pwdController,//绑定controller
maxLines: ,//最多一行
maxLength: ,//最多输入10个字符
autofocus: true,//自动获取焦点
textAlign: TextAlign.left,//从左到右对齐
style: new TextStyle(color: Colors.white,fontSize: 20.0),//输入内容颜色和字体大小
cursorColor: Colors.deepPurple,//光标颜色
keyboardType: TextInputType.phone,
decoration: InputDecoration(
//添加装饰效果
filled: true,//背景是否填充
fillColor: Colors.redAccent,//添加黄色填充色(filled: true必须设置,否则单独设置填充色不会生效)
helperText: '密 码',
prefixIcon: Icon(Icons.person_add),//左侧图标
suffixText: '密 码',//右侧文本提示
suffixStyle: new TextStyle(fontSize: ),//右侧提示文案字体大小
hintText: 'input user pwd',//hint提示文案
hintStyle: new TextStyle(color: Colors.amber),//hint提示文案字体颜色
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),//添加圆角
),
)
),
),
RaisedButton(
onPressed: _loginSubmit,
child: new Text('登陆'),
)
],
),
),
);
} void _loginSubmit() {
if(_useController.text.length != ){
Fluttertoast.showToast(msg: '用户名长度为11位');
}
}
}

效果截图:

上面的各类属性设置都有很详细的注释,这里就挑几个容易踩的坑来讲一下吧!

1.多个TextField一定要对应多个controller,不然多个TextField用同一个controller的话,会导致一个输入框的内容会同步到其他输入框内:

  final TextEditingController _useController = new TextEditingController();
final TextEditingController _pwdController = new TextEditingController();

2.如果要给TextField设置背景填充色,filled和fillColor这两个属性一定要成对出现,不然你会发现设置不生效:

filled: true,//背景是否填充
fillColor: Colors.redAccent,//添加黄色填充色(filled: true必须设置,否则单独设置填充色不会生效)

3.通过controller获取输入框内输入的内容:

_useController.text

4.TextField内容变化监听,一般可用作金额输入进行动态计算操作:

onChanged: (value){
Fluttertoast.showToast(msg: value);
},

5.TextField装饰器构造方法

InputDecoration({
this.icon, //位于装饰器外部和输入框前面的图片
this.labelText, //用于描述输入框,例如这个输入框是用来输入用户名还是密码的,当输入框获取焦点时默认会浮动到上方,
this.labelStyle, // 控制labelText的样式,接收一个TextStyle类型的值
this.helperText, //辅助文本,位于输入框下方,如果errorText不为空的话,则helperText不会显示
this.helperStyle, //helperText的样式
this.hintText, //提示文本,位于输入框内部
this.hintStyle, //hintText的样式
this.hintMaxLines, //提示信息最大行数
this.errorText, //错误信息提示
this.errorStyle, //errorText的样式
this.errorMaxLines, //errorText最大行数
this.hasFloatingPlaceholder = true, //labelText是否浮动,默认为true,修改为false则labelText在输入框获取焦点时不会浮动且不显示
this.isDense, //改变输入框是否为密集型,默认为false,修改为true时,图标及间距会变小
this.contentPadding, //内间距
this.prefixIcon, //位于输入框内部起始位置的图标。
this.prefix, //预先填充的Widget,跟prefixText同时只能出现一个
this.prefixText, //预填充的文本,例如手机号前面预先加上区号等
this.prefixStyle, //prefixText的样式
this.suffixIcon, //位于输入框后面的图片,例如一般输入框后面会有个眼睛,控制输入内容是否明文
this.suffix, //位于输入框尾部的控件,同样的不能和suffixText同时使用
this.suffixText,//位于尾部的填充文字
this.suffixStyle, //suffixText的样式
this.counter,//位于输入框右下方的小控件,不能和counterText同时使用
this.counterText,//位于右下方显示的文本,常用于显示输入的字符数量
this.counterStyle, //counterText的样式
this.filled, //如果为true,则输入使用fillColor指定的颜色填充
this.fillColor, //相当于输入框的背景颜色
this.errorBorder, //errorText不为空,输入框没有焦点时要显示的边框
this.focusedBorder, //输入框有焦点时的边框,如果errorText不为空的话,该属性无效
this.focusedErrorBorder, //errorText不为空时,输入框有焦点时的边框
this.disabledBorder, //输入框禁用时显示的边框,如果errorText不为空的话,该属性无效
this.enabledBorder, //输入框可用时显示的边框,如果errorText不为空的话,该属性无效
this.border, //正常情况下的border
this.enabled = true, //输入框是否可用
this.semanticCounterText,
this.alignLabelWithHint,
})
  • Card卡片组件

Card即卡片组件块,内容可以由大多数类型的Widget构成,但通常和ListTitle一起使用,Card有一个child,但它可以是支持多个child的列、行、网格或其他小部件。默认情况下,Card将其大小缩小为0像素,你可以使用SizeBox来限制Card的大小,在Flutter中,Card具有圆角和阴影。

demo示例:

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; void main() => runApp(DemoApp()); class DemoApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
title: 'TextField And Card Demo',
home: Scaffold(
appBar: AppBar(
title: new Text('TextField And Card Demo'),
),
body: Center(
child: new SizedBox(
height: ,
child: Card(
color: Colors.white,
margin: EdgeInsets.only(left: ,top: ,right: ,bottom: ),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular()),//设置圆角
child: Column(
children: <Widget>[
new ListTile(
leading: Icon(Icons.add_circle_outline),
title: new Text('TextField And Card Demo1'),
subtitle: new Text('副标题1'),
),
new Divider(),
new ListTile(
leading: Icon(Icons.add_circle_outline),
title: new Text('TextField And Card Demo2'),
subtitle: new Text('副标题2'),
onTap: (){ },
),
new Divider(),
new ListTile(
leading: Icon(Icons.add_circle_outline),
title: new Text('TextField And Card Demo3'),
subtitle: new Text('副标题3'),
),
new Divider(),
new ListTile(
leading: Icon(Icons.add_circle_outline),
title: new Text('TextField And Card Demo4'),
subtitle: new Text('副标题4'),
),
new Divider(),
],
),
),
),
)
),
);
}
}

效果截图:

以上就是今天的学习内容啦!!!

Flutter学习笔记(21)--TextField文本框组件和Card卡片组件的更多相关文章

  1. Flutter学习笔记(11)--文本组件、图标及按钮组件

    如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 文本组件 文本组件(text)负责显示文本和定义显示样式,下表为text常见属性 Text组件属性及描述 属性名 类型 默认 ...

  2. Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...

  3. Flutter学习笔记(13)--表单组件

    如需转载,请注明出处:Flutter学习笔记(13)--表单组件 表单组件是个包含表单元素的区域,表单元素允许用户输入内容,比如:文本区域,下拉表单,单选框.复选框等,常见的应用场景有:登陆.注册.输 ...

  4. Flutter学习笔记(10)--容器组件、图片组件

    如需转载,请注明出处:Flutter学习笔记(10)--容器组件.图片组件 上一篇Flutter学习笔记(9)--组件Widget我们说到了在Flutter中一个非常重要的理念"一切皆为组件 ...

  5. Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...

  6. Flutter学习笔记(9)--组件Widget

    如需转载,请注明出处:Flutter学习笔记(9)--组件Widget 在Flutter中,所有的显示都是Widget,Widget是一切的基础,我们可以通过修改数据,再用setState设置数据(调 ...

  7. Flutter学习笔记(12)--列表组件

    如需转载,请注明出处:Flutter学习笔记(12)--列表组件 在日常的产品项目需求中,经常会有列表展示类的需求,在Android中常用的做法是收集数据源,然后创建列表适配器Adapter,将数据源 ...

  8. Flutter学习笔记(18)--Drawer抽屉组件

    如需转载,请注明出处:Flutter学习笔记(18)--Drawer抽屉组件 Drawer(抽屉组件)可以实现类似抽屉拉出和推入的效果,可以从侧边栏拉出导航面板.通常Drawer是和ListView组 ...

  9. Flutter学习笔记(24)--SingleChildScrollView滚动组件

    如需转载,请注明出处:Flutter学习笔记(23)--多 在我们实际的项目开发中,经常会遇到页面UI内容过多,导致手机一屏展示不完的情况出现,以Android为例,在Android中遇到这类情况的做 ...

随机推荐

  1. BZOJ4152 The Captain(dijkstra+巧妙建图)

    BZOJ4152 The Captain 题面很简洁: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 很明显 ...

  2. 用Python和Pandas以及爬虫技术统计历史天气

    背景 最近在计划明年从北京rebase到深圳去,所以最近在看深圳的各个方面.去年在深圳呆过一段时间,印象最深的是,深圳总是突然就下雨,还下好大的雨.对于我这种从小在南方长大但是后面又在北京呆了2年多的 ...

  3. grep -nr "Base64Decode" * 查找含有某字符的文件

    grep -nr "Base64Decode" * 查找含有某字符的文件

  4. dijkstra算法学习笔记

    dijkstra是一种单源最短路径算法,即求一个点到其他点的最短路.不能处理负边权. 最近某种广为人知的算法频繁被卡,让dijkstra逐渐成为了主流,甚至在初赛中鞭尸了SPFA(? dijkstra ...

  5. 个人永久性免费-Excel催化剂功能第100波-透视多行数据为多列数据结构

    在数据处理过程中,大量的非预期格式结构需要作转换,有大家熟知的多维转一维(准确来说应该是交叉表结构的数据转二维表标准数据表结构),也同样有一些需要透视操作的数据源,此篇同样提供更便捷的方法实现此类数据 ...

  6. HttpServlet cannot be resolved to a type 解决办法

    刚开始学习Servlet,在Eclipse中新建了一个Servlet,不过页面上报错: Httpservlet cannot be resolved to a type,显然是Eclipse找不到相应 ...

  7. 【数据库】postgresql数据库创建自增序列id的注意事项

    1.创建一张表 CREATE TABLE "public"."tt" ( "name" varchar(128), "status ...

  8. ThinkPHP 添加数据到数据库失败

    ThinkPHP 添加数据到数据库失败 一般情况下会先检查一下几个方面 检查控制器或Model名是否有误 检查需要插入的数据是否为空或者缺失参数 检查数据表名及字段名称(大部分下都是字段名有误出错的) ...

  9. web设计_6_图片/标题/说明文字布局

    这个web中常见的单元布局,最好的布局方式就是利用float布局. 其中有个很关键的问题是需要清浮动.子集浮动是无法撑开父级的高度. 目前较完善的清浮动解决方案:在浮动的父级上添加.clear,达到清 ...

  10. 从三个语言(C++,Java,.Net)的几个性能测试案例来看性能优化

    随着时间的发展,现在的虚拟机技术越来越成熟了,在有些情况下,Java,.Net等虚拟机密集计算的性能已经和C++相仿,在个别情况下,甚至还要更加优秀.本文详细分析几个性能测试案例,探讨现象背后的原因. ...