Warm tip: This article is reproduced from serverfault.com, please click

android-找不到脚手架小部件:打开“底部对话框”工作表时出现异常

(android - No Scaffold widget found : Getting exception while opening Bottom Dialog sheet)

发布于 2020-02-05 08:36:49

我正在使用Scaffold小部件,但在打开底部对话框时却遇到了此异常

@override
Widget build(BuildContext context) {

return Scaffold(
  appBar: AppBar(
    title: Text("Calendar"),
  ),
  body: SafeArea(
  .......
  .......

    child: GestureDetector(
                        onTap: (){
                          //getting exception here
                          showBottomSheet(
                              context: context,
                              builder: (context) => Container(
                                color: Colors.red,
                              ));
                        },

我坚持使用此代码,如果有人可以提出任何建议,它将非常有帮助。谢谢你。

Questioner
Ankit Dubey
Viewed
22
Pedro Massango 2020-09-14 23:42:58

问题在于,context用来显示的BottomSheet不是contextScaffold你可以通过使用GlobalKey或将其包装GestureDetectorBuilder小部件中来解决此问题,以便为你提供context包含的引用Scaffold

这是一个使用GlobalKey的状态的示例Scaffold

// created the ScaffoldState key    
final scaffoldState = GlobalKey<ScaffoldState>();
    
    class MyWidget extends StatelessWidget {
      void _showSheet() {
        // Show BottomSheet here using the Scaffold state instead ot«f the Scaffold context
        scaffoldState.currentState
            .showBottomSheet((context) => Container(color: Colors.red));
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            key: scaffoldState,
            appBar: AppBar(
              title: Text("Calendar"),
            ),
            body: SafeArea(child: GestureDetector(onTap: () {
              //getting exception here
              _showSheet();
            })));
      }
    }