【发布时间】:2020-04-08 13:29:04
【问题描述】:
我有一个CheckBox 项目选择屏幕(在浮动按钮中触发 Navigator.push 时打开)来检查我喜欢的项目,并且我希望这些项目在使用 Navigator.pop 时显示在屏幕上(当一个浮动按钮被轻敲)返回到它起源的原始屏幕。但我做不到,我认为 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,
),
],
);
}
}
【问题讨论】:
-
请显示调用 Navigator.push 的位置。你可以利用返回值,不是吗?
-
@LoL 抱歉,现在我已经编辑了问题,您现在可以看看!谢谢