my_input_field.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:flutter/material.dart';
  2. // ignore: slash_for_doc_comments
  3. /**
  4. 继承于FromTextField
  5. 模板:
  6. MyInputField(
  7. controller: controller.registerEmailController,
  8. keyboardType: TextInputType.text,
  9. labelText: '',
  10. placeholder: 'Enter Email Address',
  11. validator: (value) {
  12. if (value?.isEmpty ?? true) {
  13. return 'Email is required.';
  14. }
  15. return null;
  16. },
  17. ),
  18. */
  19. class MyInputField extends StatelessWidget {
  20. final TextEditingController controller;
  21. final TextInputType keyboardType;
  22. final String labelText;
  23. final String placeholder;
  24. final Color color;
  25. final double fontSize;
  26. final bool password;
  27. final String? Function(String?)? validator;
  28. MyInputField({
  29. required this.controller,
  30. this.keyboardType = TextInputType.text,
  31. this.labelText = '',
  32. this.placeholder = '',
  33. this.color = Colors.black,
  34. this.fontSize = 22.0,
  35. this.password = false,
  36. this.validator,
  37. });
  38. @override
  39. Widget build(BuildContext context) {
  40. return TextFormField(
  41. decoration: InputDecoration(
  42. fillColor: Colors.transparent,
  43. contentPadding: EdgeInsets.symmetric(vertical: 10.0),
  44. focusedBorder: UnderlineInputBorder(
  45. borderSide: BorderSide(
  46. color: this.color,
  47. ),
  48. ),
  49. enabledBorder: UnderlineInputBorder(
  50. borderSide: BorderSide(
  51. color: this.color,
  52. ),
  53. ),
  54. floatingLabelBehavior: FloatingLabelBehavior.always,
  55. labelText: this.labelText,
  56. labelStyle: TextStyle(
  57. fontSize: fontSize - 2,
  58. color: color,
  59. height: 0.2,
  60. fontWeight: FontWeight.normal,
  61. ),
  62. hintText: this.placeholder,
  63. hintStyle: TextStyle(
  64. fontSize: fontSize,
  65. color: color,
  66. fontWeight: FontWeight.normal,
  67. ),
  68. filled: true,
  69. isDense: true,
  70. ),
  71. controller: this.controller,
  72. style: TextStyle(
  73. color: color,
  74. fontSize: fontSize,
  75. fontWeight: FontWeight.normal,
  76. ),
  77. keyboardType: this.keyboardType,
  78. obscureText: this.password,
  79. autocorrect: false,
  80. validator: this.validator,
  81. );
  82. }
  83. }