Flutter 输入框 TextField、表单 Form 图文示例

爱被打了一巴掌 2023-05-28 15:54 43阅读 0赞

文章目录

      • 输入框 TextField
        • 默认效果
        • 监听内容变化,获取输入框的内容
        • 和键盘的交互
        • 美化 TextField 样式
      • 表单 Form 验证
        • 输入框内容校验
        • 输入框内容重置
        • 输入内容保存
      • 登录页面例子

输入框 TextField

TextField用于文本输入,它提供了很多属性:

  1. const TextField({
  2. Key key,
  3. this.controller,// 控制器
  4. this.focusNode, // 控制TextField是否占有当前键盘的输入焦点
  5. this.decoration = const InputDecoration(), // 控制TextField的外观显示
  6. TextInputType keyboardType,// 输入框默认的键盘输入类型
  7. this.textInputAction,
  8. this.textCapitalization = TextCapitalization.none,
  9. this.style,//输入文本的样式
  10. this.strutStyle,
  11. this.textAlign = TextAlign.start,//文本对齐方式
  12. this.textAlignVertical,
  13. this.textDirection,
  14. this.readOnly = false,
  15. ToolbarOptions toolbarOptions,
  16. this.showCursor,// 显示光标
  17. this.autofocus = false,//是否自动对焦
  18. this.obscureText = false,//是否隐藏输入的内容
  19. this.autocorrect = true,//是否自动更正
  20. this.enableSuggestions = true,
  21. this.maxLines = 1,// 最大行数
  22. this.minLines,//最小行数
  23. this.expands = false,
  24. this.maxLength,//最大长度
  25. this.maxLengthEnforced = true,
  26. this.onChanged,// 输入框变化回调
  27. this.onEditingComplete,// 编辑完成回调
  28. this.onSubmitted,// 提交回调
  29. this.inputFormatters,//允许的输入格式
  30. this.enabled,//是否禁用textfield
  31. this.cursorWidth = 2.0,// 光标宽度
  32. this.cursorRadius,//光标半径
  33. this.cursorColor,//光标颜色
  34. this.keyboardAppearance,
  35. this.scrollPadding = const EdgeInsets.all(20.0),
  36. this.dragStartBehavior = DragStartBehavior.start,
  37. this.enableInteractiveSelection = true,
  38. this.onTap,
  39. this.buildCounter,
  40. this.scrollController,
  41. this.scrollPhysics,
  42. })

字段太多了,我们从几个示例来了解主要字段的使用。

默认效果

只有一条下划线,也没有自动获取焦点。

  1. TextField();

在这里插入图片描述

监听内容变化,获取输入框的内容

如果我们要获取输入的内容,这时候可以通过onChange, onSubmitted,伪代码如下:

  1. Widget _buildTextField(){
  2. return TextField(
  3. onChanged: (text) {
  4. //内容改变的回调
  5. print('change $text');
  6. },
  7. onEditingComplete:(){
  8. print('editing ');
  9. },
  10. onSubmitted: (text) {
  11. //内容提交(按回车)的回调
  12. print('submit $text');
  13. },
  14. );
  15. }

在这里插入图片描述

在这里插入图片描述

和键盘的交互

默认获取焦点
只需要将autofocus设置为true即可。

  1. Widget _buildTextField(){
  2. return TextField(
  3. autofocus: true,
  4. );
  5. }

改变键盘右下角的功能键

  1. Widget _buildTextField(){
  2. return TextField(
  3. autofocus: true,
  4. keyboardType: TextInputType.text,
  5. // 右下角按钮变为:前往或者Go
  6. textInputAction:TextInputAction.go,
  7. );
  8. }

在这里插入图片描述
在这里插入图片描述

改变键盘的输入类型

  1. Widget _buildTextField(){
  2. return TextField(
  3. autofocus: true,
  4. keyboardType: TextInputType.number,
  5. // 将键盘显示类型设置为数字键盘
  6. keyboardType: TextInputType.number,
  7. );
  8. }

在这里插入图片描述
在这里插入图片描述
如何自适应屏幕弹起

目前来看TextField已满足一个输入框的功能了。
但是它太丑了,这不符合Flutter构建一个漂亮App的目标

美化 TextField 样式

主要是使用TextField自带的字段修改,或者使用InputDecoration修改样式。
TextField的常用字段我们在上面已经给出注释了,接下来查看InputDecoration的常见字段:

字段基本释义来源:Flutter文本输入框TextField属性(InputDecoration、textInputAction、inputFormatters等等)详解

  1. const InputDecoration({
  2. this.icon, //位于装饰器外部和输入框前面的图片
  3. this.labelText,//用于描述输入框,当输入框获取焦点时默认会浮动到上方,
  4. this.labelStyle,// 控制labelText的样式
  5. this.helperText,//辅助文本,位于输入框下方,如果errorText不为空的话,则helperText不会显示
  6. this.helperStyle,//helperText的样式
  7. this.helperMaxLines,//提示信息最大行数
  8. this.hintText,//提示文本,位于输入框内部
  9. this.hintStyle,//hintText的样式
  10. this.hintMaxLines,//提示信息最大行数
  11. this.errorText,//errorText的信息
  12. this.errorStyle,//errorText的样式
  13. this.errorMaxLines, //errorText的最大行数
  14. this.hasFloatingPlaceholder = true, //labelText是否浮动,默认为true
  15. this.isDense,//改变输入框是否为密集型
  16. this.contentPadding,//内间距
  17. this.prefixIcon,//位于输入框内部起始位置的图标。
  18. this.prefix,//预先填充的Widget,跟prefixText同时只能出现一个
  19. this.prefixText,//预填充的文本,例如手机号前面预先加上区号等
  20. this.prefixStyle,//prefixText的样式
  21. this.suffixIcon,//位于输入框后面的图片
  22. this.suffix, //位于输入框尾部的控件,同样的不能和suffixText同时使用
  23. this.suffixText,//位于尾部的填充文字
  24. this.suffixStyle, //suffixText的样式
  25. this.counter,//位于输入框右下方的小控件,不能和counterText同时使用
  26. this.counterText,//位于右下方显示的文本,常用于显示输入的字符数量
  27. this.counterStyle,//counterText的样式
  28. this.filled,//如果为true,则输入使用fillColor指定的颜色填充
  29. this.fillColor,//输入框的背景颜色
  30. this.focusColor,//输入框有焦点时的边框颜色
  31. this.hoverColor,//输入框被悬浮时的边框颜色
  32. this.errorBorder,//errorText不为空,输入框没有焦点时要显示的边框
  33. this.focusedBorder,//输入框有焦点时的边框
  34. this.focusedErrorBorder,//errorText不为空时,输入框有焦点时的边框
  35. this.disabledBorder,//输入框禁用时显示的边框
  36. this.enabledBorder,//输入框可用时显示的边框
  37. this.border,//正常情况下的border
  38. this.enabled = true,//输入框是否可用
  39. this.semanticCounterText,
  40. this.alignLabelWithHint,
  41. })

接下来我们通过几个例子来看看实现的效果图。

游标颜色、粗细,控制输入长度

  1. Widget _buildTextField(){
  2. return TextField(
  3. autofocus: true,
  4. cursorColor: Colors.deepOrange,
  5. cursorRadius: Radius.circular(20.0),
  6. cursorWidth: 10.0,
  7. maxLength: 10,
  8. );
  9. }

在这里插入图片描述

密码框
增加obscureText: true,属性即可实现输入的内容是隐藏的。
在这里插入图片描述
改变下划线颜色
默认下划线是跟随主题的红色,这里将其改为橘色700。
在这里插入图片描述

  1. Widget _buildTextField(){
  2. return Container(
  3. child: TextField(
  4. autofocus: true,
  5. decoration: InputDecoration(
  6. border: InputBorder.none //隐藏下划线
  7. )
  8. ),
  9. decoration: BoxDecoration(
  10. // 下滑线橘色700,宽度3像素
  11. border: Border(bottom: BorderSide(color: Colors.orange[700], width: 3.0))
  12. ),
  13. );
  14. }

圆角输入框
在这里插入图片描述

  1. Widget _buildTextField(){
  2. return TextField(
  3. autofocus: true,
  4. cursorColor: Colors.deepOrange,
  5. cursorRadius: Radius.circular(20.0),
  6. cursorWidth: 10.0,
  7. maxLength: 10,
  8. obscureText: true,
  9. decoration: InputDecoration(
  10. // 文本内容的内边距
  11. contentPadding: EdgeInsets.all(10.0),
  12. // 圆角矩形的输入框样式
  13. border: OutlineInputBorder(
  14. // 圆角半径 10
  15. borderRadius: BorderRadius.circular(10.0),
  16. )),
  17. );
  18. }

带有图标输入框
使用InputDecoration常见的一些属性完成了以下的效果图:
在这里插入图片描述

  1. Widget _buildTextField(){
  2. return Column(
  3. children: <Widget>[
  4. TextField(
  5. decoration: InputDecoration(
  6. icon: Icon(Icons.supervised_user_circle),
  7. suffixIcon: Icon(Icons.search),
  8. prefixText: 'prefixText ',
  9. suffixText: 'suffixText',
  10. labelText: "用户名",
  11. helperText: '请输入用户名、手机号',
  12. hintText: "用户名或手机号",
  13. prefixIcon: Icon(Icons.person)
  14. ),
  15. ),
  16. TextField(
  17. decoration: InputDecoration(
  18. prefixIcon: Icon(Icons.lock),
  19. suffixIcon: Icon(Icons.remove_red_eye),
  20. labelText: "密码",
  21. hintText: "您的登录密码",
  22. hintStyle: TextStyle(color: Colors.grey, fontSize: 13.0)
  23. ),
  24. obscureText: true,
  25. )
  26. ],
  27. );
  28. }

表单 Form 验证

Flutter提供了一个Form 组件,它可以对输入框进行分组。Form对应的状态类为FormState,可以通过Form.of()GlobalKey获得。获得状态类之后就可以做统一操作了如:

  • FormState.validate():调用此方法后,会调用子孙FormFieldvalidate回调,如果有一个校验失败,则返回false,所有校验失败项都会返回用户返回的错误提示。
  • FormState.save():调用此方法后,会调用子孙FormFieldsave回调,用于保存表单内容
  • FormState.reset():调用此方法后,会将子孙FormField的内容清空。

在这里插入图片描述

输入框内容校验

  • 自动校验

autovalidate: true, //开启自动校验,在 Form 组件下设置该属性为true

  • 手动校验

通过点击按钮来校验Form中所有TextField的信息。

  1. 定义全局的 key,用于获取state
  2. TextField中做验证判断
  3. 点击按钮时获取FormState,调用validate()方法。

由于代码过多,粘贴阅读体验不好,相关代码可参考:dongxi346/Flutter-WanAndroid

输入框内容重置

  • 通过FormState去调用

    var _formState = _formKey.currentState as FormState;
    // Form 里面的所有TextField输入框内容都被清空
    _formState.reset();

由于代码过多,粘贴阅读体验不好,相关代码可参考:dongxi346/Flutter-WanAndroid

输入内容保存

  • TextField中使用 onSave字段

例如:

  1. onSaved: (value){
  2. _passWord = value;
  3. },
  • 通过FormState去调用

    var _formState = _formKey.currentState as FormState;
    // Form 里面的所有TextField onSave 方法都被调用
    _formState.save();

由于代码过多,粘贴阅读体验不好,相关代码可参考:dongxi346/Flutter-WanAndroid

登录页面例子

原来的效果图来源忘记了,好像是在一个第三方库中出现的效果,当时觉得UI超级好看就直接复制了作者的源码在 现有的项目 Flutter-WanAndroid-login-page 中使用 ,如有侵权联系。

通过前面几篇文章以及本篇文章的分析,实现这个登录效果图应该是相当容易了。
这里暂不实现,待后面学习了布局的摆放后再来实现这个例子。
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,43人围观)

还没有评论,来说两句吧...

相关阅读