pmatatias / input-quantity

Flutter widget for input quantity item
https://pub.dev/packages/input_quantity
MIT License
12 stars 13 forks source link

Cannot retrieve value of field #55

Closed artandor closed 2 months ago

artandor commented 4 months ago

Hi ! Help needed 😄

I don't understand how to retrieve the value from the field, considering the controller inside of it is private. How can i provide a controller to the field so that i can listen to changes ?

Thanks in advance

pmatatias commented 4 months ago

Hi @artandor , thank you for use this package.

you can listen the value changes from onQtyChanged

InputQty(
    maxVal: 100,
    initVal: 0,
    minVal: -100,
    steps: 10,
    onQtyChanged: (val) {   <<<<<<<<< here
      print(val); 
    },
  ),
artandor commented 4 months ago

Isn't it a fairly bad practice to setState inside a onChange ? Sorry for the questions, i'm mostly junior with flutter

pmatatias commented 4 months ago

can you provide any snippet code? i may can help based on your code

artandor commented 4 months ago

For sure, here it is

InputQty.int(
                            minVal: 10,
                            maxVal: 100,
                            initVal: 10,
                            steps: 10,
                            onQtyChanged: (quantity) {
                              setState(() {
                                _creditsToBuy = quantity;
                              });
                            },
                            messageBuilder:
                                (num minVal, num maxVal, num? value) {
                              if (value == null) return null;
                              if (value > minVal) {
                                return Text(
                                  "Value must be higher than $minVal",
                                  style: const TextStyle(color: Colors.red),
                                  textAlign: TextAlign.center,
                                );
                              } else if (value < maxVal) {
                                return Text(
                                  'Cannot buy more than $maxVal credits',
                                  style: const TextStyle(color: Colors.red),
                                  textAlign: TextAlign.center,
                                );
                              } else {
                                return null;
                              }
                            },
                            qtyFormProps:
                                const QtyFormProps(enableTyping: false),
                            decoration: const QtyDecorationProps(
                                isBordered: false,
                                borderShape: BorderShapeBtn.circle,
                                width: 12)),
pmatatias commented 4 months ago

in that case, you are right. calling setState will rebuild the widget. actually there is some options

pmatatias commented 2 months ago

hi @artandor , I just released new version. now you can use your own controller. I will close this issue.

feel free to re-open or raise new issue if you need any helps. Thank you