Home > Enterprise >  How to increment or decrement my price when you do 1% or -10% in flutter
How to increment or decrement my price when you do 1% or -10% in flutter

Time:08-13

I want to increment my price by the percentage that the user types in the UI, how do I do the math of that?

for example with simple math you do it like this

  1. Increase the value by 1%: 1.45 x 1.01 = 1.4645
  2. Decrease the value by 1%: 1.45 x 0.99 = 1.4355
  3. Increase the value by 10%: 1.45 x 1.10 = 1.595
  4. Decrease the value by 1%: 1.45 x 0.90 = 1.305

But I don’t know how to do it with flutter because the user is gonna put a value from 1 to 1000 in the percentage that he wants to add.

How to change that value to match the equation above?

CodePudding user response:

If x is the percentage the user fills in the UI and y is the value, then you do:

(x / 100   1) × y

x is positive for increase and negative for decreasing percentage.

CodePudding user response:

The above answer will solve the problem. Just writing an answer to show you by codes.

class MyWidget extends StatefulWidget {
  
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  double originalPrice = 50;
  TextEditingController textController = TextEditingController();
int defaultPercentage = 1;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
         controller: textController,
          onChanged:(val){
            setState((){});
          }
        ),
        Text("Original Price : $originalPrice"),
        Text("Price in % ${originalPrice (originalPrice*(int.tryParse(textController.text)?? defaultPercentage)/100)})")
      ],
    );
  }
}
  • Related