英文:
HOW TO SHOW CURRENT TIME ON TEXTFIELD
问题
以下是您要翻译的代码部分:
Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: TextFormField(
onTap: () async {
TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
initialEntryMode: TimePickerEntryMode.dial,
);
if (pickedTime != null) {
setState(
() {
addActivityStartTimeController.text =
DateFormat("hh:mm").format(DateTime.now());
},
);
}
},
readOnly: true,
minLines: 1,
maxLines: 1,
validator: (value) => addActivityStartTimeValidator(value!),
controller: addActivityStartTimeController,
cursorColor: Colors.black,
style: GoogleFonts.outfit(fontSize: 16, color: Colors.black),
decoration: InputDecoration(
prefixIcon: Icon(Icons.watch_later_outlined),
alignLabelWithHint: true,
filled: true,
hintText: "10:20",
hintStyle: GoogleFonts.outfit(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
color: Colors.black54,
),
fillColor: ColorConstants.backgroundColor,
focusedErrorBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
errorBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
errorStyle: GoogleFonts.outfit(
fontSize: 14.0,
fontWeight: FontWeight.normal,
color: Colors.red,
),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
border: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
),
),
),
onTap: () async {
TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
initialEntryMode: TimePickerEntryMode.dial,
);
if (pickedTime != null) {
setState(
() {
addActivityStartTimeController.text = pickedTime.toString();
},
);
}
},
请注意,我已经将HTML实体(如"
)转换为普通的引号,以使代码更容易阅读。如果您有任何其他疑问,请随时提出。
英文:
Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: TextFormField(
onTap: () async {
TimeOfDay? pickedTime =
await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
initialEntryMode:
TimePickerEntryMode.dial);
if (pickedTime != null) {
setState(
() {
addActivityStartTimeController
.text =
DateFormat("hh:mm")
.format(DateTime.now());
},
);
}
},
readOnly: true,
minLines: 1,
maxLines: 1,
validator: (value) =>
addActivityStartTimeValidator(value!),
controller:
addActivityStartTimeController,
// keyboardType: TextInputType.emailAddress,
cursorColor: Colors.black,
style: GoogleFonts.outfit(
fontSize: 16, color: Colors.black),
decoration: InputDecoration(
prefixIcon:
Icon(Icons.watch_later_outlined),
alignLabelWithHint: true,
filled: true,
hintText: "10:20",
hintStyle: GoogleFonts.outfit(
textStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400),
color: Colors.black54),
fillColor:
ColorConstants.backgroundColor,
focusedErrorBorder:
const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
errorBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(10)),
),
errorStyle: GoogleFonts.outfit(
fontSize: 14.0,
fontWeight: FontWeight.normal,
color: Colors.red),
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(10)),
),
border: const OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
),
),
),
onTap: () async {
TimeOfDay? pickedTime =
await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
initialEntryMode:
TimePickerEntryMode.dial);
if (pickedTime != null) {
setState(
() {
addActivityStartTimeController
.text = pickedTime.toString();
},
);
}
},
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
addActivityStartTimeController.text = DateFormat("dd/MM/yyyy hh:mm").format(DateTime.now());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论