我禁用了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!
答案 0 :(得分:1)
此代码段对我来说很好用。
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),
)
],
),
),
);
}
}