Я создаю маркетинговое приложение, и я в основном создаю карточку для каждого Предмета, который покупает покупатель, на карточке есть название товара и цена товара, а также кнопки + и - для изменения их количества элемент, который они хотят, который переключает цену курса, и я сохраняю его в другой переменной с именем (shownPrice), и я обрабатываю общую сумму цен на предметы в основном, а экран покупки и карты покупок имеют свой собственный файл, idk, как инициировать показаны данные о цене каждого элемента обратно на карточки, если они нажали кнопки + и - и покинули экран покупки, а затем вернулись к нему, карточек может быть много, конечно, вот мой код
MAIN:
int shownPriceMain = 0;
int indexMain = 0;
String labelMain;
void changeTotalSpending(
int shownPrice, int itemPrice, int index, String label) {
print('add and sub totalSum: $totalSum');
print('add and sub price: $shownPrice');
print('add and itemPtice: $itemPrice');
print('add and label: $label');
setState(() {
totalSum = totalSum + itemPrice;
shownPriceMain = shownPrice;
indexMain = index;
labelMain = label;
print('labelMain : $labelMain');
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Color(0xffba0100),
accentColor: Color(0xff9aaa0d),
canvasColor: Colors.grey[100],
textTheme: TextTheme().copyWith(
bodyText1: TextStyle(
color: Colors.white,
fontSize: 17.0,
fontWeight: FontWeight.w700,
),
bodyText2: TextStyle(
color: Colors.white,
),
headline6: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
)),
initialRoute: '/',
routes: {
'/': (ctx) => TabsScreen(
toggleFavs,
isFavorite,
favorites,
searchedText,
availableVeggies,
togglePurchases,
totalSum,
subtractTotalSpending,
filteredBySearch,
),
PurchasesScreen.id: (ctx) => PurchasesScreen(
togglePurchases,
purchases,
subtractTotalSpending,
changeTotalSpending,
totalSum,
shownPriceMain,
indexMain,
labelMain),
вот экран моих покупок:
class PurchasesScreen extends StatefulWidget {
static const id = '/Purchases Screen';
final Function togglePurchases;
final List<Tamata> purchases;
final Function resetTor;
final Function changeTotalSpending;
int totalSum = 0;
int shownPriceMain;
int indexMain;
String labelMain;
PurchasesScreen(
this.togglePurchases,
this.purchases,
this.resetTor,
this.changeTotalSpending,
this.totalSum,
this.shownPriceMain,
this.indexMain,
this.labelMain);
@override
_PurchasesScreenState createState() => _PurchasesScreenState();
}
class _PurchasesScreenState extends State<PurchasesScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your Orders'),
actions: [
IconButton(
icon: Icon(
Icons.save,
color: Colors.white,
),
onPressed: () {
widget.changeTotalSpending(pricey);
}),
],
),
body: widget.purchases.isEmpty
? Center(
child: Text(
'You Haven\'t ordered anything yet!',
style: Theme.of(context).textTheme.headline6,
),
)
: Padding(
padding: EdgeInsets.all(8.0),
child: ListView.builder( //i will create a card for every purchased item
itemBuilder: (context, index) {
return PurchasedCards(
price: widget.purchases[index].price,
label: widget.purchases[index].title,
imageUrl: widget.purchases[index].imageUrl,
removeItem: removeFromPurchases,
id: widget.purchases[index].id,
changeTotalSpending: widget.changeTotalSpending,
indexMain: widget.indexMain,
shownPriceMain: widget.shownPriceMain,
labelMain: widget.labelMain,
);
},
itemCount: widget.purchases.length,
),
),
);
}
}
и вот виджет моей карты покупок (кнопка - пока работает!)
class PurchasedCards extends StatefulWidget {
final String imageUrl;
final String label;
int price;
final Function removeItem;
final String id;
final Function changeTotalSpending;
int shownPriceMain;
int indexMain;
String labelMain;
PurchasedCards(
{this.price,
this.imageUrl,
this.label,
this.removeItem,
this.id,
this.changeTotalSpending,
this.indexMain,
this.shownPriceMain,
this.labelMain});
@override
_PurchasedCardsState createState() => _PurchasedCardsState();
}
class _PurchasedCardsState extends State<PurchasedCards> {
var itemPrice;
int itemIndex;
int shownPrice;
@override
void initState() {
if (widget.labelMain == widget.label) {
itemPrice = widget.shownPriceMain;
itemIndex = widget.indexMain;
} else {
itemPrice = widget.price;
itemIndex = 1;
}
super.initState();
}
void addPrice() {
setState(() {
itemPrice = itemPrice + widget.price;
shownPrice = itemPrice;
print('shown price: $shownPrice');
itemIndex++;
});
widget.changeTotalSpending(
shownPrice,
widget.price,
itemIndex,
widget.label,
);
}
@override
Widget build(BuildContext context) {
print('${widget.price} in purchasedcard build');
return Card(
child: Padding(
padding: EdgeInsets.only(right: 10.0),
child: Row(
children: [
Container(
padding: EdgeInsets.all(5),
height: 100.0,
child: Image.asset(widget.imageUrl),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.label,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
Text(
'1 package is 1 Kg',
style: TextStyle(
fontSize: 15.0,
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.w500,
),
),
SizedBox(
height: 10.0,
),
Text(
'$itemPrice',
style: TextStyle(
fontSize: 15.0,
color: Colors.black,
),
),
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// GestureDetector(
// onTap: () {
// setState(() {
// widget.changeTotalSpending(shownPrice, widget.price);
// });
// },
// child: Text(
// 'submit',
// style: TextStyle(
// color: Colors.black,
// ),
// ),
// ),
GestureDetector(
child: Icon(
Icons.delete,
color: Theme.of(context).primaryColor,
size: 30.0,
),
onTap: () {
setState(() {
widget.removeItem(widget.id, widget.price);
});
},
),
SizedBox(
height: 12.0,
),
Row(
children: [
GestureDetector(
child: Icon(Icons.add),
onTap: () {
addPrice();
},
),
SizedBox(
width: 5.0,
),
Container(
height: 25.0,
width: 25.0,
color: Theme.of(context).accentColor,
child: Center(
child: Text('$itemIndex'),
),
),
SizedBox(
width: 5.0,
),
GestureDetector(
child: Icon(Icons.remove),
onTap: () {
// itemPrice = widget.price;
// widget.price = widget.price - itemPrice;
// itemIndex--;
// setState(() {});
// widget.changeTotalSpending(widget.price);
},
),
],
),
],
),
],
),
),
);
}
}