Can i show modal bottom sheet at app launch flutter?

3,347

Solution 1

you can do so in the initState of your first screen, like so

 @override
  void initState() {
    // TODO: implement initState

    Future.delayed(Duration(seconds: 0)).then((_) {
      showModalBottomSheet(
          context: context,
          builder: (builder) {
            return Container();
          });
    });
    super.initState();
  }


you need to do it like that, using future and delayed becuase initState dosent allow .of inside it, this is kinda hack but it works

Solution 2

Simple Solution

@override
  void initState() {
   
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      showModalBottomSheet(context: context, builder: (BuildContext context) {
      return Container();
    });
    });

  }
Share:
3,347
custom apps
Author by

custom apps

Updated on December 18, 2022

Comments

  • custom apps
    custom apps over 1 year

    Am trying to show bottom sheeet at app launch automatically in flutter, but it comes with errors. It only works for me when I instantiate it with a click event. But how can i pop it on screen launch?

     @override
      Widget build(BuildContext context) {
        showModalBottomSheet(context: context, builder: (BuildContext context) {
          return Container();
        });
    
  • Gregory Conrad
    Gregory Conrad over 3 years
    This comes down to opinion as both this and the other answer work, but I think the intent with this one is clearer.