【flutter】输入框控制输入类型和键盘、输入框抵住键盘、关闭键盘

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ivy_doudou/article/details/100976512
import 'package:flutter/services.dart';

1、键盘类型

TextField(
                keyboardType: TextInputType.number,
              ),

输入的类型

TextField(
       keyboardType: TextInputType.number,//键盘类型,数字键盘
       style: TextStyle(fontSize: ScreenUtil().setWidth(40), color: Colors.black),//输入文字样式
       controller: _cpyCode,//控制器
       decoration: InputDecoration(
       		hintText: '请输入6位公司编号',
       		hintStyle: TextStyle( fontWeight: FontWeight.w600, fontSize: ScreenUtil().setWidth(40), color: 			   Colors.grey[400]),
           border: InputBorder.none,
       ),
       inputFormatters: <TextInputFormatter>[
           WhitelistingTextInputFormatter.digitsOnly,//只输入数字
           LengthLimitingTextInputFormatter(6)//限制长度
       ],
       onChanged: _listenCpyCode,
)),

小数校验

class _UsNumberTextInputFormatter extends TextInputFormatter {
  static const defaultDouble = 0.001;
  static double strToFloat(String str, [double defaultValue = defaultDouble]) {
    try {
      return double.parse(str);
    } catch (e) {
      return defaultValue;
    }
  }

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    String value = newValue.text;
    int selectionIndex = newValue.selection.end;
    if (value == ".") {
      value = "0.";
      selectionIndex++;
    } else if (value != "" && value != defaultDouble.toString() && strToFloat(value, defaultDouble) == defaultDouble) {
      value = oldValue.text;
      selectionIndex = oldValue.selection.end;
    }
    return new TextEditingValue(
      text: value,
      selection: new TextSelection.collapsed(offset: selectionIndex),
    );
  }
}

2、输入框抵住键盘

resizeToAvoidBottomPadding: false,

3、关闭键

FocusScope.of(context).requestFocus(FocusNode());

猜你喜欢

转载自blog.csdn.net/ivy_doudou/article/details/100976512