处理手势时抛出以下断言:未找到 Scaffold 小部件

问题描述 投票:0回答:2

我正在尝试在 ElevatedButton 上的 onPressed 之后实现 showBottomSheet,但出现以下错误


======== Exception caught by gesture ===============================================================
The following assertion was thrown while handling a gesture:
No Scaffold widget found.

BottomSheetScrollSheet widgets require a Scaffold widget ancestor.
The specific widget that could not find a Scaffold ancestor was: BottomSheetScrollSheet
  state: _BottomSheetScrollSheetState#f9b53
The ancestors of this widget were: 
  : Builder
  : MaterialApp
    state: _MaterialAppState#fe76c
  : MyApp
  ...

Typically, the Scaffold widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree.

When the exception was thrown, this was the stack: 
#0      debugCheckHasScaffold.<anonymous closure> (package:flutter/src/material/debug.dart:118:7)
#1      debugCheckHasScaffold (package:flutter/src/material/debug.dart:129:4)
#2      showBottomSheet (package:flutter/src/material/bottom_sheet.dart:770:10)
#3      _BottomSheetScrollSheetState.build.<anonymous closure> (package:master_learn/screens/bottomsheet_scrollsheet.dart:82:27)
#4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
#6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
#7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
#8      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:230:7)
#9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:563:9)
#10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:94:12)
#11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:139:9)
#12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
#13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:137:18)
#14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:123:7)
#15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:439:19)
#16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
#17     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
#18     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#19     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#20     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#21     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#25     _invoke1 (dart:ui/hooks.dart:170:10)
#26     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#27     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#ece38
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(166.0, 192.3)
  finalLocalPosition: Offset(71.0, 28.3)
  button: 1
  sent tap down
====================================================================================================

下面是我如何实现我的代码

 Center(
                child: Container(
                    margin: const EdgeInsets.only(top: 5, bottom: 5),
                    padding: const EdgeInsets.only(top: 5, bottom: 5),
                    child: ElevatedButton(
                        onPressed: () {
                          showBottomSheet(
                              context: context,
                              builder: (context) {
                                return Wrap(
                                  children: [
                                    ListTile(
                                      leading: Icon(Icons.ac_unit),
                                      title: Text("Title 1"),
                                    ),
                                    // Add keyboard padding
                                    Padding(padding: EdgeInsets.only(
                                      bottom: MediaQuery.of(context).viewInsets.bottom
                                    ))
                                  ],
                                );
                              });
                        },
                        child: const Text("Persistent Bottom Sheet"))))

当我使用

showModalBottomSheet
时,它工作正常,但当我使用
showBottomSheet
时,问题就开始了。我尝试将整个
showBottomSheet
放入 void 方法中并将上下文解析为参数,但它仍然给我
The following assertion was thrown while handling a gesture: No Scaffold widget found.
错误。

flutter dart widget bottom-sheet scaffold
2个回答
0
投票

尝试下面的代码希望对您有所帮助。我尝试过其他方法。

创建您的 BottomSheet 方法:

openBottomSheet(BuildContext context) {
    return showBottomSheet(
      context: context,
      builder: (context) {
        return Wrap(
          children: [
            ListTile(
              leading: Icon(Icons.ac_unit),
              title: Text("Title 1"),
            ),
            // Add keyboard padding
            Padding(
              padding: EdgeInsets.only(
                bottom: MediaQuery.of(context).viewInsets.bottom,
              ),
            ),
          ],
        );
      },
    );
  }

你的小部件

 ElevatedButton(
        onPressed: () {
          openBottomSheet(context);
          print('Bottom Sheet Open');
        },
        child: const Text("Persistent Bottom Sheet"),
      ),

更多请参考

showBottomSheet


0
投票

试试这个

 final scaffoldKey = GlobalKey<ScaffoldState>();
Center(
                child: Container(
                    margin: const EdgeInsets.only(top: 5, bottom: 5),
                    padding: const EdgeInsets.only(top: 5, bottom: 5),
                    child: ElevatedButton(
                        onPressed: () {
                          scaffoldKey.currentState!.
                showBottomSheet((context) {
                                return Wrap(
                                  children: [
                                    ListTile(
                                      leading: Icon(Icons.ac_unit),
                                      title: Text("Title 1"),
                                    ),
                                    // Add keyboard padding
                                    Padding(padding: EdgeInsets.only(
                                      bottom: MediaQuery.of(context).viewInsets.bottom
                                    ))
                                  ],
                                );
                              });
                        },
                        child: const Text("Persistent Bottom Sheet"))))
© www.soinside.com 2019 - 2024. All rights reserved.