如何在运行时在DropDownMenuItems列表中添加新项目?

时间:2020-06-19 09:56:26

标签: android flutter flutter-layout

我想在运行时在下拉菜单中添加新项目。

select 
    cardName,
    sum(case when month = 1 then [Total Sales S$]  end) [Jan Sales $],
    sum(case when month = 1 then [Total Sales SGP] end) [Jan Sales GP],
    sum(case when month = 2 then [Total Sales S$]  end) [Feb Sales $],
    sum(case when month = 2 then [Total Sales SGP] end) [Feb Sales GP],
    ...
from data
group by cardName

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,请举一个例子:

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String dropdownValue = 'One';
  var values = <String>['One', 'Two', 'Free', 'Four'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Title'),
        ),
        body: Row(
          children: [
            DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                value: dropdownValue,
                onChanged: (String newValue) {
                  setState(() {
                    dropdownValue = newValue;
                  });
                },
                items: values.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
            ),
            //Button to add/change value at runtime
            RaisedButton(
              child: Text('Change'),
              onPressed: () {
                setState(() {
                  values = values..add('Value');
                });
              },
            )
          ],
        ));
  }
}

答案 1 :(得分:0)

var _currencies = ['Rupees','Dollars','Pounds'];

child: DropdownButton<String>(
                    items: _currencies.map((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(value),
                      );
                    }).toList(),
                    value: 'Rupees',
                    onChanged: (String newValueSelected) {
                      // Your code to execute, when a menu item is selected from dropdown
                    },
                  ),

创建一个想要添加字符串的变量。在这里,我添加了var _currencies。 希望这对您有用。