text_field_item.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:ftrecruiter/comm/utils/number_text_input_formatter.dart';
  5. // ignore: slash_for_doc_comments
  6. /**
  7. 封装输入框 左边名字 右边输入框的布局
  8. 常用于一些表单的提交
  9. 模板:
  10. TextFieldItem(
  11. focusNode: _nodeText3,
  12. keyboardType: TextInputType.phone,
  13. title: '联系电话',
  14. hintText: '填写店主联系电话'
  15. ),
  16. */
  17. class TextFieldItem extends StatelessWidget {
  18. const TextFieldItem({
  19. Key? key,
  20. this.controller,
  21. required this.title,
  22. this.keyboardType = TextInputType.text,
  23. this.hintText = '',
  24. this.focusNode,
  25. }): super(key: key);
  26. final TextEditingController? controller;
  27. final String title;
  28. final String hintText;
  29. final TextInputType keyboardType;
  30. final FocusNode? focusNode;
  31. @override
  32. Widget build(BuildContext context) {
  33. final Row child = Row(
  34. children: <Widget>[
  35. Text(title),
  36. SizedBox(width: 16),
  37. Expanded(
  38. child: Semantics(
  39. label: hintText.isEmpty ? '请输入$title' : hintText,
  40. child: TextField(
  41. focusNode: focusNode,
  42. keyboardType: keyboardType,
  43. inputFormatters: _getInputFormatters(),
  44. controller: controller,
  45. //style: TextStyles.textDark14,
  46. decoration: InputDecoration(
  47. hintText: hintText,
  48. border: InputBorder.none, //去掉下划线
  49. //hintStyle: TextStyles.textGrayC14
  50. ),
  51. ),
  52. ),
  53. ),
  54. SizedBox(width: 16),
  55. ],
  56. );
  57. return Container(
  58. height: 50.0,
  59. margin: const EdgeInsets.only(left: 16.0),
  60. width: double.infinity,
  61. decoration: BoxDecoration(
  62. border: Border(
  63. bottom: Divider.createBorderSide(context, width: 0.6),
  64. ),
  65. ),
  66. child: child,
  67. );
  68. }
  69. List<TextInputFormatter>? _getInputFormatters() {
  70. if (keyboardType == const TextInputType.numberWithOptions(decimal: true)) {
  71. return <TextInputFormatter>[UsNumberTextInputFormatter()];
  72. }
  73. if (keyboardType == TextInputType.number || keyboardType == TextInputType.phone) {
  74. return <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly];
  75. }
  76. return null;
  77. }
  78. }