【问题标题】:How to disable the textfield in textfomrfield?如何禁用 textformrfield 中的文本字段?
【发布时间】:2021-07-27 01:22:36
【问题描述】:

我使用 textformfield 进行验证,使用表单而不是文本我想要的是让用户选择日期。所以这是我的问题如何禁用在文本字段上点击打开键盘以便我可以显示我的 Datepicker?这是我的小部件

 String _date = "Please pick Age";

  Widget _buildage() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Text(
        'Enter Age',
        style: kLabelStyle,
      ),
      SizedBox(height: 10.0),
      GestureDetector(
        onTap: () {
          DatePicker.showDatePicker(context,
              theme: DatePickerTheme(
                containerHeight: 210.0,
              ),
              showTitleActions: true,
              minTime: DateTime(2000, 1, 1),
              maxTime: DateTime(2022, 12, 31), onConfirm: (date) {
            print('confirm $date');

            setState(() {
              _date = '${date.year} - ${date.month} - ${date.day}';
            });
          }, currentTime: DateTime.now(), locale: LocaleType.en);
        },
        child: Container(
          decoration: kBoxDecorationStyle,
          alignment: Alignment.centerLeft,
          height: 60.0,
          child: Container(
            child: TextFormField(
              decoration: InputDecoration(
                border: InputBorder.none,
                contentPadding: EdgeInsets.only(top: 14.0),
                prefixIcon: Icon(
                  Icons.date_range_rounded,
                  color: Colors.white,
                  size: 28,
                ),
                hintText: " $_date",
                hintStyle: kHintTextStyle,
              ),
            ),
          ),
        ),
      ),
    ]);
  }

希望任何人都可以帮助我也使用其他一些 Textformfield,但我只想在小部件中禁用它。

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    在 textformfield 中,设置readOnly: true。您还可以使用 textformfield 的 onTap 属性,这样您就不必使用手势检测器了。

    TextFormField(
    readOnly: true,
    onTap: ()=>your function)
    

    【讨论】:

    • 好的,但是另一个问题你知道我为什么会收到这个错误../../flutter/.pub-cache/hosted/pub.dartlang.org/flutter_datetime_picker-1.5.1/lib /flutter_datetime_picker.dart:311:32:警告:null-aware 操作的操作数 '??'具有不包括空值的“颜色”类型。 - “颜色”来自“飞镖:ui”。颜色:theme.backgroundColor ??颜色.白色,^
    • 我看到您发布了一个关于您的新问题的新问题。请考虑将此答案标记为本文中问题的解决方案。
    • stackoverflow.com/questions/67386501/… 你可以看看这个。谢谢兄弟的回答
    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多