common_input_field.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'package:flutter/material.dart';
  2. class CommonInputField extends StatelessWidget {
  3. final String label;
  4. final bool required;
  5. final String value;
  6. final ValueChanged<String> onChanged;
  7. final ValueChanged<String>? onFieldSubmitted; // 回车键事件
  8. final TextInputType keyboardType;
  9. final bool obscureText;
  10. final Widget? suffix;
  11. final String? hintText;
  12. const CommonInputField({
  13. super.key,
  14. required this.label,
  15. this.required = false,
  16. required this.value,
  17. required this.onChanged,
  18. this.onFieldSubmitted,
  19. this.keyboardType = TextInputType.text,
  20. this.obscureText = false,
  21. this.suffix,
  22. this.hintText
  23. });
  24. @override
  25. Widget build(BuildContext context) {
  26. return Column(
  27. crossAxisAlignment: CrossAxisAlignment.start,
  28. children: [
  29. RichText(
  30. text: TextSpan(
  31. text: label,
  32. style: const TextStyle(color: Colors.black87, fontSize: 14),
  33. children: [
  34. if (required)
  35. const TextSpan(
  36. text: '*',
  37. style: TextStyle(color: Colors.red),
  38. ),
  39. ]
  40. )
  41. ),
  42. const SizedBox(height: 8),
  43. TextFormField(
  44. initialValue: value,
  45. onChanged: onChanged,
  46. onFieldSubmitted: onFieldSubmitted,
  47. keyboardType: keyboardType,
  48. obscureText: obscureText,
  49. decoration: InputDecoration(
  50. hintText: hintText,
  51. hintStyle: TextStyle(color: Colors.grey.shade400),
  52. suffixIcon: suffix,
  53. contentPadding: const EdgeInsets.symmetric(vertical: 14, horizontal: 12),
  54. border: OutlineInputBorder(
  55. borderRadius: BorderRadius.circular(6),
  56. ),
  57. ),
  58. )
  59. ],
  60. );
  61. }
  62. }