merhaba,
bir adet sepet ekranım var sepete eklemede kaldırmada sorun yok fakat sepetteki ürünü sildiğimde setstate çalışmıyor. Ürünü siliyor ama ekranda anlık göremiyorum sayfa değiştirip tekrar sepete döndüğümde silinmiş oluyor. kodlarım şu şekilde;
`
class CartItem extends StatefulWidget {
int index;
final String name;
final String img;
final bool isFav;
final double rating;
final int raters;
CartItem(
{Key key,
@required this.index,
@required this.name,
@required this.img,
@required this.isFav,
@required this.rating,
@required this.raters})
: super(key: key);
@override
_CartItemState createState() => _CartItemState();
}
class _CartItemState extends State<CartItem> {
@override
void initState() {
super.initState();
// print(widget.index);
}
Future<void> refresh() async {
setState(() {});
}
removecart() async {
this.setState(() {
sepet.remove(sepet[widget.index]);
print(sepet.remove(widget.index));
});
SharedPreferences prefs = await SharedPreferences.getInstance();
String stringValue = prefs.getString('sepetkayit');
prefs.setString('sepetkayit', jsonEncode(sepet));
prefs.remove(stringValue);
// var test1 = (json.decode(stringValue) as List<dynamic>).cast<Map>();
// sepet = test1;
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(55), bottom: Radius.circular(55))),
//BorderRadius.all(Radius.circular(65))),
//backgroundColor: Colors.red.shade100,
title: new Text(
"Başarılı",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.green,
),
),
content: new Text(
"Ürün Sepetten Kaldırıldı",
textAlign: TextAlign.center,
),
);
});
return sepet;
}
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.fromLTRB(0, 4, 0, 4),
child: InkWell(
onTap: () {
setState(() {});
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
print(widget.index);
return CartDetails(widget.index);
},
),
);
},
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 0.0, right: 10.0),
child: Container(
height: MediaQuery.of(context).size.width / 3.5,
width: MediaQuery.of(context).size.width / 3,
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.asset(
"${widget.img}",
fit: BoxFit.cover,
),
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"${widget.name}",
style: TextStyle(
// fontSize: 15,
fontWeight: FontWeight.w900,
),
),
SizedBox(height: 10.0),
Row(
children: <Widget>[
SmoothStarRating(
starCount: 1,
color: Constants.ratingBG,
allowHalfRating: true,
rating: 5.0,
size: 12.0,
),
SizedBox(width: 6.0),
Text(
"5.0 (23 Yorum)",
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w300,
),
),
],
),
SizedBox(height: 10.0),
Row(
children: <Widget>[
Text(
"20 Dakika",
style: TextStyle(
fontSize: 11.0,
fontWeight: FontWeight.w300,
),
),
SizedBox(width: 10.0),
Text(
r"₺90",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w900,
color: Theme.of(context).accentColor,
),
),
],
),
SizedBox(height: 10.0),
Text(
"Adet: 1",
style: TextStyle(
fontSize: 11.0,
fontWeight: FontWeight.w300,
),
),
SizedBox(height: 10.0),
GestureDetector(
child: IconButton(
icon: Icon(
Icons.delete,
),
splashColor: Colors.red,
tooltip: "SİL",
onPressed: () {
removecart();
refresh();
setState(() {});
},
),
)
],
),
],
),
),
);
}
}
`
yardımcı olabilecek var mıdır, şimdiden teşekkürler.