大佬教程收集整理的这篇文章主要介绍了Flutter实战】文本组件及五大案例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Text是显示文本的组件,最常用的组件,没有之一。基本用法如下:
Text('老孟')
注意:Text组件一定要包裹在Scaffold组件下,否则效果如下:
文本的样式在style
中设置,类型为TextStyle
,TextStyle
中包含很多文本样式属性,下面介绍一些常用的。
设置文本大小和颜色:
Text('老孟',style: TextStyle(color: Colors.red,fontSize: 20),),
上面黑色的字体为没有设置的效果,作为对比。
设置字体粗细:
Text('老孟',style: TextStyle(fontWeight: FontWeight.bold))
字体粗细共有9个级别,为w100
至w900
,FontWeight.bold是w700
。
设置斜体:
Text('老孟',style: TextStyle(fontStyle: FontStyle.italic,))
设置自定义的字体:
pubspec.yaml
:fonts:
- family: maobi
fonts:
- asset: assets/fonts/maobi.ttf
@H_706_0@maobi:是自己对当前字体的命名,有意义即可。
asset:字体文件的目录。
使用:
Text('老孟',style: TextStyle(fontFamily: 'maobi',)),
设置对齐方式:
Container(
height: 100,width: 200,color: Colors.blue.withOpacity(.4),child: Text('老孟',textAlign: TextAlign.center),
textAlign
只是控制水平方向的对齐方式,值说明如下:
TextDirection
属性有关,如果设置TextDirection.ltr
,则左对齐,设置TextDirection.rtl
则右对齐。TextDirection
属性有关,如果设置TextDirection.ltr
,则右对齐,设置TextDirection.rtl
则左对齐。设置文本自动换行:
Container(
height: 100,child: Text('老孟,专注分享Flutter技术和应用实战',softWrap: true,)
文本超出范围时的处理:
Container(
height: 100,overflow: TextOverflow.ellipsis,)
溢出的处理方式:
设置全局字体样式:
在@H_502_32@materialApp的theme
中设置如下
@H_979_10@materialApp( title: 'Flutter Demo',theme: ThemeData( ... textTheme: TextTheme( bodyText2: TextStyle(color: Colors.red,fontSize: 24),) ),home: Scaffold( body: TextDemo(),)
Text组件默认为红色,
Text('老孟'),Text('老孟',style: TextStyle(color: Colors.blue,
RichText的属性和Text基本一样,使用如下:
RichText(
text: TextSpan(
style: DefaultTextStyle.of(context).style,children: <Inlinespan>[
TextSpan(text: '老孟',style: TextStyle(color: Colors.red)),TextSpan(text: ','),TextSpan(text: '专注分享Flutter技术和应用实战'),]),)
TextField是文本输入组件,即输入框,常用组件之一。基本用法:
TextField()
不需要任何参数,@L_197_25@最简单的文本输入组件就出来了,效果如下:
decoration
是TextField组件的装饰(外观)参数,类型是InputDecoration。
TextField(
decoration: InputDecoration(
icon: Icon(Icons.person),)
当输入框是空而且没有焦点时,labelText显示在输入框上边,当获取焦点或者不为空时labelText往上移动一点,labelStyle
参数表示文本样式,具体参考TextStyle
, 用法如下:
TextField(
decoration: InputDecoration(
labelText: '姓名:',labelStyle: TextStyle(color:Colors.red)
),)
hasFloaTingPlaceholder
参数控制当输入框获取焦点或者不为空时是否还显示labelText
,默认为true,显示。
HelperText
显示在输入框的左下部,用于提示用户,HelperStyle
参数表示文本样式,具体参考TextStyle
用法如下:
TextField(
decoration: InputDecoration(
HelperText: '用户名长度为6-10个字母',HelperStyle: TextStyle(color: Colors.bluE),HelperMaxLines: 1
),)
hintText
是当输入框为空时的提示,不为空时不在显示,用法如下:
TextField(
decoration: InputDecoration(
hintText: '请输入用户名',hintStyle: TextStyle(color: Colors.grey),hintMaxLines: 1
),)
errorText
显示在输入框的左下部,默认字体为红色,用法如下:
TextField(
decoration: InputDecoration(
errorText: '用户名输入错误',errorStyle: TextStyle(fontSize: 12),errorMaxLines: 1,errorBorder: OutlineInputBorder(borderside: Borderside(color: Colors.red)),)
prefix
系列的组件是输入框前面的部分,用法如下:
TextField(
decoration: InputDecoration(
prefixIcon: Icon(Icons.person)
),)
注意prefix和icon的区别,icon是在输入框边框的外部,而prefix在里面。
suffix和prefix相反,suffix在输入框的尾部,用法如下:
TextField(
decoration: InputDecoration(
suffixIcon: Icon(Icons.person)
),)
counter
组件统计输入框文字的个数,counter仅仅是展示效果,不具备自动统计字数的功能, 自动统计字数代码如下:
var _textFieldValue = '';
TextField(
onChanged: (value){
setState(() {
_textFieldValue = value;
});
},decoration: InputDecoration(
counterText: '${_textFieldValue.length}/32'
),)
filled
为true时,输入框将会被fillColor
填充,仿QQ登录输入框代码如下:
Container(
height: 60,width: 250,child: TextField(
decoration: InputDecoration(
fillColor: Color(0x30cccccC),filled: true,enabledBorder: OutlineInputBorder(
borderside: Borderside(color: Color(0x00FF0000)),borderRadius: BorderRadius.all(Radius.circular(100))),hintText: 'QQ号/手机号/邮箱',focusedBorder: OutlineInputBorder(
borderside: Borderside(color: Color(0x00000000)),)
controller
是输入框文本编辑的控制器,可以获取TextField的内容、设置TextField的内容,下面将输入的英文变为大写:
TextEdiTingController _controller;
@override
void initState() {
super.initState();
_controller = TextEdiTingController()
..addListener(() {
//获取输入框的内容,变为大写
_controller.text = _controller.text.toUpperCase();
});
}
@override
Widget build(BuildContext context) {
return TextField(
controller: _controller,);
}
@override
dispose() {
super.dispose();
_controller.dispose();
}
有时输入框后面带有“清除”功能,需要controller来实现。如果需要2个TextField的内容进行同步,只需要给2个TextField设置同@L_197_25@controller即可实现。
keyboardType
参数控制软键盘的类型,说明如下:
texTinputAction
参数控制软键盘右下角的按键,说明如下:
大家可能发现了,Android上显示的按钮大部分是不确定的,比如next
有的显示向右的箭头,有的显示前进,这是因为各大厂商对Android ROM定制引发的。
textCapitalization
参数是配置键盘是大写还是小写,仅支持键盘模式为text
,其他模式下忽略此配置,说明如下:
这里仅仅是控制软键盘是大写模式还是小写模式,你也可以切换大小写,系统并不会改变输入框内的内容。
textAlignVertical
表示垂直方向的对齐方式,textDirection
表示文本方向,用法如下:
TextField(
textAlignVertical: TextAlignVertical.center,textDirection: TextDirection.rtl,)
toolbarOptions
表示长按时弹出的菜单,有copy
、cut
、paste
、SELEctAll
,用法如下:
TextField(
toolbarOptions: ToolbarOptions(
copy: true,cut: true,paste: true,SELEctAll: true
),)
TextField(
showcursor: true,cursorWidth: 3,cursorRadius: Radius.circular(10),cursorColor: Colors.red,)
效果如下:
将输入框设置为密码框,只需obscureText
属性设置true即可,用法如下:
TextField(
obscureText: true,)
通过inputFormatters
可以限制用户输入的内容,比如只想让用户输入字符,设置如下:
TextField(
inputFormatters: [
WhitelisTingTexTinputFormatter(RegExp("[a-zA-Z]")),],)
onChanged
是当内容发生变化时回调,onSubmitted
是点击回车或者点击软键盘上的完成回调,onTap
点击输入框时回调,用法如下:
TextField(
onChanged: (value){
print('onChanged:$value');
},onEdiTingComplete: (){
print('onEdiTingComplete');
},onTap: (){
print('onTap');
},)
输入框右下角经常需要字数统计,除了使用上面介绍的方法外,还可以使用buildCounter
,建议使用此方法,用法如下:
TextField(
maxLength: 100,buildCounter: (
BuildContext context,{
int currentLength,int maxLength,bool isFocused,}) {
return Text(
'$currentLength/$maxLength',);
},)
动态获取焦点
FocusScope.of(context).requestFocus(_focusnodE);
_focusnode
为TextField的focusnode:
_focusnode = Focusnode();
TextField(
focusnode: _focusnode,...
)
动态失去焦点
_focusnode.unfocus();
Builder(
builder: (BuildContext context) {
RenderBox Box = context.findRenderObject();
final Shader linearGradient = LinearGradient(
colors: <Color>[Colors.purple,Colors.blue],).createShader(
Rect.fromLTWH(0.0,0.0,Box?.size?.width,Box?.size?.height));
return Text(
'老孟,专注分享Flutter技术和应用实战',style: new TextStyle(
fontSize: 18.0,fontWeight: FontWeight.bold,foreground: Paint()..shader = linearGradient),)
Builder
组件是为了计算当前Text组件大小,生成LinearGradient。
RichText(
text: TextSpan(
style: DefaultTextStyle.of(context).style,children: <Inlinespan>[
WidgetSpan(
child: Container(
margin: EdgeInsets.only(right: 10),padding: EdgeInsets.symmetric(horizontal: 10),decoration: BoxDecoration(
shape: BoxShape.rectangle,borderRadius: BorderRadius.all(Radius.circular(20)),color: Colors.bluE),child: Text(
'判断题',style: TextStyle(color: Colors.whitE),TextSpan(text: '泡沫灭火器可用于带电灭火'),)
通常在登录页面的底部会出现登录即代表同意并阅读 《服务协议》,其中《服务协议》为蓝色且可点击:
Text.rich(
TextSpan(
text: '登录即代表同意并阅读',style: TextStyle(fontSize: 11,color: Color(0xFF999999)),children: [
TextSpan(
text: '《服务协议》',fontWeight: FontWeight.bold),recognizer: TapGestureRecognizer()
..onTap = () {
print('onTap');
},)
TextField(
decoration: InputDecoration(
fillColor: Color(0x30cccccC),enabledBorder: OutlineInputBorder(
borderside: Borderside(color: Color(0x00FF0000)),hintText: '输入密码',focusedBorder: OutlineInputBorder(
borderside: Borderside(color: Color(0x00000000)),textAlign: TextAlign.center,obscureText: true,onChanged: (value) {
},)
Text.rich(
TextSpan(
text: '回复',children: [
TextSpan(
text: '@老孟:',TextSpan(
text: '你好,想知道Flutter发展前景如何?',)
老孟Flutter@L_607_169@地址(330个控件用法):http://laomengit.com
欢迎加入Flutter交流群(微信:laomENGIt)、关注公众号【老孟Flutter】:
@H_617_696@ |
以上是大佬教程为你收集整理的Flutter实战】文本组件及五大案例全部内容,希望文章能够帮你解决Flutter实战】文本组件及五大案例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。