Flutter之TextField组件

/**
    const TextField({
    Key key,
    this.controller,//控制器,TextField的相关信息存储在里面
    this.focusNode,
    this.decoration = const InputDecoration(),//输入器装饰
    TextInputType keyboardType, //弹出键盘的类型
    this.textInputAction,//更改TextField的textInputAction可以更改键盘右下角的操作按钮,搜索,完成
    this.textCapitalization = TextCapitalization.none,//户输入中的字母大写的选项,TextCapitalization.sentences每个句子的首字母大写,TextCapitalization.characters:句子中的所有字符都大写,TextCapitalization.words : 将每个单词的首字母大写。
    this.style,
    this.textAlign = TextAlign.start, //文字显示位置
    this.autofocus = false,//自动获取焦点
    this.obscureText = false,//是否隐藏输入,true密码样式显示,false明文显示
    this.autocorrect = true,
    this.maxLines = 1,//编辑框最多显示行数
    this.maxLength,//输入最大长度,并且默认情况下会将计数器添加到TextField
    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),
    }) : assert(textAlign != null),
    assert(autofocus != null),
    assert(obscureText != null),
    assert(autocorrect != null),
    assert(maxLengthEnforced != null),
    assert(scrollPadding != null),
    assert(maxLines == null || maxLines > 0),
    assert(maxLength == null || maxLength > 0),
    keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),
    super(key: key);
 */
/**
    const InputDecoration({
    this.icon,//输入框外侧左面的Icon
    this.labelText,//Material Design风格的输入提示
    this.labelStyle,//设置labeltext的样式
    this.helperText,//显示在输入的下面
    this.helperStyle,
    this.hintText,//普通的输入提示
    this.hintStyle,
    this.errorText,//显示在输入的下面,输入框会变成红色
    this.errorStyle,
    this.errorMaxLines,//错误提示最多显示的行数
    this.isDense,
    this.contentPadding,//显示内容的padding
    this.prefixIcon,//输入框内侧左面的Icon
    this.prefix,//输入框内侧左面的Widget
    this.prefixText,//输入框内侧左面的Text
    this.prefixStyle,//设置prefixText的样式
    this.suffixIcon,//输入框内侧右面的Icon
    this.suffix,//输入框内侧右面的Widget
    this.suffixText,//输入框内侧右面的Text
    this.suffixStyle,//设置suffixText的样式
    this.counterText,//输入框右下角的计数器文本
    this.counterStyle,
    this.filled,//是否显示输入框背景色,true显示,false不显示
    this.fillColor,
    this.errorBorder,
    this.focusedBorder,
    this.focusedErrorBorder,
    this.disabledBorder,
    this.enabledBorder,
    this.border,//InputBorder.none没有边框,UnderlineInputBorder下边框,OutlineInputBorder四周都有边框
    this.enabled = true,
    this.semanticCounterText,
    }) : assert(enabled != null),
    assert(!(prefix != null && prefixText != null), 'Declaring both prefix and prefixText is not allowed'),
    assert(!(suffix != null && suffixText != null), 'Declaring both suffix and suffixText is not allowed'),
    isCollapsed = false;
 */
body: ListView(
          padding: EdgeInsets.all(10.0),
          children: <Widget>[
            TextField(
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(5.0),
                labelText: "labelText",
              ),
            ),
            TextField(
              decoration: InputDecoration(
                helperText: "helperText",
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "更改光标颜色、宽度、半径",
              ),
              cursorColor: Color(0xffff0000),
              cursorWidth: 4.0,
              cursorRadius: Radius.circular(2.0),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "enabled: false 编辑框不可用",
              ),
              enabled: false,
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "textInputAction",
                ),
                textInputAction: TextInputAction.newline
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "onChanged输入监听,onEditingComplete当用户提交时调用,onSubmitted文字提交触发(键盘按键),controller编辑框的相关信息存储在里面",
                ),
                textInputAction: TextInputAction.go,
                onChanged: (text) => print("onChanged:$text"),
                onSubmitted: (text) => print("onSubmitted:$text"),
                onEditingComplete: () =>
                    print("onEditingComplete:${controller.text.toString()}"),
                controller: controller
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "maxLength 编辑框最多输入字数,maxLengthEnforced:true时,只能输入限制字数;maxLengthEnforced:false达到最大限制输入数仍然可以输入,这时labelText和计数器会变成红色,提示超出限制",
                ),
                maxLength: 8
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "maxLength 编辑框最多输入字数,maxLengthEnforced:true时,只能输入限制字数;maxLengthEnforced:false达到最大限制输入数仍然可以输入,这时labelText和计数器会变成红色,提示超出限制",
                ),
                maxLengthEnforced: false,
                maxLength: 8
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "maxLines 编辑框最多显示行数",
              ),
              maxLines: 2,
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "obscureText 是否隐藏输入,true密码样式显示,false明文显示",
                ),
                obscureText: true
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "obscureText 是否隐藏输入,true密码样式显示,false明文显示",
                ),
                obscureText: false
            ),
            TextField(
                decoration: InputDecoration(
                  labelText: "autofocus 自动获取焦点",
                ),
                autofocus: true
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "keyboardType 弹出键盘的类型",
              ),
              keyboardType: TextInputType.number,
            ),
            TextField(
              decoration: InputDecoration(
                  labelText: "labelText是Material Design风格的输入提示,通过labelStyle设置样式",
                  labelStyle: TextStyle(
                      color: Color(0xff999999),
                      fontSize: 12.0
                  )
              ),
            ),
            TextField(
              decoration: InputDecoration(
                  hintText: "hintText 普通的输入提示",
                  hintStyle: TextStyle(
                      color: Color(0xff999999),
                      fontSize: 12.0
                  )
              ),
            ),
            TextField(
              decoration: InputDecoration(
                  helperText: "helperText 显示在输入的下面",
                  helperStyle: TextStyle(
                      color: Color(0xff999999),
                      fontSize: 12.0
                  )
              ),
            ),
            TextField(
              decoration: InputDecoration(
                errorText: "errorText 显示在输入的下面,输入框会变成红色   显示在输入的下面,输入框会变成红色  显示在输入的下面,输入框会变成红色  显示在输入的下面,输入框会变成红色  显示在输入的下面,输入框会变成红色  显示在输入的下面,输入框会变成红色",
                errorStyle: TextStyle(
                    color: Color(0xffff0000),
                    fontSize: 12.0
                ),
                errorMaxLines: 2,
              ),
            ),
            TextField(
              decoration: InputDecoration(
                  labelText: "prefixIcon 输入框内侧左面的Icon",
                  prefixIcon: Icon(Icons.phone)
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "prefixIcon 输入框内侧左面的prefixText,可以通过prefixStyle设置样式",
                prefixText: "手机号",
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "prefix 输入框内侧左面的Widget",
                prefix: Text("手机号"),
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "prefix 输入框内侧左面的Widget",
                prefix: Icon(Icons.phone),
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "suffix 输入框内侧右面的Widget",
                suffix: Icon(Icons.phone),
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "counterText 输入框右下角的计数器文本",
                counterText: "0/10",
                counterStyle: TextStyle(
                    color: Color(0xffff0000)
                ),
              ),
            ),
            TextField(
              decoration: InputDecoration(
                labelText: "filled 是否显示输入框背景,true显示,false不显示",
                filled: true,
                fillColor: Color(0xffff0000),
              ),
            ),
            Container(
              padding: EdgeInsets.all(5.0),
              child: TextField(
                decoration: InputDecoration(
                    labelText: "border  InputBorder.none没有边框,UnderlineInputBorder下边框,OutlineInputBorder四周都有边框",
                    border: OutlineInputBorder(

                    )
                ),
              ),
            ),
          ],
        ),

练习demo,链接https://gitee.com/xgljh/Flutter

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容