Modal BottomSheet in Flutter: solved exercise
Modal BottomSheet in Flutter: solved exercise
showModalBottomSheet is the standard way to display temporary panels from the bottom of the screen: filters, contextual actions, confirmations, option selectors. It feels more natural on mobile than a centered AlertDialog when the action is related to a list or content below.
Problem statement
Implement three BottomSheet variants:
- Basic BottomSheet: an action list (share, edit, delete).
- BottomSheet with a form: a filter panel with switches and apply/cancel buttons.
- DraggableScrollableSheet: a draggable panel with a long scrollable list.
Dependencies
Flutter SDK only.
Solution: Actions BottomSheet
Solution: Filters BottomSheet with form
Solution: DraggableScrollableSheet
Main screen
Common mistakes
isScrollControlled: falsewith a text field: the sheet has a fixed height and the keyboard covers it. Always useisScrollControlled: truewhen the sheet contains text inputs.DraggableScrollableSheetwithout passingscrollControllerto theListView: drag and scroll conflict with each other. Always pass the builder’s controller.Navigator.pop(context, value)with a type mismatch: the value type must match the generic ofshowModalBottomSheet<T>; otherwise the result isnull.
Practical use
Search filters, contextual action menus, delete confirmations, image/file pickers, checkout flows — any secondary workflow that does not deserve a full route.
Recommended next exercise
- Implicit animations in Flutter: solved exercise
- go_router with auth guards in Flutter: solved exercise
- Form validation in Flutter: solved exercise
- All Flutter exercises
Guided practice and next step
- More Flutter exercises
- C exercises to strengthen fundamentals
- Programming in C in 100 Solved Exercises
- View the book on Amazon (included in Kindle Unlimited)
- Subscribe to the newsletter
FAQ
How do I prevent the sheet from closing when tapping outside?
Pass isDismissible: false to showModalBottomSheet. Useful for critical confirmations or forms with unsaved data.
Can I open a BottomSheet from another BottomSheet?
Technically yes, but the UX is confusing. Instead, use Navigator.push inside the sheet to go to a full screen if you need deeper navigation.
What is the difference between a modal and a persistent BottomSheet?
The modal blocks interaction with the rest of the screen (showModalBottomSheet). The persistent one coexists with the screen below (Scaffold.showBottomSheet). For most use cases, the modal is the right choice.