我有一个带有小部件和索引的数组。使用索引,我知道选择了哪个小部件。小部件的阵列位于主体中。如何在Array的一个小部件中使用setState()? 在我要使用setState的位置上带有setState()的注释????
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
List<Widgets> _widgetOptions = <Widget> [
.
.
.
Container(
margin:
new EdgeInsets.symmetric(horizontal: 10.0, vertical: 5),
child: Align(
alignment: Alignment.topRight,
child: IconButton(
icon: new Icon(
videoslist[index].isFavorite
? Icons.star
: Icons.star_border,
color: Colors.yellowAccent[200],
size: 40),
onPressed: () {
//setState????
},
),
),
)
]
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black87,
centerTitle: true,
title: Text("")
leading: new Image.asset(
'assets/icon2.png',
),
),
body: Center(
child:
_widgetOptions.elementAt(_selectedIndex), //Array with widgets
),
}
答案 0 :(得分:0)
Set状态,但是它需要更新变量。我已经修改了您的示例以更改颜色;
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
void initState() {
_color = Colors.yellowAccent[200];
super.initState();
}
Color _color;
List<Widgets> _widgetOptions = <Widget> [
.
.
.
Container(
margin:
new EdgeInsets.symmetric(horizontal: 10.0, vertical: 5),
child: Align(
alignment: Alignment.topRight,
child: IconButton(
icon: new Icon(
videoslist[index].isFavorite
? Icons.star
: Icons.star_border,
color: _color,
size: 40),
onPressed: () {
setState((){
_color = Colors.Red;
});
},
),
),
)
]