【问题标题】:Dropdown Button wont change下拉按钮不会改变
【发布时间】:2020-06-06 14:02:57
【问题描述】:

您好,我在下拉按钮上编写颤振代码时卡住了,用户从列表中选择后,提示不会更改为用户选择的内容。有人可以帮忙吗?

这是我的代码:

DropdownButton(items: [
            DropdownMenuItem(value: "1", child: Text('+')),
            DropdownMenuItem(value: "2", child: Text('-')),
            DropdownMenuItem(value: "3", child: Text('X')),
            DropdownMenuItem(value: "4", child: Text('/'))
            ].toList(), onChanged: (value){
              setState(() {
                _value = value;
              });
            },hint: Text('Operation'),)

【问题讨论】:

标签: flutter dart


【解决方案1】:

这里是运行示例:

String dropdownValue = 'Lahore';

@override
Widget build(BuildContext context) {
  return DropdownButton<String>(
    value: dropdownValue,
    icon: Icon(Icons.arrow_downward),
    iconSize: 24,
    elevation: 16,
    style: TextStyle(
      color: Colors.deepPurple
    ),
    underline: Container(
      height: 2,
      color: Colors.deepPurpleAccent,
    ),
    onChanged: (String newValue) {
      setState(() {
        dropdownValue = newValue;
      });
    },
    items: <String>['Lahore', 'Islamabad', 'Faisalabad', 'Attabad']
      .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      })
      .toList(),
  );
}

【讨论】:

    【解决方案2】:

    我刚刚在下面创建了一个示例,请检查它并告诉我它是否有效:

    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',
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String selectedOperator;
      var listOfOperators = [
        Operators(type: "+ Addition", value: 1),
        Operators(type: "- Substraction", value: 2),
        Operators(type: "* Multiplication", value: 3),
        Operators(type: "/ Division", value: 4),
      ];
    
      @override
      void initState() {
        super.initState();
        print(listOfOperators.length);
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
                      child: Container(
    
                child: Padding(
                  padding: const EdgeInsets.all(30.0),
                  child: Container(
                    height: 50,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5.0),
                      border: Border.all(
                          color: Colors.red, style: BorderStyle.solid, width: 0.80),
                    ),
                    child: DropdownButton(
                        value: selectedOperator,
                        isExpanded: true,
                        icon: Padding(
                          padding: const EdgeInsets.only(left: 15.0),
                          child: Icon(Icons.arrow_drop_down),
                        ),
                        iconSize: 25,
                        underline: SizedBox(),
                        onChanged: (newValue) {
                          setState(() {
                            print(newValue);
                            selectedOperator = newValue;
                          });
                          print(selectedOperator);
                        },
                        hint: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Text('Select'),
                        ),
                        items: listOfOperators.map((data) {
                          return DropdownMenuItem(
                            value: data.value.toString(),
                            child: Padding(
                              padding: const EdgeInsets.only(left: 10.0),
                              child: Text(
                                data.type,
                                style: TextStyle(
                                  fontSize: 18,
                                  color: Colors.black,
                                ),
                              ),
                            ),
                          );
                        }).toList()),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    class Operators {
      String type;
      int value;
      Operators({this.type, this.value});
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2022-11-08
      • 2020-12-20
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      相关资源
      最近更新 更多