如何在 statelessWidget 中使用 setState() 函数 class
How to use setState() function inside statelessWidget class
我无法在 dialogContent
中使用 setState
函数,我得到了这个错误:
The method 'setState' isn't defined for the class 'CustomDialog'
这里我用了setState()
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: status ? <Widget>[
Container(
child: Padding(
padding: EdgeInsets.only(right: 5,top: 0),
child: Image.asset(
'assets/images/profile.png',
width: 60.0,
height: 60.0,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 0.0),
child: Container(
width: 200.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(50.0),
topRight: const Radius.circular(50.0),
bottomLeft: const Radius.circular(50.0),
bottomRight: const Radius.circular(50.0),
),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Text(
'test',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 25.0
),
),
),
),
)
),
Padding(
padding: const EdgeInsets.only(left: 5.0),
child: CustomSwitch(
activeColor: Colors.green,
value: status,
onChanged: (value) {
print("VALUE : $value");
setState(() {
status = value;
});
},
),
),
] :
[
Expanded(
child: Padding(
padding: const EdgeInsets.only(left :25.0),
child: Center(
child: Text(
'test',
style: TextStyle(
color: Colors.red,
fontSize: 15.0,
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: CustomSwitch(
activeColor: Colors.green,
value: status,
onChanged: (value) {
print("VALUE : $value");
setState(() {
status = value;
});
},
),
),
]
),
当然不能在StatelessWidget 中使用setState(),这就是这个widget 的想法。
StatelessWidget 应该只用于不应保持任何状态的“哑”视图。
如果您应该为小部件设置任何状态,请考虑使用 StatefulWidget。
查看 flutter 官方文档:
https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html
我无法在 dialogContent
中使用 setState
函数,我得到了这个错误:
The method 'setState' isn't defined for the class 'CustomDialog'
这里我用了setState()
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: status ? <Widget>[
Container(
child: Padding(
padding: EdgeInsets.only(right: 5,top: 0),
child: Image.asset(
'assets/images/profile.png',
width: 60.0,
height: 60.0,
),
),
),
Padding(
padding: const EdgeInsets.only(top: 0.0),
child: Container(
width: 200.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(50.0),
topRight: const Radius.circular(50.0),
bottomLeft: const Radius.circular(50.0),
bottomRight: const Radius.circular(50.0),
),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Text(
'test',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 25.0
),
),
),
),
)
),
Padding(
padding: const EdgeInsets.only(left: 5.0),
child: CustomSwitch(
activeColor: Colors.green,
value: status,
onChanged: (value) {
print("VALUE : $value");
setState(() {
status = value;
});
},
),
),
] :
[
Expanded(
child: Padding(
padding: const EdgeInsets.only(left :25.0),
child: Center(
child: Text(
'test',
style: TextStyle(
color: Colors.red,
fontSize: 15.0,
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: CustomSwitch(
activeColor: Colors.green,
value: status,
onChanged: (value) {
print("VALUE : $value");
setState(() {
status = value;
});
},
),
),
]
),
当然不能在StatelessWidget 中使用setState(),这就是这个widget 的想法。 StatelessWidget 应该只用于不应保持任何状态的“哑”视图。 如果您应该为小部件设置任何状态,请考虑使用 StatefulWidget。
查看 flutter 官方文档: https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html