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

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

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());

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:

确定