flutter 表单组件TextField、TextFormField使用

属性介绍

属性 说明
controller 控制器,可以控制 textField 的输入内容,也可以监听 textField 改变
focusNode 焦点控制,
decoration textField 装饰
keyboardType TextInputType,键盘类型
textCapitalization 大小写,默认为 TextCapitalization.none
style 字体样式
strutStyle 字体的布局样式
textAlign 文字对齐方式,默认为 TextAlign.start
textAlignVertical 文字纵轴对齐方式
textDirection TextDirection.ltr 是居左,TextDirection.rtl 是居右,和 textAlign 效果一致
readOnly 只读属性,默认为 false
toolbarOptions 长按时弹出的按钮设置,(如赋值,粘贴,全部选中等)
showCursor 是否显示光标,默认为 true
autofocus 是否自动聚焦,默认为 false
obscuringCharacter 加密输入时的替换字符,默认为 '•'
obscureText 是否加密,默认为 false
autocorrect 是否自动更正,默认为 true
smartDashesType SmartDashesType 智能替换破折号,例如连续输入三个'-' 会自动替换成一个'------',当 obseretext == true 时,smartDashesType 默认不可用
smartQuotesType SmartQuotesType 智能替换引号,根据文字情况智能替换为左引号或者右引号,当 obseretext == true 时,SmartQuotesType 默认不可用
enableSuggestions 是否在用户输入时显示输入建议,此标志仅影响Android,默认为 true
maxLines 最大行数
minLines 最小行数
expands 是否填充父控件,默认为 false
maxLength 最大长度
maxLengthEnforced 是否强制限制,或者只提供字符计数器和警告,默认为 true
onChanged 输入框文字改变回调
onEditingComplete 输入框完成回调
onSubmitted 提交按钮点击回调
inputFormatters 格式化输入,注意这里比 onChanged 先执行
enabled 是否可用
cursorWidth 光标宽度,默认为 2.0
cursorRadius 光标圆角
cursorColor 光标颜色
selectionHeightStyle 选中高度样式,默认为 ui.BoxHeightStyle.tight
keyboardAppearance 键盘外观,此设置仅适用于iOS设备,iOS的白色以及黑色风格键盘
scrollPadding 滚动后距离边缘的距离,默认为 EdgeInsets.all(20.0)
dragStartBehavior 启动阻力,默认为 DragStartBehavior.start
enableInteractiveSelection ///默认为True,如果为false,则大多数辅助功能支持选择文本、复制和粘贴,移动插入符号将被禁用。
onTap 点击事件
mouseCursor 鼠标悬停,Web可以了解
buildCounter InputDecorator.counter 自定义小工具
scrollController 滚动控制器
scrollPhysics 滚动物理效果
autofillHints 自动填充

示例代码

复制代码
     children: [
        const Text("提货点:"),
        //这里只能写在容器container中才可以
        Container(
           width: 500.w,
          child: TextField(
          decoration: const InputDecoration(hintText: "请输入提货点",
            // border: OutlineInputBorder(),
            // 取消自带的下边框
                // border: InputBorder.none,
                // 底部下边框
            enabledBorder:  UnderlineInputBorder(
                  borderSide: BorderSide(color: Color.fromRGBO(235, 229, 229, 1),width: 1),
                
                ),
            // 选中时候下边框
            focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Color.fromRGBO(228, 225, 225, 1),width: 1)
                )
          ),
          controller:_username, //默认显示的文字
          onChanged: (value){
            setState((){
              thText=value;
            });
            print("提货点名称$thText");
          },
        )
        )
        
      ],
    )
相关推荐
衿璃4 分钟前
Flutter应用架构设计的思考
前端·flutter
QuantumLeap丶2 小时前
《Flutter全栈开发实战指南:从零到高级》- 04 - Widget核心概念与生命周期
flutter·xcode
勤劳打代码4 小时前
触类旁通 —— Flutter 与 React 对比解析
前端·flutter·react native
消失的旧时光-194320 小时前
Flutter Event Loop
flutter
程序员老刘21 小时前
跨平台开发地图:客户端技术选型指南 | 2025年10月
flutter·react native·客户端
傅里叶1 天前
Flutter 工程环境、插件使用、protobuf配置与字体/持久化管理
flutter
傅里叶1 天前
Flutter之《环境与依赖配置》
flutter
大雷神1 天前
【成长纪实】HarmonyOS中ArkTS与Flutter数据类型对比详解
flutter
未来猫咪花1 天前
Riverpod 3.0:一个过度设计的反面教材
flutter
消失的旧时光-19431 天前
Flutter 并发编程全解:从零掌握 Isolate
flutter