如何在文本字段上显示当前时间

huangapple go评论81阅读模式
英文:

HOW TO SHOW CURRENT TIME ON TEXTFIELD

问题

以下是您要翻译的代码部分:

  1. Expanded(
  2. child: Padding(
  3. padding: const EdgeInsets.all(10),
  4. child: TextFormField(
  5. onTap: () async {
  6. TimeOfDay? pickedTime = await showTimePicker(
  7. context: context,
  8. initialTime: TimeOfDay.now(),
  9. initialEntryMode: TimePickerEntryMode.dial,
  10. );
  11. if (pickedTime != null) {
  12. setState(
  13. () {
  14. addActivityStartTimeController.text =
  15. DateFormat("hh:mm").format(DateTime.now());
  16. },
  17. );
  18. }
  19. },
  20. readOnly: true,
  21. minLines: 1,
  22. maxLines: 1,
  23. validator: (value) => addActivityStartTimeValidator(value!),
  24. controller: addActivityStartTimeController,
  25. cursorColor: Colors.black,
  26. style: GoogleFonts.outfit(fontSize: 16, color: Colors.black),
  27. decoration: InputDecoration(
  28. prefixIcon: Icon(Icons.watch_later_outlined),
  29. alignLabelWithHint: true,
  30. filled: true,
  31. hintText: "10:20",
  32. hintStyle: GoogleFonts.outfit(
  33. textStyle: const TextStyle(
  34. fontSize: 14,
  35. fontWeight: FontWeight.w400,
  36. ),
  37. color: Colors.black54,
  38. ),
  39. fillColor: ColorConstants.backgroundColor,
  40. focusedErrorBorder: const OutlineInputBorder(
  41. borderSide: BorderSide.none,
  42. borderRadius: BorderRadius.all(
  43. Radius.circular(10),
  44. ),
  45. ),
  46. errorBorder: const OutlineInputBorder(
  47. borderSide: BorderSide.none,
  48. borderRadius: BorderRadius.all(Radius.circular(10)),
  49. ),
  50. errorStyle: GoogleFonts.outfit(
  51. fontSize: 14.0,
  52. fontWeight: FontWeight.normal,
  53. color: Colors.red,
  54. ),
  55. focusedBorder: const OutlineInputBorder(
  56. borderSide: BorderSide.none,
  57. borderRadius: BorderRadius.all(Radius.circular(10)),
  58. ),
  59. border: const OutlineInputBorder(
  60. borderSide: BorderSide.none,
  61. borderRadius: BorderRadius.all(
  62. Radius.circular(10),
  63. ),
  64. ),
  65. ),
  66. ),
  67. ),
  68. ),
  1. onTap: () async {
  2. TimeOfDay? pickedTime = await showTimePicker(
  3. context: context,
  4. initialTime: TimeOfDay.now(),
  5. initialEntryMode: TimePickerEntryMode.dial,
  6. );
  7. if (pickedTime != null) {
  8. setState(
  9. () {
  10. addActivityStartTimeController.text = pickedTime.toString();
  11. },
  12. );
  13. }
  14. },

请注意,我已经将HTML实体(如")转换为普通的引号,以使代码更容易阅读。如果您有任何其他疑问,请随时提出。

英文:
  1. Expanded(
  2. child: Padding(
  3. padding: const EdgeInsets.all(10),
  4. child: TextFormField(
  5. onTap: () async {
  6. TimeOfDay? pickedTime =
  7. await showTimePicker(
  8. context: context,
  9. initialTime: TimeOfDay.now(),
  10. initialEntryMode:
  11. TimePickerEntryMode.dial);
  12. if (pickedTime != null) {
  13. setState(
  14. () {
  15. addActivityStartTimeController
  16. .text =
  17. DateFormat("hh:mm")
  18. .format(DateTime.now());
  19. },
  20. );
  21. }
  22. },
  23. readOnly: true,
  24. minLines: 1,
  25. maxLines: 1,
  26. validator: (value) =>
  27. addActivityStartTimeValidator(value!),
  28. controller:
  29. addActivityStartTimeController,
  30. // keyboardType: TextInputType.emailAddress,
  31. cursorColor: Colors.black,
  32. style: GoogleFonts.outfit(
  33. fontSize: 16, color: Colors.black),
  34. decoration: InputDecoration(
  35. prefixIcon:
  36. Icon(Icons.watch_later_outlined),
  37. alignLabelWithHint: true,
  38. filled: true,
  39. hintText: "10:20",
  40. hintStyle: GoogleFonts.outfit(
  41. textStyle: const TextStyle(
  42. fontSize: 14,
  43. fontWeight: FontWeight.w400),
  44. color: Colors.black54),
  45. fillColor:
  46. ColorConstants.backgroundColor,
  47. focusedErrorBorder:
  48. const OutlineInputBorder(
  49. borderSide: BorderSide.none,
  50. borderRadius: BorderRadius.all(
  51. Radius.circular(10),
  52. ),
  53. ),
  54. errorBorder: const OutlineInputBorder(
  55. borderSide: BorderSide.none,
  56. borderRadius: BorderRadius.all(
  57. Radius.circular(10)),
  58. ),
  59. errorStyle: GoogleFonts.outfit(
  60. fontSize: 14.0,
  61. fontWeight: FontWeight.normal,
  62. color: Colors.red),
  63. focusedBorder: const OutlineInputBorder(
  64. borderSide: BorderSide.none,
  65. borderRadius: BorderRadius.all(
  66. Radius.circular(10)),
  67. ),
  68. border: const OutlineInputBorder(
  69. borderSide: BorderSide.none,
  70. borderRadius: BorderRadius.all(
  71. Radius.circular(10),
  72. ),
  73. ),
  74. ),
  75. ),
  76. ),
  77. ),
  78. onTap: () async {
  79. TimeOfDay? pickedTime =
  80. await showTimePicker(
  81. context: context,
  82. initialTime: TimeOfDay.now(),
  83. initialEntryMode:
  84. TimePickerEntryMode.dial);
  85. if (pickedTime != null) {
  86. setState(
  87. () {
  88. addActivityStartTimeController
  89. .text = pickedTime.toString();
  90. },
  91. );
  92. }
  93. },

The current view Show some text with time.
Can someone plz explain me how can i show current date and time on text form field

答案1

得分: 1

你做得很好
只需要设置日期字符串格式为Date

"dd/MM/yyy" -> 日期

"hh:mm" -> 时间

addActivityStartTimeController.text = DateFormat("dd/MM/yyyy hh:mm").format(DateTime.now());

英文:

you are doing well
just need to set the datetime string format for Date too

"dd/MM/yyy" -> date

"hh:mm" -> time

  1. addActivityStartTimeController.text = DateFormat("dd/MM/yyyy hh:mm").format(DateTime.now());

huangapple
  • 本文由 发表于 2023年7月12日 21:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76670933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定