1

我正在尝试根据用户下拉输入降低产品(智能手机)价格并将价格传递到下一个屏幕。我的代码没有错误并且运行完美,但问题是,它仅基于一个 switch 语句(为下拉值全局选择的初始语句)降低了价格,因为setState方法仅适用于更改屏幕上显示的下拉值,而不是在 switch 语句中含功能。有人可以帮我解决这个问题。我尝试了很多东西,但没有任何帮助。下面是我的代码(它很长,所以我在这里跳过了小部件构建部件和设计部件)。

值已经这样定义

   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(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",
                            ),

我正在使用这种方法来计算

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;
    }

Navigator在计算器方法中像这样使用

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

1 回答 1

0

请检查我在下面添加的示例,它工作得很好。

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()),
      ),
    );
  }
}

于 2021-12-24T17:22:40.647 回答