flutter - Flutter - 如何使用 Switch 按钮启用 TextFormField
问题描述
我禁用了 TextFormField,我想使用自定义开关再次启用它。包在这里 ,这是代码
bool isEnabled = false;
CustomSwitch(
activeColor: Colors.greenAccent,
value: isEnabled,
onChanged: (value) {
setState(() {
isEnabled = value;
});
},
),
TextFormField(
enabled: isEnabled,
decoration: InputDecoration(labelText: 'Name'),
),
您知道如何使用切换按钮启用/禁用 textformfield 吗?如果我使用复选框而不是开关也没关系。蒂亚!
解决方案
这个代码片段对我来说很好用。
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),
)
],
),
),
);
}
}