当前位置:   article > 正文

【flutter】输入框控制输入类型和键盘、输入框抵住键盘、关闭键盘、强制横屏/竖屏_flutter 输入框控制器类型

flutter 输入框控制器类型
import 'package:flutter/services.dart';

  • 1
  • 2

1、键盘类型

TextField(
                keyboardType: TextInputType.number,
              ),
  • 1
  • 2
  • 3

输入的类型

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,
)),
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

小数校验

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),
    );
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2、输入框抵住键盘

resizeToAvoidBottomPadding: false,
  • 1

3、关闭键

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

4、只能输入汉字或者字母或数字

inputFormatters: [
  WhitelistingTextInputFormatter(RegExp(
      "[a-zA-Z]|[\u4e00-\u9fa5]|[0-9]")), //只能输入汉字或者字母或数字
  LengthLimitingTextInputFormatter(maxLength),//最大长度
],

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5\强制横屏/竖屏

void main(){
  // 强制横屏
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);
 
  runApp(new MyApp());
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
void main(){
  // 强制竖屏
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
 
  runApp(new MyApp());
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/268684
推荐阅读
相关标签
  

闽ICP备14008679号