Flutter Widget => TextField

拼搏现实的明天。 2021-10-01 10:18 608阅读 0赞
  1. const TextField({
  2. Key key,
  3. this.controller, //控制器,控制TextField文字
  4. this.focusNode,
  5. this.decoration: const InputDecoration(), //输入器装饰
  6. TextInputType keyboardType: TextInputType.text, //输入的类型
  7. this.style,
  8. this.textAlign: TextAlign.start,
  9. this.autofocus: false,
  10. this.obscureText: false, //是否隐藏输入
  11. this.autocorrect: true,
  12. this.maxLines: 1,
  13. this.maxLength,
  14. this.maxLengthEnforced: true,
  15. this.onChanged, //文字改变触发
  16. this.onSubmitted, //文字提交触发(键盘按键)
  17. this.onEditingComplete, //当用户提交可编辑内容时调用
  18. this.inputFormatters,
  19. this.enabled,
  20. this.cursorWidth = 2.0,
  21. this.cursorRadius,
  22. this.cursorColor,
  23. this.keyboardAppearance,
  24. })

只有输入框的TextField

  1. TextField()

一个TextField示例,实例中是传入一个controller。通过controller添加通知来获取TextField的值,这种使用场景不一定合适,更多的时候是在点击按钮的时候直接读取controller.text的值

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. class TextFieldPage extends StatelessWidget {
  4. Widget buildTextField(TextEditingController controller) {
  5. return TextField(
  6. controller: controller,
  7. maxLength: 30,//最大长度,设置此项会让TextField右下角有一个输入数量的统计字符串
  8. maxLines: 1,//最大行数
  9. autocorrect: true,//是否自动更正
  10. autofocus: true,//是否自动对焦
  11. obscureText: true,//是否是密码
  12. textAlign: TextAlign.center,//文本对齐方式
  13. style: TextStyle(fontSize: 30.0, color: Colors.blue),//输入文本的样式
  14. inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],//允许的输入格式
  15. onChanged: (text) {//内容改变的回调
  16. print('change $text');
  17. },
  18. onSubmitted: (text) {//内容提交(按回车)的回调
  19. print('submit $text');
  20. },
  21. enabled: true,//是否禁用
  22. );
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. final controller = TextEditingController();
  27. controller.addListener(() {
  28. print('input ${controller.text}');
  29. });
  30. return Scaffold(
  31. appBar: AppBar(
  32. title: Text('TextField'),
  33. ),
  34. body: Padding(
  35. padding: const EdgeInsets.all(20.0),
  36. child: buildTextField(controller),
  37. ),
  38. );
  39. }
  40. }

InputDecoration装饰器

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

圆角矩形的边框

  1. import 'package:flutter/material.dart';
  2. class TextFieldPage extends StatelessWidget {
  3. Widget buildTextField() {
  4. return TextField(
  5. decoration: InputDecoration(
  6. contentPadding: EdgeInsets.all(10.0),
  7. border: OutlineInputBorder(
  8. borderRadius: BorderRadius.circular(15.0),
  9. borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)
  10. )),
  11. );
  12. }
  13. @override
  14. Widget build(BuildContext context) {
  15. return Scaffold(
  16. appBar: AppBar(
  17. title: Text('TextField'),
  18. ),
  19. body: Container(
  20. color: Colors.blue.shade100,
  21. child: Padding(
  22. padding: const EdgeInsets.all(30.0),
  23. child: buildTextField(),
  24. ),
  25. ),
  26. );
  27. }
  28. }

发表评论

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

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

相关阅读

    相关 Flutter widget目录

    这是个Flutter教学系列的文章,会从Flutter基础入门开始讲解,带领大家从零开始学习Flutter,从基础组件一个一个的过,可以保证每个没有Flutter开发基础的同学

    相关 Flutter Widget大全

    还没有做完,应该是目前最全的Widget介绍了,如果需要使用什么控件在这里找就行,如果还没有就得自定义Widget了,欢迎点赞支持我继续把图做完,这个图没什么技术含量,大部分就