试图在我的应用程序中实现showBottomSheet,但是它抛出一个错误:Scaffold.of()在不包含Scaffold的上下文中调用。

在网上搜索了一下之后,我添加了一个GlobalKey,但这似乎并没有解决问题。有人有建议吗?

class _DashboardState extends State<Dashboard> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  int _bottomNavBarCurrentIndex = 0;
  dashboardViews.DashboardView dView = new dashboardViews.DashboardView();

  List<Widget> _listOffers = new List<Widget>();

  Widget _currentView;

  void loadDashboardView() {
    _currentView = (_bottomNavBarCurrentIndex == 0
        ? dView.getOfferView(_listOffers, _getOfferData)
        : dView.getOrdersView());
  }

  _showInfoSheet() {
    showBottomSheet(
                context: _scaffoldKey.currentContext,
                builder: (context) {
                  return Text('Hello');
                });
  }

  Future _getOfferData() async {
    loadDashboardView();

    List<Widget> _resultsOffers = new List<Widget>();

    SharedPreferences prefs = await SharedPreferences.getInstance();
    String _token = prefs.getString('token');

    final responseOffers =
        await http.get(globals.apiConnString + 'GetActiveOffers?token=$_token');

    if (responseOffers.statusCode == 200) {
      List data = json.decode(responseOffers.body);

      for (var i = 0; i < data.length; i++) {
        _resultsOffers.add(GestureDetector(
            onTap: () => _showInfoSheet(),
            child: Card(
                child: Padding(
                    padding: EdgeInsets.all(15),
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Row(children: <Widget>[
                            Expanded(
                                flex: 5,
                                child: Text('${data[i]['Title']}',
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: globals.themeColor4))),
                            Expanded(
                                flex: 3,
                                child: Row(children: <Widget>[
                                  Icon(Icons.access_time,
                                      size: 15, color: Colors.grey),
                                  Padding(
                                      padding: EdgeInsets.fromLTRB(5, 0, 10, 0),
                                      child: Text('11:30 PM',
                                          style:
                                              TextStyle(color: Colors.black))),
                                ])),
                            Expanded(
                                flex: 1,
                                child: Row(children: <Widget>[
                                  Icon(Icons.local_dining,
                                      size: 15, color: Colors.grey),
                                  Padding(
                                      padding: EdgeInsets.fromLTRB(5, 0, 0, 0),
                                      child: Text('${i.toString()}',
                                          style:
                                              TextStyle(color: Colors.black))),
                                ])),
                          ]),
                          Padding(padding: EdgeInsets.all(10)),
                          Row(children: <Widget>[
                            Text(
                              'Created May 2, 2019 at 2:31 PM',
                              style: TextStyle(color: Colors.grey[600]),
                              textAlign: TextAlign.start,
                            )
                          ])
                        ])))));
           }
      }

      setState(() {
        _listOffers = _resultsOffers;
      });

      loadDashboardView();
    }
  }

  void _bottomNavBarTap(int index) {
    setState(() {
      _bottomNavBarCurrentIndex = index;
      loadDashboardView();
    });
  }

  void pullRefresh() {
    _getOfferData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Colors.grey[200],
      body: _currentView,
      bottomNavigationBar: BottomNavigationBar(
        onTap: _bottomNavBarTap,
        currentIndex: _bottomNavBarCurrentIndex,
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.near_me), title: Text('OFFERS')),
          BottomNavigationBarItem(
              icon: Icon(Icons.broken_image), title: Text('ORDERS'))
        ],
      ),
    );
  }
}

希望有人能指出正确的方向,谢谢!

编辑:这是我看过的同一个问题的链接之一,我尝试按照建议将生成器上下文设置为c,但没有用:https://github.com/flutter/flutter/issues/23234

编辑2:当我使用不包含Scaffold错误的上下文调用Scaffold.of()时,添加了显示变量内容的屏幕截图。

dart - 带有上下文的showBottomSheet脚手架问题-LMLPHP

最佳答案

出现此错误的原因是因为您在构建过程中正在调用Scaffold,因此showBottomSheet函数无法看到它。解决此问题的方法是为有状态的小部件提供Scaffold,为Scaffold分配一个密钥,然后将其传递给您的Dashboard(我假设它是状态类中有状态的小部件的名称)。您无需在Scaffoldbuild内的_DashboardState中分配密钥。

这是您在其中提供Scaffoldkey的类:

 class DashboardPage extends StatefulWidget {

  @override
  _DashboardPageState createState() => _DashboardPageState() ;
}

class _DashboardPageState extends State<DashboardPage> {

  final GlobalKey<ScaffoldState> scaffoldStateKey ;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldStateKey,
      body: Dashboard(scaffoldKey: scaffoldStateKey),
    );
  }

您的原始Dashboard已更改:
  class Dashboard extends StatefulWidget{

  Dashboard({Key key, this.scaffoldKey}):
    super(key: key);

  final GlobalKey<ScaffoldState> scaffoldKey ;

  @override
  _DashboardState createState() => new _DashboardState();
 }

您的_DashboardState带有概述的更改:
 class _DashboardState extends State<Dashboard> {
   int _bottomNavBarCurrentIndex = 0;
   dashboardViews.DashboardView dView = new dashboardViews.DashboardView();

   List<Widget> _listOffers = new List<Widget>();

   Widget _currentView;

   void loadDashboardView() {
     _currentView = (_bottomNavBarCurrentIndex == 0
    ? dView.getOfferView(_listOffers, _getOfferData)
    : dView.getOrdersView());
   }

   _showInfoSheet() {
     showBottomSheet(
        context: widget.scaffoldKey.currentContext, // referencing the key passed from [DashboardPage] to use its Scaffold.
        builder: (context) {
          return Text('Hello');
        });
   }

   Future _getOfferData() async {
     loadDashboardView();

     List<Widget> _resultsOffers = new List<Widget>();

     SharedPreferences prefs = await SharedPreferences.getInstance();
     String _token = prefs.getString('token');

     final responseOffers =
     await http.get(globals.apiConnString + 'GetActiveOffers?token=$_token');

     if (responseOffers.statusCode == 200) {
       List data = json.decode(responseOffers.body);

       for (var i = 0; i < data.length; i++) {
         _resultsOffers.add(GestureDetector(
             onTap: () => _showInfoSheet(),
             child: Card(
                child: Padding(
                    padding: EdgeInsets.all(15),
                    child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                        Row(children: <Widget>[
                        Expanded(
                            flex: 5,
                            child: Text('${data[i]['Title']}',
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    color: globals.themeColor4))),
                        Expanded(
                            flex: 3,
                            child: Row(children: <Widget>[
                              Icon(Icons.access_time,
                                  size: 15, color: Colors.grey),
                              Padding(
                                  padding: EdgeInsets.fromLTRB(5, 0, 10, 0),
                                  child: Text('11:30 PM',
                                      style:
                                      TextStyle(color: Colors.black))),
                            ])),
                        Expanded(
                            flex: 1,
                            child: Row(children: <Widget>[
                              Icon(Icons.local_dining,
                                  size: 15, color: Colors.grey),
                              Padding(
                                  padding: EdgeInsets.fromLTRB(5, 0, 0, 0),
                                  child: Text('${i.toString()}',
                                      style:
                                      TextStyle(color: Colors.black))),
                            ])),
                      ]),
                      Padding(padding: EdgeInsets.all(10)),
                      Row(children: <Widget>[
                        Text(
                          'Created May 2, 2019 at 2:31 PM',
                          style: TextStyle(color: Colors.grey[600]),
                          textAlign: TextAlign.start,
                        )
                      ])
                    ])))));
      }
    }

    setState(() {
      _listOffers = _resultsOffers;
    });

    loadDashboardView();
  }


   void _bottomNavBarTap(int index) {
     setState(() {
      _bottomNavBarCurrentIndex = index;
      loadDashboardView();
    });
  }

   void pullRefresh() {
     _getOfferData();
   }

   @override
   Widget build(BuildContext context) {
     // Scaffold key has been removed as there is no further need to it.
     return Scaffold(
      backgroundColor: Colors.grey[200],
      body: _currentView,
      bottomNavigationBar: BottomNavigationBar(
         onTap: _bottomNavBarTap,
         currentIndex: _bottomNavBarCurrentIndex,
         items: [
           BottomNavigationBarItem(
               icon: Icon(Icons.near_me), title: Text('OFFERS')),
           BottomNavigationBarItem(
               icon: Icon(Icons.broken_image), title: Text('ORDERS'))
           ],
         ),
       );
     }
   }
 }

关于dart - 带有上下文的showBottomSheet脚手架问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56220012/

10-09 03:23