颤振输入字段已禁用

时间:2020-01-06 07:45:32

标签: 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;
    });
  }
}

2 个答案:

答案 0 :(得分:0)

我建议您使用textfieldtextformfield小部件,因为它们具有enable选项。据我了解,具有inputfield的程序包没有启用选项。

答案 1 :(得分:0)

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

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",
)