I have a CheckBox
items selection screen (opened when Navigator.push is triggered in a floating button) to check the items that I like and I want these items to show on the screen when Navigator.pop is used (when a floating button is tapped) to go back to the original screen from where it originated. But I'm not able to do it, I think list.map will be used here but have I'm able to implement it properly. Any help is really appreciated.
You can check the entire code here.
My code where I've problem:
// 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
},
),
);
}
}
Here The original screen, to where I want to return
the _saved
list (i've used a Set as to avoid duplicates)
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,
),
],
);
}
}