Reputation: 19444
Is it possible to open bottomSheet after navigate the page without using onPressed?
Currently, I added to open bottomSheet
on onPressed
event
scaffoldKey.currentState
.showBottomSheet((context) => Container(
height: 100,
color: Colors.red,
)),
Upvotes: 5
Views: 5321
Reputation: 1010
You cannot add it on initState()
because at that time you do not have the context yet.
The easiest way to do it is to add a variable didOpenModalBottomSheet = false
in the beginning
Then in the build add something like this
@override
Widget build(BuildContext context) {
if(!didOpenModalBottomSheet){
didOpenModalBottomSheet = true;
_modalBottomSheetMenu();
}
return ...
You could also use this library: https://pub.dev/packages/need_resume specialy if you are coming from Android. But I did not try it yet.
Upvotes: 3
Reputation: 879
I'm not sure if you've tried this already but you could define a stateful widget and add your function that shows the bottomSheet to it's state, I've added a sample bellow,
void _modalBottomSheetMenu() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await showModalBottomSheet(
context: context,
builder: (builder) {
return new Container(
height: 350.0,
color:
Colors.transparent, //could change this to Color(0xFF737373),
//so you don't have to change MaterialApp canvasColor
child: new Container(
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(10.0),
topRight: const Radius.circular(10.0))),
child: new Center(
child: new Text("This is a modal sheet"),
)),
);
});
});
}
That will wait for the first frame to be drawn before showing the bottomSheet, therefore you can call it from initState
@overrideand in the init state you'll call the above function.
@override
void initState() {
super.initState();
_modalBottomSheetMenu();
}
Upvotes: 23