Hello,你们好这里是一个苦逼的前Android
开发,原本Android
干的开开心心,可是受到一些影响转行了😂,不过我就是吃不起饭,找不到工做也不会放弃Android
开发!,Flutter
真香!!我我的自学Flutter
以来受到了诸多大佬文章的启迪,因此也萌生了写一下分享文章给一样须要帮助的同窗们。api
TextField
文本输入框相信你们都不陌生,本篇文章带你更加仔细的学习TextField
的一切。bash
建议阅读时间: 没事能够多来看看啊😂ide
Widget build(BuildContext context) {
return Container(child: TextField());
}
复制代码
大功告成?naive,这段代码收获的是一段报错,提炼以后的报错信息学习
I/flutter ( 7877): No Material widget found.`
I/flutter ( 7877): TextField widgets require a Material widget ancestor.
I/flutter ( 7877): Material itself, such as a Card, Dialog, Drawer, or Scaffold.
复制代码
这个错误很好解决,TextField
须要被Material
组件包裹才能使用,咱们能够用Card
,Dialog
,Drawer
,Scaffold
,甚至直接用Material
。可是咱们不须要直接包裹住,只要TextField
渲染时向上查找能够找到Material
组件就ok。 ui
const TextField({
Key key,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
TextInputType keyboardType,
this.textInputAction,
this.textCapitalization = TextCapitalization.none,
this.style,
this.textAlign = TextAlign.start,
this.textDirection,
this.autofocus = false,
this.obscureText = false,
this.autocorrect = true,
this.maxLines = 1,
this.maxLength,
this.maxLengthEnforced = true,
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.down,
this.enableInteractiveSelection,
this.onTap,
this.buildCounter,
})
复制代码
inputFormatters
inputFormatters: [LengthLimitingTextInputFormatter(20)]
复制代码
inputFormatters
用来限制输入内容,咱们能够自定义一个只接受输入数字内容的inputFormatters
this
class NumberFormatter extends TextInputFormatter {
bool isNumber(String value) {
return RegExp('^[0-9]*\$').hasMatch(value);
}
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
String value = newValue.text;
return isNumber(value) ? newValue : oldValue;
}
}
复制代码
obscureText
用来隐藏输入内容,通常输入密码时须要设置这个属性。autofocus
用来设置是否自动获取焦点,默认为false
。注意:设置focusNode
后此属性失效。focusNode
用来控制TextField
焦点,也可控制软键盘弹出隐藏~
TextField
还能够用focusNode
来控制具体哪个TextField
获取焦点
focusNode
还能够获取焦点状态~FocusNode focusNode = FocusNode();
focusNode.addListener(() {
if (focusNode.hasFocus)
print('得到焦点');
else
print('失去焦点');
});
复制代码
cursorWidth
设置光标宽度cursorRadius
设置光标圆角cursorColor
设置光标颜色enabled
是否禁用输入框enableInteractiveSelection
值为true
时能够长按选择复制粘贴,false
时禁用。onTap
单击输入框的回调TextField(
onTap: () {
print('onTap()');
}
//单击时
I/flutter (14649): onTap()
I/flutter (14649): onTap()
复制代码
onSubmitted
点击软键盘确认按钮的回调 注意这里的确承认以是done
、search
、go
等TextField(
onSubmitted: (value) {
print('value:' + value);
})
I/flutter (14978): value:苏武难飞
I/flutter (14978): value:苏武难飞
复制代码
还须要注意的一点就是单独注册onSubmitted
时点击后会关闭软键盘~spa
onEditingComplete
点击软键盘确认按钮的回调😂 这个方法和上面的onSubmitted
区别在于value
回调TextField
一个负责用户名一个负责密码,用户输入完用户名后能够很天然的点击软键盘的按钮使焦点聚焦于密码框。TextField(
onEditingComplete: () {
print('onEditingComplete()');
FocusScope.of(context).requestFocus(focusNode);
},
textInputAction: TextInputAction.next),
TextField(focusNode: focusNode)
I/flutter (15170): onEditingComplete()
I/flutter (15170): onEditingComplete()
复制代码
textInputAction
用来设置软键盘图标能够设置为Search
、Next
、go
、previous
等等,须要注意的是部分InputAction
可能只有Android
平台有,部分InputAction
可能只有iOS
平台有。textCapitalization
用来控制用户输入文字大小写
textAlign
文本对齐方式 这个其实很好理解,就像原生Android
中的Gravity
,本章节就拿center
举一个小例子。
textDirection
控制文本方向style
逐渐到了重头戏了嗷~,style
用来控制输入文本样式举个栗子 3d
decoration
用来控制输入框样式 咱们先来作一个很是Material Design
的输入框~TextField(
decoration: InputDecoration(labelText: 'Name')
)
复制代码
InputDecoration
功能是很是丰富的,咱们还能够添加
border
属性来让咱们输入框更加有
Material
感受。
OutlineInputBorder
TextField(
decoration: InputDecoration(labelText: 'Name',border:OutlineInputBorder())
)
复制代码
TextField(
decoration: InputDecoration(hintText: 'Name',border: OutlineInputBorder(borderRadius:BorderRadius.all(Radius.circular(16.0)))))
复制代码
TextField(
decoration: InputDecoration(
labelText: 'Name',
labelStyle: TextStyle(color: Colors.red),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(color:Colors.red)),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color:Colors.red))))
复制代码
border
是不生效的,会被
enabledBorder
和
focusedBorder
所覆盖。
Theme
更改Theme(
data: ThemeData(primaryColor: Colors.red),
child: TextField(
decoration: InputDecoration(
labelText: 'Name', border: OutlineInputBorder())),
)
复制代码
TextField(
decoration: InputDecoration(
labelText: 'Name',
labelStyle: TextStyle(color: Colors.red),
focusedBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.red, width: 3.0)),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.red, width: 3.0))))
复制代码
UnderlineInputBorder
表现形式与默认的样式相同,更改颜色和圆角粗细也与OutlineInputBorder
一致~code
suffixIcon
能够用来作清空输入框的×
errorText
与
helperText
都有内容时,只有
errorText
会生效
Flutter
中的TextField
默认表现输入形式是单行输入,若是咱们设置了maxLines
呢?请看图😂~ orm
maxLines
以后总体的高度都被放大了,这显然不是咱们想要的效果,咱们想要的应该是输入超出一行后会自动换行,像原生
Android
的
EditText
同样,怎么作呢,其实也很简单,看图┗|`O′|┛ 嗷~~
如何处理文本输入呢,咱们有两个办法。
onChanged
回调每当用户输入时,TextField会调用它的onChanged回调。 您能够处理此回调以查看用户输入的内容。例如,若是您正在输入搜索字段,则可能须要在用户输入时更新搜索结果。
TextField(onChanged: (value) {
print('value: ${value}');
}
I/flutter (23802): value:苏
I/flutter (23802): value:苏武
I/flutter (23802): value:苏武难
I/flutter (23802): value:苏武难飞
复制代码
一个更强大(但更精细)的方法是提供一个TextEditingController做为TextField的controller属性。 在用户输入时,controller的text和selection属性不断的更新。要在这些属性更改时获得通知,请使用controller的addListener方法监听控制器 。 (若是你添加了一个监听器,记得在你的State对象的dispose方法中删除监听器 )。
TextEditingController _controller = TextEditingController();
_controller.addListener(() {
String text = _controller.text;
int length = text.length;
print('text: ${text} length:${length}');
if (length >= 5) {
_controller.clear();
}
});
I/flutter (23802): text: 苏 length:1
I/flutter (23802): text: 苏武难飞 length:4
I/flutter (23802): text: 苏武难飞帅 length:5
I/flutter (23802): text: length:0
复制代码
本篇内容到这里就结束了,虽然我再也不作Android
开发,可是我却依然喜好怀念作开发的日子。给本身定个小目标,但愿下半年能够在掘金社区有50个赞吧😂