how to open bottomSheet after navigate on the page without using onPressed flutter?

Blo*_*oss 2 dart flutter flutter-layout

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,
)),                                                                                      
Run Code Online (Sandbox Code Playgroud)

小智 17

我不确定你是否已经尝试过这个,但你可以定义一个有状态的小部件并添加你的函数来显示底部的状态,我已经添加了一个示例波纹管,

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"),
                  )),
            );
          });
    });
  }
Run Code Online (Sandbox Code Playgroud)

这将在显示bottomSheet之前等待绘制第一帧,因此您可以从initState调用它

@overrideand 在 init 状态你会调用上面的函数。

 @override
  void initState() {
    super.initState();
    _modalBottomSheetMenu();
  }
Run Code Online (Sandbox Code Playgroud)