我有一个CheckBox
项目选择屏幕(在 float 按钮中触发Navigator.push时打开)以检查我喜欢的项目,并且我希望这些项目在使用Navigator.pop时(在轻按 float 按钮时在屏幕上显示) )返回到其原始屏幕。但是我无法做到这一点,我认为list.map将在这里使用,但是我能够正确实现它。任何帮助都非常感谢。
您可以检查整个代码here。
我遇到问题的代码:
// CheckBox Item Selection Screen!
class FavoriteList extends StatefulWidget {
@override
_FavoriteListState createState() => _FavoriteListState();
}
class _FavoriteListState extends State<FavoriteList> {
final Set _saved = Set();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Add to Favorites!'), centerTitle: true, backgroundColor: Colors.red),
// backgroundColor: Colors.indigo,
body: SafeArea(
child: ListView.builder(
itemCount: 53,
itemBuilder: (context, index) {
return CheckboxListTile(
activeColor: Colors.red,
checkColor: Colors.white,
value: _saved.contains(index),
onChanged: (val) {
setState(() {
if(val == true){
_saved.add(index);
} else{
_saved.remove(index);
}
});
},
title: Row(
children: <Widget>[
Image.asset('lib/images/${images[index]}'),
SizedBox(width: 10,),
Text(nameOfSite[index]),
],
),
);
},
),
),
floatingActionButton: FloatingActionButton(foregroundColor: Colors.red,
child: Icon(Icons.check),
onPressed: (){
Navigator.pop(context, _saved); // Navigator.pop
},
),
);
}
}
在此原始屏幕上,我要对
return
列表进行_saved
的位置(我使用Set来避免重复)class SecondPage extends StatefulWidget {
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return
// if the `_saved` list contains something return the "_saved" list from
the CheckBox item selection screen, if not then
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Add Your Favorite Sites Here!❤',
style: TextStyle(color: Colors.white),
),
Container(
child: Icon(Icons.favorite, size: 150, color: Colors.blue[100]),
),
SizedBox(height: 250),
FloatingActionButton(
onPressed: () {
Navigator.push( //Navigator.push is here!
context,
MaterialPageRoute(
builder: (context) => FavoriteList(),
),
);
},
child: Icon(Icons.add),
foregroundColor: Colors.blue,
),
],
);
}
}
最佳答案
您可以使用Event_Bus在Screen之间传递数据。
这是lib
https://pub.dev/packages/event_bus
关于flutter - 在复选框选择屏幕上使用Navigator.pop时,如何在其他屏幕上显示列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59350615/