I have tried in many ways to pass the variable points but I can not use it in another class below I leave the basic code.the variable in question is points which is located in the pointspage and I would like to use it in cartpage
CartPage
class CartPage extends StatefulWidget {
_CartPagestate createState() => _CartPagestate();
}
class _CartPagestate extends State<CartPage> {
@override
Widget build(BuildContext context) {//some code...}
PointsPage
class points extends StatefulWidget {
_pointsState createState() => _pointsState();
}
class _pointsState extends State<cubox> {
int points = 0;
@override
void initState() {
super.initState();
_loadPoints();
}
_loadPunti() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
punti = (prefs.getInt('points') ?? 0);
});
}
_savePunti() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('points', points);
}
@override
Widget build(BuildContext context) { // some code...}
the variable in question is points which is located in the pointspage and I would like to use it in cartpage. the cartpage also has a cardmodel I would like to use the points variable to make "purchases" but I can't.
Cartmodel
class CartModel extends ChangeNotifier {
// list of items on sale
final List _shopItems = const [
// [ itemName, itemPrice, imagePath, color ]
["Avocado", "4", "assets/images/avocado.png", Colors.green],
["Banana", "2", "assets/images/banana.png", Colors.yellow],
["chicken", "12", "assets/images/chicken.png", Colors.brown],
["water", "1", "assets/images/water.png", Colors.blue],
["apple", "3", "assets/images/mela.png", Colors.red],
["chocolate", "3", "assets/images/broccoli.png", Colors.brown],
];
final List _other = const [
["Avocado", "14000", "assets/images/avocado.png", Colors.orange],
["Banana", "2", "assets/images/banana.png", Colors.blueGrey],
["chicken", "12", "assets/images/chicken.png", Colors.red],
["water", "1", "assets/images/water.png", Colors.blue],
];
// list of cart items
final List _cartItems = [];
get other => _other;
get cartItems => _cartItems;
get shopItems => _shopItems;
// add item to cart
void addItemToCart(int index) {
_cartItems.add(_shopItems[index]);
notifyListeners();
}
// remove item from cart
void removeItemFromCart(int index) {
_cartItems.removeAt(index);
notifyListeners();
}
// calculate total price
String calculateTotal() {
double totalPrice = 0;
for (int i = 0; i < cartItems.length; i++) {
totalPrice += double.parse(cartItems[i][1].toString());
}
return totalPrice.toStringAsFixed(2);
}
}