I am working on an e-commerce app with flutter, so I have to add product with variations,and each variation has its own price and quantity, so I come up with an idea and I woulk like to know if my approach is good or bad,
`class VariantPriceAndStockContainer extends ConsumerStatefulWidget {
const VariantPriceAndStockContainer({super.key});
@override
ConsumerState<VariantPriceAndStockContainer> createState() =>
_VariantPriceAndStockContainerState();
}
class _VariantPriceAndStockContainerState
extends ConsumerState<VariantPriceAndStockContainer> {
late List<TextEditingController> _priceControllers;
late List<TextEditingController> _stockControllers;
@override
void initState() {
super.initState();
_initializeControllers();
}
void _initializeControllers() {
final variations = ref.read(productVariationProvider);
_priceControllers = List.generate(
variations.length,
(index) => TextEditingController(
text: variations[index].price.toString(),
),
);
_stockControllers = List.generate(
variations.length,
(index) => TextEditingController(
text: variations[index].stock.toString(),
),
);
}
void _updateControllers() {
// Dispose old controllers
for (var controller in _priceControllers) {
controller.dispose();
}
for (var controller in _stockControllers) {
controller.dispose();
}
final variations = ref.read(productVariationProvider);
_priceControllers = List.generate(
variations.length,
(index) => TextEditingController(
text: variations[index].price.toString(),
),
);
_stockControllers = List.generate(
variations.length,
(index) => TextEditingController(
text: variations[index].stock.toString(),
),
);
}
@override
void dispose() {
for (var controller in _priceControllers) {
controller.dispose();
}
for (var controller in _stockControllers) {
controller.dispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
ref.listen(productVariationProvider, (previous, next) {
if (previous != next) {
_updateControllers();
}
});
final variations = ref.watch(productVariationProvider);
return ListView.builder(
shrinkWrap: true,
itemCount: variations.length,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
//Determine What to display based on the selected attributes
String displayText = '';
if (variations[index].attributesValues.length >= 2) {
displayText = variations[index].attributesValues.values.join(' - ');
} else {
displayText = variations[index].attributesValues.values.join('');
}
return Container(
width: double.infinity,
height: 115.h,
margin: EdgeInsets.only(top: 15.h),
padding: EdgeInsets.all(10.w),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.r),
boxShadow: [
BoxShadow(
color: Colors.grey.withValues(alpha: 0.2),
blurRadius: 7,
spreadRadius: 5,
),
],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 10.h,
children: [
Text.rich(
TextSpan(
children: [
TextSpan(
text: "Variante: ",
style: Theme.of(context).textTheme.labelMedium,
),
TextSpan(
text: displayText,
),
],
),
),
Row(
spacing: 10.w,
children: [
Expanded(
child: AppTextFormField(
controller: _priceControllers[index],
labelText: "Prix",
onChanged: (value) {
double price = double.tryParse(value) ?? 0.0;
ref
.read(productVariationProvider.notifier)
.onVariationPrice(index, price);
},
),
),
Expanded(
child: AppTextFormField(
controller: _stockControllers[index],
labelText: "Stock",
onChanged: (value) {
int stock = int.tryParse(value) ?? 0;
ref
.read(productVariationProvider.notifier)
.onVariationStock(index, stock);
},
),
)
],
)
],
),
);
},
);
}
}
`
so my reason of doing this is whenever I add a variation a new form is variation price and quantity is created, so when I delete the so called variation I want to re-initialize the other variation that a in list so that I can put value again, because without this when i delete the value of the deleted variation gp the the above variation.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744939831a4602251.html
评论列表(0条)