我在 ListView 中为每个元素创建一个 Switch 小部件。当我单击开关时,所有元素的值都会更改。
我检查了 flutter 文档、dart 文档...我想,我没有恢复 onChanged 方法中的元素位置。
但怎么办呢?
当我单击时,我的值已切换更改。
当我点击时,我恢复了 lignes,这要归功于:snapshot.data[index].name
主题屏幕:
class _ThemePage extends State<ThemePage> {
bool isSwitched = false;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: Text("Blackbox"),
leading: Image.asset(
'assets/img/logo_ineat.png',
fit: BoxFit.contain,
height: 32,
),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Container(
margin: EdgeInsets.only(top: 10.0, bottom: 10.0),
child: new Text('Sélection du profil',
style: new TextStyle(
fontSize: 24, fontWeight: FontWeight.bold)),
alignment: Alignment.topCenter),
new Flexible(
child: new Container(
child: new FutureBuilder<List<t.Theme>>(
future: t.Theme().getThemes(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
if (snapshot.data != null) {
return new Column(
children: <Widget>[
new Expanded(
child: new ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) {
return ListTile(
title:
new Text(snapshot.data[index].name),
trailing: new Switch(
value: isSwitched,
activeColor: Colors.pink,
activeTrackColor: Colors.pinkAccent,
onChanged: (value){
setState(() {
isSwitched = value;
if(value==true) {
print(snapshot.data[index].name);
}
});
},
),
);
},
),
),
],
);
}
} else {
new Text("Loading...");
return new CircularProgressIndicator();
}
}),
),
),
new Container(
margin: EdgeInsets.only(right: 10.0),
child: new RaisedButton.icon(
/*onPressed: () {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => Technologies()));
},*/
label: Text('Suivant'),
icon: Icon(Icons.navigate_next),
),
alignment: Alignment.bottomRight,
),
],
),
),
);
}
}