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