Flutter 颤振-如何使用开关按钮启用TextFormField
我禁用了TextFormField,并希望使用自定义开关再次启用它。包裹是 这就是代码Flutter 颤振-如何使用开关按钮启用TextFormField,flutter,flutter-web,Flutter,Flutter Web,我禁用了TextFormField,并希望使用自定义开关再次启用它。包裹是 这就是代码 bool isEnabled = false; CustomSwitch( activeColor: Colors.greenAccent, value: isEnabled, onChanged: (value) { setState(() { isEnabled = value; }); }, ), TextFormField(
bool isEnabled = false;
CustomSwitch(
activeColor: Colors.greenAccent,
value: isEnabled,
onChanged: (value) {
setState(() {
isEnabled = value;
});
},
),
TextFormField(
enabled: isEnabled,
decoration: InputDecoration(labelText: 'Name'),
),
您知道如何使用开关按钮启用/禁用textformfield吗?
如果我用checkbox代替switch也没关系。蒂亚 这段代码对我来说运行良好
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
bool status = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Switch Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomSwitch(
activeColor: Colors.pinkAccent,
value: status,
onChanged: (value) {
print("VALUE : $value");
setState(() {
status = value;
});
},
),
SizedBox(height: 12.0,),
Text('Value : $status', style: TextStyle(
color: Colors.black,
fontSize: 20.0
),),
SizedBox(height: 12.0,),
TextFormField(
enabled: status,
decoration: InputDecoration(
labelText: 'Name', disabledBorder: InputBorder.none),
)
],
),
),
);
}
}
类主屏幕扩展StatefulWidget{
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
布尔状态=假;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“自定义开关示例”),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
自定义开关(
activeColor:Colors.pinkAccent,
价值:地位,
一旦更改:(值){
打印(“值:$VALUE”);
设置状态(){
状态=价值;
});
},
),
尺寸箱(高度:12.0,),
Text('值:$status',样式:TextStyle(
颜色:颜色,黑色,
字体大小:20.0
),),
尺寸箱(高度:12.0,),
TextFormField(
启用:状态,
装饰:输入装饰(
labelText:'Name',disabledBorder:InputBorder.none),
)
],
),
),
);
}
}
上述代码段是否不起作用?是的,开关工作时,它可以启用和禁用,但当开关打开/关闭时,textformfield无法启用/禁用。它保持禁用/错误。这两个小部件是否显示在同一屏幕上?如果是,则enabled
参数应按预期工作。不在as前面,但set state调用不应被isEnabled!=我可以从真到假再返回?是@MaadhavSharma。它在同一个屏幕上。啊。。我明白了,也许问题出在我的CustomSwitch和AlertDialog中的TextFormField上。谢谢@CodeFloat True,警报对话是一个无状态小部件。要在其中设置状态,您需要在警报对话框中使用StatefulBuilder。如果需要帮助,一定要告诉我