text_field_item.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:keyboard_actions/keyboard_actions.dart';
  5. import 'package:liftmanager/res/resources.dart';
  6. import 'package:liftmanager/utils/number_text_input_formatter.dart';
  7. import 'package:liftmanager/utils/theme_utils.dart';
  8. /// 封装输入框
  9. class TextFieldItem extends StatelessWidget {
  10. const TextFieldItem(
  11. {Key key,
  12. this.controller,
  13. @required this.title,
  14. this.keyboardType: TextInputType.text,
  15. this.hintText: "",
  16. this.isMust: false,
  17. this.content: "",
  18. this.maxLength,
  19. this.focusNode,
  20. this.onChanged,
  21. this.config,
  22. this.enabled=true})
  23. : super(key: key);
  24. final TextEditingController controller;
  25. final String title;
  26. final String hintText;
  27. final bool isMust;
  28. final String content;
  29. final TextInputType keyboardType;
  30. final int maxLength;
  31. final FocusNode focusNode;
  32. final Function onChanged;
  33. final KeyboardActionsConfig config;
  34. final bool enabled;
  35. @override
  36. Widget build(BuildContext context) {
  37. bool isDark = ThemeUtils.isDark(context);
  38. if (config != null && defaultTargetPlatform == TargetPlatform.iOS) {
  39. // 因Android平台输入法兼容问题,所以只配置IOS平台
  40. FormKeyboardActions.setKeyboardActions(context, config);
  41. }
  42. if (content.length > 0) {
  43. controller.text = content;
  44. }
  45. return Container(
  46. height: 50.0,
  47. margin: const EdgeInsets.only(left: 16.0),
  48. width: double.infinity,
  49. decoration: BoxDecoration(
  50. color: isDark?Colors.black:Colors.white,
  51. border: Border(
  52. bottom: Divider.createBorderSide(context, width: 0.6),
  53. )),
  54. child: Row(
  55. children: <Widget>[
  56. Padding(
  57. padding: const EdgeInsets.only(right: 5.0),
  58. child: Text(title),
  59. ),
  60. Offstage(
  61. offstage: !this.isMust,
  62. child: Text(
  63. " *",
  64. style: TextStyle(color: Colors.red),
  65. ),
  66. ),
  67. Expanded(
  68. flex: 1,
  69. child: TextField(
  70. enabled: enabled,
  71. textAlign: TextAlign.right,
  72. onChanged: onChanged,
  73. maxLength: maxLength,
  74. focusNode: focusNode,
  75. keyboardType: keyboardType,
  76. inputFormatters: _getInputFormatters(),
  77. controller: controller,
  78. //style: TextStyles.textDark14,
  79. decoration: InputDecoration(
  80. hintText: hintText,
  81. hintStyle: TextStyle(color: Colours.text_gray_c),
  82. border: InputBorder.none, //去掉下划线
  83. //hintStyle: TextStyles.textGrayC14
  84. )
  85. ),
  86. ),
  87. Gaps.hGap16
  88. ],
  89. ),
  90. );
  91. }
  92. _getInputFormatters() {
  93. if (keyboardType == TextInputType.numberWithOptions(decimal: true)) {
  94. return [UsNumberTextInputFormatter(),MoneyTextInputFormatter()];
  95. }
  96. if (keyboardType == TextInputType.number ||
  97. keyboardType == TextInputType.phone) {
  98. return [WhitelistingTextInputFormatter.digitsOnly];
  99. }
  100. return null;
  101. }
  102. }
  103. class MoneyTextInputFormatter extends TextInputFormatter{
  104. @override
  105. TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
  106. // TODO: implement formatEditUpdate
  107. String newvalueText=newValue.text;
  108. if(newvalueText=="."){
  109. //第一个数为.
  110. newvalueText="0.";
  111. } else if(newvalueText.contains(".")){
  112. if(newvalueText.lastIndexOf(".")!=newvalueText.indexOf(".")){
  113. //输入了2个小数点
  114. newvalueText= newvalueText.substring(0,newvalueText.lastIndexOf('.'));
  115. }else if(newvalueText.length-1-newvalueText.indexOf(".")>2){
  116. //输入了1个小数点 小数点后两位
  117. newvalueText=newvalueText.substring(0,newvalueText.indexOf(".")+3);
  118. }
  119. }
  120. String a ;
  121. if(!RegExp(r"^([1-9]\d*|[0]{1,1})$").hasMatch(newvalueText)){
  122. if(newvalueText.contains(".")){
  123. a = newvalueText;
  124. }else if (newvalueText == "00"){
  125. a = "0";
  126. }
  127. // else if (newvalueText == "00."){
  128. // newvalueText = "0.";
  129. // }
  130. else {
  131. a = newvalueText.replaceAll(RegExp(r'(^[0]+)'), "");
  132. }
  133. // ignore: unnecessary_statements
  134. }else (
  135. a = newvalueText
  136. );
  137. print(a);
  138. print(123456789);
  139. return TextEditingValue(
  140. text: a,
  141. selection: TextSelection.collapsed(offset: a.length),
  142. );
  143. }
  144. }