I have a ListView.builder
with Items and we can click on a heart to add to favorite. I want to put all Items which were add to Favorite in an other ListView.builder
in an other screen. I want too the Favorite items can be supressed of the Favorite ListView by click on the heart. How I can do that? This is my code :
Home_screen.dart
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';
import 'package:chrolix/constants.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
int itemCount = item.length;
List<bool> selected = new List<bool>();
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
initState() {
for (var i = 0; i < itemCount; i++) {
selected.add(false);
}
super.initState();
}
Icon notFavorite = Icon(Icons.favorite_border, size: 32,);
Icon inFavorite = Icon(Icons.favorite, size: 32,);
@override
Widget build(BuildContext context) {
return new Scaffold(
body: ListView.builder(
scrollDirection: Axis.vertical,
physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
itemCount: itemCount,
itemBuilder: (BuildContext context, int index) {
return Container(
child: new Row(
children: <Widget>[
//Image
new Container(
margin: new EdgeInsets.only(top: 5.0, left: 0.0),
child: new CachedNetworkImage(
imageUrl: item[index].imageURL,
height: MediaQuery.of(context).size.width / 3,
width: MediaQuery.of(context).size.width / 2,
fit: BoxFit.cover,
),
),
new Container(
height: MediaQuery.of(context).size.width / 3,
width: MediaQuery.of(context).size.width / 2,
child : new Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
//Text
new Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Spacer(),
//Titre
Container(
padding: const EdgeInsets.only(top: 15.0 ),
child: Text(
item[index].title,
style: kItemTitle,
),
),
//Decription
Container(
padding: const EdgeInsets.only(left: 10.0, top: 15.0),
child:Text(
item[index].description,
style: kItemDescription,
),
),
Spacer(),
],
),
Container(
padding: const EdgeInsets.only(top: 10.0),
child: CountdownTimer(
daysSymbol: new Text("j "),
hoursSymbol: new Text(" : "),
minSymbol: new Text(" : "),
secSymbol: new Text(""),
endTime: item[index].countdown,
textStyle: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
onEnd: () {
print('onEnd');
},
),
),
Container(
padding: const EdgeInsets.only(left: 8.0, top: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: selected.elementAt(index) ? inFavorite : notFavorite,
onTap: () {
setState(() {
selected[index] = !selected.elementAt(index);
});
},
),
],),
),
],),
),
],
),
);
}
)
);
}
}
Favorite_screen.dart
import 'package:flutter/material.dart';
import 'package:chrolix/constants.dart';
import 'package:chrolix/nav.dart';
import 'package:get/get.dart';
class Favoris extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: //Put here the LisView.builder with the favorite items
)
}
}
I have don't seen other questions that can answer to mine. Thanks !
Update : I have used provider's package
The recommanded way to do this is to use provider. This is used to share variables to multiple widget down the tree.
This takes more than a StackOverflow answer to write so I will link you to a relevant article from flutter which basically explain everything you want to do: Here is is.
If the link is broken you can find it by searching "Simple app state management flutter"