flutter - flutter 输入字段已禁用

标签 flutter dart

我是Flutter的新手,需要对此应用程序进行一些修改。如何禁止输入字段能够在该字段中输入文本。我尝试使用enabled: false,但无法正常工作,并且包含enabled的语法错误。

这是使用InputField的主文件

import 'package:x/widgets/common/InputField.dart';
....
InputField(
   text: 'Incident address',
   controller: this._addressController,
),

这是小部件InputField的代码。
import 'package:flutter/material.dart';
import '../../styles/CommonStyle.dart';
import 'DecoCard.dart';

class InputField extends StatefulWidget {
  final String text;
  final bool obscureText;
  final bool password;
  final bool email;
  final bool number;
  var onSaved;
  final TextEditingController controller;

  InputField({
    this.obscureText = false,
    this.password = false,
    this.text,
    this.email = false,
    this.number = false,
    this.onSaved = null,
    this.controller
  });

  @override
  _InputFieldState createState() => _InputFieldState(
    text: text,
    obscureText: obscureText,
    password: password,
    email: email,
      number: number,
    onSaved: onSaved,
    controller: controller
  );
}

class _InputFieldState extends State<InputField> {
  String text;
  bool obscureText = false;
  bool password = false;
  bool email = false;
  bool number = false;
  var onSaved = null;
  final TextEditingController controller;

  _InputFieldState({
    this.obscureText,
    this.text,
    this.password,
    this.email,
    this.number,
    this.onSaved,
    this.controller
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Material(
        child: DecoCard(
          padding: 0.0,
          child: Row(
            children: [
              Expanded (
                child: TextFormField(
                  controller: this.controller,
                  keyboardType: number ? TextInputType.number : null,
                  obscureText: obscureText ? true : false,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.fromLTRB(20.0, 20.0, 5.0, 20.0),
                    border: InputBorder.none,
                    hintText: text,
                  ),
                  onSaved: (value) { _callParentOnSave(value); },
                ),
              ),

              Container(
                padding: EdgeInsets.only(right: 0.0),
                child: password ? IconButton(
                  icon:  Icon(Icons.remove_red_eye),
                  color: DecoColors.icon,
                  onPressed: _showHide,
                ) : null,
              ),
            ]
          ),
        ),
      )
    );
  }

  _showHide() {
    setState(() {
      obscureText = !obscureText;
    });
  }
}

最佳答案

由于开发人员添加了自定义的InputField。他们尚未在自定义窗口小部件行中传递启用了方法。该小部件最终使用了TextFormField,因此显然我们可以在其中添加启用了的选项

import 'package:flutter/material.dart';
import '../../styles/CommonStyle.dart';
import 'DecoCard.dart';

class InputField extends StatefulWidget {
  final String text;
  final bool obscureText;
  final bool password;
  final bool email;
  final bool number;
  final bool enabled; // Added enabled option to InputField here
  var onSaved;
  final TextEditingController controller;

  InputField({
    this.obscureText = false,
    this.password = false,
    this.text,
    this.email = false,
    this.number = false,
    this.onSaved = null,
    this.enabled = true, // Enable is true by default which is generally the case
    this.controller
  });

  @override
  _InputFieldState createState() => _InputFieldState(
    text: text,
    obscureText: obscureText,
    password: password,
    email: email,
    number: number,
    enabled:enabled, // Pass the new enable field to State of InputField
    onSaved: onSaved,
    controller: controller
  );
}

class _InputFieldState extends State<InputField> {
  String text;
  bool obscureText = false;
  bool password = false;
  bool email = false;
  bool number = false;
  var onSaved = null;
  bool enabled = true, 

  // I don't know why life is so complicated sometimes. Initialization here is completely unnecessary.

  final TextEditingController controller;

  _InputFieldState({
    this.obscureText,
    this.text,
    this.password,
    this.email,
    this.number,
    this.onSaved,
    this.enabled, // Pass it along to custom widget build
    this.controller
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Material(
        child: DecoCard(
          padding: 0.0,
          child: Row(
            children: [
              Expanded (
                child: TextFormField(
                  controller: this.controller,
                  enabled: (enabled)?true:false, 

                  // just adding enable:enable should also work please confirm this.

                  keyboardType: number ? TextInputType.number : null,
                  obscureText: obscureText ? true : false,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.fromLTRB(20.0, 20.0, 5.0, 20.0),
                    border: InputBorder.none,
                    hintText: text,
                  ),
                  onSaved: (value) { _callParentOnSave(value); },
                ),
              ),

              Container(
                padding: EdgeInsets.only(right: 0.0),
                child: password ? IconButton(
                  icon:  Icon(Icons.remove_red_eye),
                  color: DecoColors.icon,
                  onPressed: _showHide,
                ) : null,
              ),
            ]
          ),
        ),
      )
    );
  }

  _showHide() {
    setState(() {
      obscureText = !obscureText;
    });
  }
}

现在,您可以将InputField用作:
InputField(
   enabled:false,
   text:"Done :D",
)

关于flutter - flutter 输入字段已禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608384/

相关文章:

flutter - 保存图像和视频以供离线访问

android-studio - flutter 医生 : Flutter and dart plugins not installed

android - 无法居中列小部件

dart - Dart .onMouseOver

flutter - Flutter 子级对父级的回调

date - 如何在 DART(Flutter)中舍入最接近 30 分钟间隔的日期时间?

dart - 如何在 dart flutter 中从 statefulwidget 类获取变量到状态类?

dart - 如何在Flutter中将 snackbar 设置得非常低?

flutter - 提供商与 Get_it

ios - 从 Flutter/Dart 检测 iOS13 深色模式是否启用