1

I am trying to reduce a products(smartphone) price based on user dropdown input & pass the price to next screen. My code has no error & runs perfectly but the problem is, it reduces price based on only one switch statement (the initial one selected globally for dropdown value) because setState method is working only on changing dropdown value displayed on screen, not in the switch statement containing function. Can someone help me on how to solve this. I tried many things, but nothing helped. Below is my code (It's long, so I skipped widget building parts & designing parts here).

Value has been defined like this

   double price = 18000;  
   String bdropDownValue = 'Less than 6 hours'; //initialvalue
    final List<String> bItems = [
    'Less than 6 hours',
    'Less than 12 hours',
    'Less than 24 hours'],

DropdownButtonFormField


DropdownButtonFormField(value: bdropDownValue,
                        items: bItems.map((bItems) {
                          return DropdownMenuItem(
                            value: bItems,
                            child: Text(bItems),
                          );
                        }).toList(),
                        onChanged: (String? value) {
                          setState(() {
                            bdropDownValue = value!;
                          });
                        },
                        ),
                  /// button used to calculate next
                   TextButton(
                          onPressed: () {
                            calculator(bdropDownValue);
                          },
                          child: Text(
                            "confirm",
                            ),

I am using this method to calculate

void calculator(String bdropDownValue) {
    switch (bdropDownValue) {
      case 'Less than 6 hours':
        price = price - 3000;
        break;
      case 'Less than 12 hours':
        price = price - 1500;
        break;
      case 'Less than 24 hours':
        price = price - 1000;
        break;
    }

And using Navigator like this inside calculator method

Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => priceScreen(
                  price: price,
                )));
  }
}

1 Answers1

0

Please check the example that I have added below it works perfectly fine.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MainHomePage());
  }
}

class MainHomePage extends StatefulWidget {
  MainHomePage({Key key}) : super(key: key);

  @override
  _MainHomePageState createState() => _MainHomePageState();
}

class _MainHomePageState extends State<MainHomePage> {
  double price = 18000;
  String bdropDownValue = 'Less than 6 hours'; //initialvalue
  final List<String> bItems = [
    'Less than 6 hours',
    'Less than 12 hours',
    'Less than 24 hours'
  ];

  void calculator(String bdropDownValue) {
    var actualPrice = price;
    switch (bdropDownValue) {
      case 'Less than 6 hours':
        actualPrice = actualPrice - 3000;
        break;
      case 'Less than 12 hours':
        actualPrice = actualPrice - 1500;
        break;
      case 'Less than 24 hours':
        actualPrice = actualPrice - 1000;
        break;
    }

    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => PriceScreen(
                  price: actualPrice,
                )));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            DropdownButtonFormField(
              value: bdropDownValue,
              items: bItems.map((bItems) {
                return DropdownMenuItem(
                  value: bItems,
                  child: Text(bItems),
                );
              }).toList(),
              onChanged: (String value) {
                setState(() {
                  bdropDownValue = value;
                });
              },
            ),
            TextButton(
              onPressed: () {
                calculator(bdropDownValue);
              },
              child: Text(
                "confirm",
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class PriceScreen extends StatelessWidget {
  final double price;
  const PriceScreen({Key key, this.price}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Text(price.toString()),
      ),
    );
  }
}

Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34