Bottomsheets in Flutter – Android and iOS

By | January 15, 2019

This demo shows how you can implement bottom sheets in flutter which works for both Android and iOS.

 
The StatefulWidget has a built-in function called “showModalBottomSheet’ which we will implement to show a bottom sheet.
 
Watch Video Tutorial
 

 
Modal Bottom Sheet
 

Modal Bottom Sheet in Flutter

Modal Bottom Sheet in Flutter

 

The below function implements a bottom sheet which shows like a modal, that means it will be dismissed when you tap anywhere on the screen outside the sheet.

 

[java]
openBottomSheet(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.photo),
title: Text("Photos"),
onTap: () {},
),
ListTile(
leading: Icon(Icons.camera),
title: Text("Camera"),
onTap: () {},
),
ListTile(
leading: Icon(Icons.videocam),
title: Text("Video"),
onTap: () {},
),
],
);
});
}
[/java]

 

This will take the BuildContext parameter which you can pass from the build function.

 

Persistent Bottom Sheet

 

Persistent Bottom Sheet in Flutter

Persistent Bottom Sheet in Flutter

 

We have to create a GlobalState<ScaffoldState> and a PersistentBottomSheetController instance to create a persistent Bottom sheet across the screens.

 

Declare the variables

 

[java]
final scaffoldKey = GlobalKey<ScaffoldState>();
PersistentBottomSheetController controller;
[/java]

 

Let’s write the function that shows a Persistent Bottom Sheet.

 

[java]
openPersistentBottomController(BuildContext context) {
controller =
scaffoldKey.currentState.showBottomSheet((BuildContext context) {
return Container(
padding: EdgeInsets.all(30.0),
color: Colors.green,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Persistent Bottom Bar"),
SizedBox(
height: 20,
),
Text(
"This is a simple demo of flutter persistent bottom sheet",
style: TextStyle(fontSize: 20),
),
],
),
);
});
}
[/java]

 

Don’t forget to add the key to the Scaffold that is calling this function.

 

[java]
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,

[/java]

 

Make sure you assign the controller…

 

[java]
controller =
scaffoldKey.currentState.showBottomSheet((BuildContext context) {
….
[/java]

 

That’s it. It’s that simple in Flutter.

Leave a Reply

Your email address will not be published. Required fields are marked *