I have a counter with a price and buttons with price increment and decrement. I do it all through bloc, but I ran into an error that when I click on the buttons, my price does not change. Accordingly, the state does not change, as I understand it. What exactly is the reason and how can I fix the error so that the price changes when you click on the buttons?
price_counter
class PriceCounter extends StatelessWidget {
PriceCounter({Key? key, required this.price}) : super(key: key);
double price = 0.13;
@override
Widget build(BuildContext context) {
final FilterPriceCubit cubit = BlocProvider.of<FilterPriceCubit>(context);
return BlocBuilder<FilterPriceCubit, FilterPriceState>(
builder: (context, state) {
if (state is FilterPriceInitial) {
state.fitlerPrice = price;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 21),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () =>
cubit.priceFilterDecrement(state.fitlerPrice),
icon: SvgPicture.asset(constants.Assets.minus),
constraints: const BoxConstraints(),
padding: EdgeInsets.zero,
),
const SizedBox(width: 20),
InputFieldPrice(
price: state.fitlerPrice,
textStyle: constants.Styles.normalBookTextStyleWhite),
const SizedBox(width: 20),
IconButton(
onPressed: () =>
cubit.priceFilterIncrement(state.fitlerPrice),
icon: SvgPicture.asset(constants.Assets.plus),
constraints: const BoxConstraints(),
padding: EdgeInsets.zero,
),
],
),
);
}
return const SizedBox();
},
);
}
}
input_fields_price
class InputFieldPrice extends StatefulWidget {
final double price;
final TextStyle textStyle;
const InputFieldPrice(
{Key? key, required this.price, required this.textStyle})
: super(key: key);
@override
State<InputFieldPrice> createState() => _InputFieldPrice();
}
class _InputFieldPrice extends State<InputFieldPrice> {
final _formKey = GlobalKey<FormState>();
final _priceController = TextEditingController();
@override
void dispose() {
_priceController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => SizedBox(
width: 100,
child: Form(
key: _formKey,
child: TextFormField(
keyboardType: TextInputType.number,
controller: _priceController
..text = widget.price.toStringAsFixed(2),
style: widget.textStyle,
textAlign: TextAlign.center,
decoration: InputDecoration(
prefix: Text('JC', style: widget.textStyle),
suffix: const Text(
'KWh',
style: constants.Styles.smallerBookTextStyleWhite,
),
contentPadding: EdgeInsets.zero,
border: InputBorder.none,
),
inputFormatters: [
LengthLimitingTextInputFormatter(4),
FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
],
),
),
);
}
price_cubit
class FilterPriceCubit extends Cubit<FilterPriceState> {
FilterPriceCubit() : super(FilterPriceInitial(0.15));
void priceFilterIncrement(double priceFilter) {
double priceIncrement = priceFilter 0.01;
emit(FilterPriceInitial(priceIncrement));
}
void priceFilterDecrement(double priceFilter) {
double priceDecrement =
priceFilter >= 0.01 ? priceFilter - 0.01 : priceFilter - 0;
emit(FilterPriceInitial(priceDecrement));
}
}
price_state_cubit
abstract class FilterPriceState {}
class FilterPriceInitial extends FilterPriceState {
double fitlerPrice;
FilterPriceInitial(this.fitlerPrice);
}
CodePudding user response:
Everytime the bloc builder is called the state.fitlerPrice is set to price. Please remove this from bloc builder. Rest everything seems to be good.
state.fitlerPrice = price;