首页 > 解决方案 > 使用 DropdownButton 值

问题描述

我在我的颤振应用程序中构建了一个漂亮的 DropdownButton,但很难找到如何实际使用它。

如何提取选定的值?

我想用它来更新我的搜索栏的设置。当用户选择“1”时,搜索栏应该搜索我的 Sqlite 数据库中的特定列,当他们选择“2”时,它应该搜索其他列。在我提取值之后,最好将 if/else 语句放在搜索栏上方或在我的代码中告诉如何搜索数据库的部分?

标签: sqliteflutterdropdownbutton

解决方案


尝试这个:

search_page.dart

class SearchingPage extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return MyBeautifulDropdownPage(
      myIfElseFunction: (value) {
        print('selected value is $value');
        if(value =='1'){
          _searchASpecificColumnInMySqliteDb();
        }else if (value == '2'){
          _searchTheOtherColumnInMySqliteDb();
        }
      },,
    );
  }
}

my_beautiful_dropdown.dart

class MyBeautifulDropdownPage extends StatelessWidget {
  final Function(String) myIfElseFunction;

  const MyBeautifulDropdownPage({Key key, this.myIfElseFunction}) : super(key: key); 
  @override
  Widget build(BuildContext context) {
    return new DropdownButton<String>(
      items: <String>['1', '2'].map((String value) {
        return new DropdownMenuItem<String>(
          value: value,
          child: new Text(value),
        );
      }).toList(),
      onChanged: myIfElseFunction
    ),
  }
}

onChanged在下拉列表中,您可以使用属性获取所选列的值。在那里你可以把你的 if 语句


推荐阅读