【问题标题】:Using the DropdownButton value使用 DropdownButton 值
【发布时间】:2020-12-05 08:13:51
【问题描述】:

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

如何提取选定的值?

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

【问题讨论】:

    标签: sqlite flutter dropdownbutton


    【解决方案1】:

    试试这个:

    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 语句

    【讨论】:

    • 我在单独的页面上编写了下拉菜单并将其作为小部件插入。有没有办法在 onChanged 中写一些东西,以便在我的搜索页面上我可以使用该值并在那里写下我的 if/else?
    • @BillWatts 确定这是可能的。检查我编辑的答案
    猜你喜欢
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2023-01-05
    • 2020-09-16
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多