Flutter 输入字段已禁用

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

Flutter input field disabled

问题

以下是翻译好的部分:

"I am new to Flutter and require to make some modification to this application. How to disable the input field from being able to enter text in the field. I tried using enabled: false but it is not working and consist syntax error for enabled."

"这是我第一次使用Flutter,需要对这个应用程序进行一些修改。如何禁用输入字段以防止在字段中输入文本。我尝试使用 enabled: false 但它不起作用,并且出现了 enabled 的语法错误。"

"This is the main file which uses InputField"

"这是使用 InputField 的主要文件"

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

"This is the code for the widget InputField."

"这是 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;
    });
  }
}
英文:

I am new to Flutter and require to make some modification to this application. How to disable the input field from being able to enter text in the field. I tried using enabled: false but it is not working and consist syntax error for enabled.

This is the main file which uses InputField

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

This is the code for the widget InputField.

import &#39;package:flutter/material.dart&#39;;
import &#39;../../styles/CommonStyle.dart&#39;;
import &#39;DecoCard.dart&#39;;
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() =&gt; _InputFieldState(
text: text,
obscureText: obscureText,
password: password,
email: email,
number: number,
onSaved: onSaved,
controller: controller
);
}
class _InputFieldState extends State&lt;InputField&gt; {
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;
});
}
}

答案1

得分: 0

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

英文:

I'd recommend you use the textfield or textformfield widgets as they have the enable option. From what I learnt, the package that has the inputfield does not have an enable option.

答案2

得分: 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; // 在此添加了enabled选项到InputField
  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, // 默认情况下启用为true,这通常是情况
    this.controller
  });

  @override
  _InputFieldState createState() => _InputFieldState(
    text: text,
    obscureText: obscureText,
    password: password,
    email: email,
    number: number,
    enabled: enabled, // 将新的enabled字段传递给InputField的State
    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, 

  // 我不知道为什么有时候生活会变得如此复杂。这里的初始化是完全不必要的。

  final TextEditingController controller;

  _InputFieldState({
    this.obscureText,
    this.text,
    this.password,
    this.email,
    this.number,
    this.onSaved,
    this.enabled, // 将其传递给自定义小部件的构建方法
    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, 

                  // 只需添加enable:enable也应该可以,请确认一下。

                  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",
)
英文:

As there is a customized InputField added by the developer. They have not passed the enabled method down the custom widget line. This widget ultimately uses TextFormField so apparently we can add enabled option to this.

import &#39;package:flutter/material.dart&#39;;
import &#39;../../styles/CommonStyle.dart&#39;;
import &#39;DecoCard.dart&#39;;
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() =&gt; _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&lt;InputField&gt; {
String text;
bool obscureText = false;
bool password = false;
bool email = false;
bool number = false;
var onSaved = null;
bool enabled = true, 
// I don&#39;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;
});
}
}

Now you can use InputField as :

InputField(
enabled:false,
text:&quot;Done :D&quot;,
)

huangapple
  • 本文由 发表于 2020年1月6日 15:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定