import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:keyboard_actions/keyboard_actions.dart'; import 'package:liftmanager/res/resources.dart'; import 'package:liftmanager/utils/number_text_input_formatter.dart'; import 'package:liftmanager/utils/theme_utils.dart'; /// 封装输入框 class TextFieldItem extends StatelessWidget { const TextFieldItem( {Key key, this.controller, @required this.title, this.keyboardType: TextInputType.text, this.hintText: "", this.isMust: false, this.content: "", this.maxLength, this.focusNode, this.onChanged, this.config, this.enabled=true}) : super(key: key); final TextEditingController controller; final String title; final String hintText; final bool isMust; final String content; final TextInputType keyboardType; final int maxLength; final FocusNode focusNode; final Function onChanged; final KeyboardActionsConfig config; final bool enabled; @override Widget build(BuildContext context) { bool isDark = ThemeUtils.isDark(context); if (config != null && defaultTargetPlatform == TargetPlatform.iOS) { // 因Android平台输入法兼容问题,所以只配置IOS平台 FormKeyboardActions.setKeyboardActions(context, config); } if (content.length > 0) { controller.text = content; } return Container( height: 50.0, margin: const EdgeInsets.only(left: 16.0), width: double.infinity, decoration: BoxDecoration( color: isDark?Colors.black:Colors.white, border: Border( bottom: Divider.createBorderSide(context, width: 0.6), )), child: Row( children: [ Padding( padding: const EdgeInsets.only(right: 5.0), child: Text(title), ), Offstage( offstage: !this.isMust, child: Text( " *", style: TextStyle(color: Colors.red), ), ), Expanded( flex: 1, child: TextField( enabled: enabled, textAlign: TextAlign.right, onChanged: onChanged, maxLength: maxLength, focusNode: focusNode, keyboardType: keyboardType, inputFormatters: _getInputFormatters(), controller: controller, //style: TextStyles.textDark14, decoration: InputDecoration( hintText: hintText, hintStyle: TextStyle(color: Colours.text_gray_c), border: InputBorder.none, //去掉下划线 //hintStyle: TextStyles.textGrayC14 ) ), ), Gaps.hGap16 ], ), ); } _getInputFormatters() { if (keyboardType == TextInputType.numberWithOptions(decimal: true)) { return [UsNumberTextInputFormatter(),MoneyTextInputFormatter()]; } if (keyboardType == TextInputType.number || keyboardType == TextInputType.phone) { return [WhitelistingTextInputFormatter.digitsOnly]; } return null; } } class MoneyTextInputFormatter extends TextInputFormatter{ @override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) { // TODO: implement formatEditUpdate String newvalueText=newValue.text; if(newvalueText=="."){ //第一个数为. newvalueText="0."; } else if(newvalueText.contains(".")){ if(newvalueText.lastIndexOf(".")!=newvalueText.indexOf(".")){ //输入了2个小数点 newvalueText= newvalueText.substring(0,newvalueText.lastIndexOf('.')); }else if(newvalueText.length-1-newvalueText.indexOf(".")>2){ //输入了1个小数点 小数点后两位 newvalueText=newvalueText.substring(0,newvalueText.indexOf(".")+3); } } String a ; if(!RegExp(r"^([1-9]\d*|[0]{1,1})$").hasMatch(newvalueText)){ if(newvalueText.contains(".")){ a = newvalueText; }else if (newvalueText == "00"){ a = "0"; } // else if (newvalueText == "00."){ // newvalueText = "0."; // } else { a = newvalueText.replaceAll(RegExp(r'(^[0]+)'), ""); } // ignore: unnecessary_statements }else ( a = newvalueText ); print(a); print(123456789); return TextEditingValue( text: a, selection: TextSelection.collapsed(offset: a.length), ); } }