如何在Flutter的DateTimeField小部件中禁用键盘输入?

问题描述 投票:0回答:2

我想在Flutter应用程序中制作一个DateTimeField小部件。但是,在该字段中选择一个日期后,我仍然可以使用单击该字段后出现的键盘来编辑该字段。我确实喜欢以下内容。如何禁用此小部件的键盘?

DateTimeField(
  textAlign: TextAlign.center,
  format: format,
  style: TextStyle(
    color: Colors.white,
    decoration: TextDecoration.none,
    fontWeight: FontWeight.w600,
  ),
  decoration: InputDecoration(
    hintText: "Tanggal Lahir",
    hintStyle: TextStyle(
      color: Colors.white,
      fontWeight: FontWeight.w600,
    ),
    border: InputBorder.none,
  ),
  onShowPicker: (context, currentValue) {
    return showDatePicker(
      context: context,
      firstDate: DateTime(1990,1,1),
      initialDate: currentValue ?? DateTime(2000,6,16),
        lastDate: DateTime(2010,12,30));
     },
   ),
flutter flutter-layout
2个回答
0
投票

仅添加只读参数

         DateTimeField(
            textAlign: TextAlign.center,
            format: format,
            readOnly: true,
            style: TextStyle(
              color: Colors.white,
              decoration: TextDecoration.none,
              fontWeight: FontWeight.w600,
            ),
            decoration: InputDecoration(
              hintText: "Tanggal Lahir",
              hintStyle: TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.w600,
              ),
              border: InputBorder.none,
            ),
            onShowPicker: (context, currentValue) {
              return showDatePicker(
                  context: context,
                  firstDate: DateTime(1990, 1, 1),
                  initialDate: currentValue ?? DateTime(2000, 6, 16),
                  lastDate: DateTime(2010, 12, 30));
            },
          ), 

0
投票

尝试一下:

// set readOnly property to true,

DateTimeField(
  textAlign: TextAlign.center,
  format: format,
  style: TextStyle(
    color: Colors.white,
    decoration: TextDecoration.none,
    fontWeight: FontWeight.w600,
  ),
  decoration: InputDecoration(
    hintText: "Tanggal Lahir",
    hintStyle: TextStyle(
      color: Colors.white,
      fontWeight: FontWeight.w600,
    ),
    border: InputBorder.none,
  ),
  onShowPicker: (context, currentValue) {
    return showDatePicker(
      context: context,
      firstDate: DateTime(1990,1,1),
      initialDate: currentValue ?? DateTime(2000,6,16),
        lastDate: DateTime(2010,12,30));
     },

      readOnly: true,  //Add this line


     ),
© www.soinside.com 2019 - 2024. All rights reserved.