Show bottom sheet with dropwdown options on dropdown click in flutter

3,938

If you want to make your own, here's a simple example made by me:

class ModalDropDown extends StatefulWidget {
  @override
  _ModalDropDownState createState() => _ModalDropDownState();
}

class _ModalDropDownState extends State<ModalDropDown> {
  String _selected = '';
  List<String> _items = ['A', 'B', 'C', 'D'];

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          RaisedButton(
            child: Text('Show Modal'),
            onPressed: () => showModal(context),
          ),
          Text('Selected item: $_selected')
        ],
      ),
    );
  }

  void showModal(context){
    showModalBottomSheet(
      context: context,
      builder: (context){
        return Container(
          padding: EdgeInsets.all(8),
          height: 200,
          alignment: Alignment.center,
          child: ListView.separated(
            itemCount: _items.length,
            separatorBuilder: (context, int) {
              return Divider();
            },
            itemBuilder: (context, index) {
              return GestureDetector(
                child: Text(_items[index]),
                onTap: () {
                  setState(() {
                    _selected = _items[index];
                  });
                  Navigator.of(context).pop();
                }
              );
            }
          ),
        );
      }
    );
  }
}

If instead you want the Flutter made picker for iOS, you can use the CupertinoPicker

Share:
3,938
user2511882
Author by

user2511882

Updated on November 26, 2022

Comments

  • user2511882
    user2511882 over 1 year

    I have a DropDown which shows various options. The current behavior displays the options as a DropdownMenuItem.

    How can I switch the DropdownMenuItem to a BottomSheet which shows all the options within the DropDown?

    Current code:

    DropdownButtonHideUnderline(
      child: Container(
        color: Color.fromRGBO(216, 216, 216, 0.33),
        padding: const EdgeInsets.fromLTRB(32, 8, 16, 8),
        child: DropdownButton<String>(
          hint: Text("TEST DROPDOWN"),
          items: <String>['A', 'B', 'C', 'D'].map((String value) {
    
            // this crashes
            return showModalBottomSheet(context: context, builder: (builder) {
              return Container(
                child: Text('Hello From Modal Bottom Sheet'),
                padding: EdgeInsets.all(40.0),
              );
            });
    
            // this works
            return new DropdownMenuItem<String>(
              value: value,
              child: new Text(value),
            );
    
          }).toList(),
          ...
    

    EDIT: I am trying to display something similar to an iOS date selector on dropdown click.

    • J. S.
      J. S. over 4 years
      A showModalBottomSheet is in no way related to a DropdownMenuItem. You can't just return it inside a DropdownButton. Can you explain what you want to achieve?
    • user2511882
      user2511882 over 4 years
      I have a dropdown. Now when the user clicks on the dropdown instead of showing the list with various options, I would like to show the options in a bottom sheet.
    • J. S.
      J. S. over 4 years
      You mean like an iOS dropdown or date select?
    • user2511882
      user2511882 over 4 years
      Yes! I should have mentioned that in the question
  • user2511882
    user2511882 over 4 years
    The issue I am facing is how to populate a modal from the dropdown (while showing the options). I am able to show a modal on button click without any issue
  • J. S.
    J. S. over 4 years
    I've changed my answer to use a List of strings like you do in your example.
  • user2511882
    user2511882 over 4 years
    Thanks for that! I still need to figure out how to show the modal on dropdown click rather than a button click.
  • J. S.
    J. S. over 4 years
    You can just style a button to the way you want it to look. Why does it need to be a DropdownButton if you don't want it to behave like one?
  • user2511882
    user2511882 over 4 years
    ohh geez. didn't think of that. Thank you!
  • J. S.
    J. S. over 4 years
    Haha. Glad I could help. Good luck with your app!