如何解决Flutter-如何使用“切换”按钮启用TextFormField
我禁用了TextFormField,我想使用“自定义开关”再次启用它。包裹为here 这是代码
bool isEnabled = false;
CustomSwitch(
activeColor: Colors.greenAccent,value: isEnabled,onChanged: (value) {
setState(() {
isEnabled = value;
});
},),TextFormField(
enabled: isEnabled,decoration: InputDecoration(labelText: 'Name'),
您知道如何使用切换按钮启用/禁用textformfield吗? 如果我使用复选框而不是switch也可以。 TIA!
解决方法
此代码段对我来说很好用。
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
),TextFormField(
enabled: status,decoration: InputDecoration(
labelText: 'Name',disabledBorder: InputBorder.none),)
],);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。