0

I built a beautiful DropdownButton in my flutter app, but have had a hard time finding out how to actually use it.

How can I extract the selected value?

I want to use it to update the settings for my search bar. When a user selects '1' the search bar should search a specific column in my Sqlite database, when they select '2' it should search other columns. After I extract the value is it best to make my if/else statement above the search bar or in the part of my code that tells how to search the database?

Bill Watts
  • 21
  • 4

1 Answers1

1

Try this:

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

In dropdown, you can get the value of the selected column with the onChanged property. In there you can put your if statement

TSR
  • 17,242
  • 27
  • 93
  • 197
  • I wrote the dropdown menu on a separate page and inserted it as a widget. Is there a way to write something in onChanged so that on my searching page I can use the value and write my if/else there? – Bill Watts Aug 15 '20 at 20:33
  • 1
    @BillWatts sure that is possible. check my edited answer – TSR Aug 15 '20 at 20:44