Flutter学习笔记(三):文本输入框TextField

2019-04-26  本文已影响0人  睿丶清

TextField用于文本输入,它提供了很多属性,通过源码查看一下主要属性的作用:

const TextField({
    Key key,
    this.controller,
    this.focusNode,
    this.decoration = const InputDecoration(),
    TextInputType keyboardType,
    this.textInputAction,
    this.textCapitalization = TextCapitalization.none,
    this.style,
    this.textAlign = TextAlign.start,
    this.autofocus = false,
    this.obscureText = false,
    this.autocorrect = true,
    this.maxLines = 1,
    this.maxLength,
    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);
import 'package:flutter/material.dart';

class InputPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InputState();
  }
}

class _InputState extends State<InputPage> {

  //输入框监听器
  TextEditingController _unameController = new TextEditingController();
  TextEditingController _passwordController = new TextEditingController();

  //输入框焦点管理对象
  FocusNode unFocusNode = new FocusNode();
  FocusNode pwdFocusNode = new FocusNode();

  //管理输入框焦点对象的对象
  FocusScopeNode focusScopeNode;

  @override
  void initState() {

    //输入框件添加(注册)监听器,用于检测输入框内内容的变化
    _unameController.addListener(() {
      print("username:" + _unameController.text);
    });

    _passwordController.addListener(() {
      print("password:" + _passwordController.text);
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    //页面绘制
    return Scaffold(
      appBar: AppBar(
        title: Text("输入框,表单"),
      ),
      body: Container(
        //竖直排列布局
        child: Column(
          children: <Widget>[
            TextField(
              autofocus: true,
              decoration: InputDecoration(
                  labelText: "用户名",
                  hintText: "输入用户名称",
                  prefixIcon: Icon(Icons.person)),
//              controller: _unameController,
              onChanged: (v) {
                print("username:" + v);
              },
              focusNode: unFocusNode,
            ),
            TextField(
              autofocus: true,
//              controller: _passwordController,
              decoration: InputDecoration(
                  labelText: "密码",
                  hintText: "输入密码",
                  prefixIcon: Icon(Icons.lock)),
              onChanged: (v) {
                print("password:" + v);
              },
              focusNode: pwdFocusNode,
            ),
            //绘制按钮布局
            Builder(builder: (ctx) {
              return Column(
                children: <Widget>[
                  RaisedButton(
                    onPressed: () {
                      //焦点对象管理对象初始化
                      if (focusScopeNode == null) {
                        focusScopeNode = FocusScope.of(ctx);
                      }

                      //进行输入框焦点的转移
                      if (unFocusNode.hasFocus && !pwdFocusNode.hasFocus) {
                        focusScopeNode.requestFocus(pwdFocusNode);
                      } else if (pwdFocusNode.hasFocus &&
                          !unFocusNode.hasFocus) {
                        focusScopeNode.requestFocus(unFocusNode);
                      }else if (!unFocusNode.hasFocus&&!pwdFocusNode.hasFocus){
                        focusScopeNode.requestFocus(unFocusNode);
                      }
                    },
                    child: Text("移动焦点"),
                  ),
                  RaisedButton(
                    onPressed: (){
                      //输入框失去焦点
                      unFocusNode.unfocus();
                      pwdFocusNode.unfocus();
                    },
                    child: Text("关闭软键盘"),
                  ),
                ],
              );
            }),
          ],
        ),
      ),
    );
  }
}

运行程序可以实现上述描述的效果,具体api详情可查看源码或者官方文档,输入框学习暂时到此为止!希望能够对看官们有所帮助!

上一篇 下一篇

猜你喜欢

热点阅读