showBottomSheet 抛出错误,抱怨没有脚手架
Posted
技术标签:
【中文标题】showBottomSheet 抛出错误,抱怨没有脚手架【英文标题】:showBottomSheet throwing an error complaining no scaffold 【发布时间】:2020-11-09 22:09:32 【问题描述】:从 LoginPage 重定向到 OnBoarding。入职时单击按钮需要显示底部工作表。使用命名路线进行导航。
稍后在登录页面将使用 Navigator.pushNamed(context, '/onboarding');重定向。
这里是 main.dart
void main()
runApp(
MultiProvider(
providers: [ChangeNotifierProvider(create: (context) => ThemeModel())],
child: DevicePreview(
enabled: !kReleaseMode,
builder: (context) => MyApp(),
),
),
);
SystemChrome.setEnabledSystemUIOverlays([]);
class MyApp extends StatelessWidget
// This widget is the root of your application.
@override
Widget build(BuildContext context)
return MaterialApp(
locale: DevicePreview.of(context).locale, // <--- Add the locale
builder: DevicePreview.appBuilder, // <--- Add the builder
theme: Provider.of<ThemeModel>(context).currentTheme,
debugShowCheckedModeBanner: false,
initialRoute: '/',
routes:
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => LoginPage(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/onboarding': (context) => OnboardingPage(),
'/home': (context) => Home(),
,
);
这里是 buttonClick 然后打开底部表单的代码 sn-p
_getButton(字符串提示,int 索引,BuildContext 上下文) 返回材质按钮( 身高:58, 最小宽度:340, 形状:RoundedRectangleBorder(borderRadius: new BorderRadius.circular(12)), 按下:() 如果(索引 == 1)
**showBottomSheet(
context: context,
builder: (context) => BasicProfileBottomSheetWidget());
**
,
child: Text(
hint,
style: TextStyle(
fontSize: 22,
color: Colors.black,
),
),
color: Colors.white,
);
这是我的小部件树
这是错误。
[38;5;248m════════ Exception caught by gesture ═══════════════════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown while handling a gesture:[39;49m
No Scaffold widget found.
[38;5;244mOnboardingPage widgets require a Scaffold widget ancestor.[39;49m
[38;5;244mThe specific widget that could not find a Scaffold ancestor was: OnboardingPage[39;49m
[38;5;244mstate: _OnboardingPageState#4c2bb[39;49m
[38;5;244mThe ancestors of this widget were[39;49m
[38;5;244mMaterialApp[39;49m
[38;5;244mstate: _MaterialAppState#e362b[39;49m
[38;5;244mMyApp[39;49m
[38;5;244mdependencies: [_InheritedProviderScope<ThemeModel>][39;49m
[38;5;244mDevicePreview[39;49m
[38;5;244mstate: DevicePreviewState#2a192[39;49m
[38;5;244mChangeNotifierProvider<ThemeModel>[39;49m
[38;5;244mvalue: Instance of 'ThemeModel'[39;49m
[38;5;244mlistening to value[39;49m
[38;5;244mMultiProvider[39;49m
[38;5;244m...[39;49m
[38;5;248mTypically, the Scaffold widget is introduced by the MaterialApp or WidgetsApp widget at the top of your application widget tree.[39;49m
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0 debugCheckHasScaffold.<anonymous closure>[39;49m
[38;5;244m#1 debugCheckHasScaffold[39;49m
[38;5;244m#2 showBottomSheet[39;49m
[38;5;248m#3 _getButton.<anonymous closure>[39;49m
[38;5;244m#4 _InkResponseState._handleTap[39;49m
[38;5;244m...[39;49m
[38;5;244mHandler: "onTap"[39;49m
[38;5;244mRecognizer: TapGestureRecognizer#bb0ca[39;49m
[38;5;244mdebugOwner: GestureDetector[39;49m
[38;5;244mstate: ready[39;49m
[38;5;244mwon arena[39;49m
[38;5;244mfinalPosition: Offset(422.5, 960.5)[39;49m
[38;5;244mfinalLocalPosition: Offset(166.7, 29.3)[39;49m
[38;5;244mbutton: 1[39;49m
[38;5;244msent tap down[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
【问题讨论】:
你好,请添加showbottomsheet的代码 谢谢@ByteMe。我刚刚为 showbottomsheet 添加了代码 sn-p 【参考方案1】:尝试将Scaffold
小部件紧跟在MaterialApp 之后,然后OnBoardingPage
可以在Scaffold
的主体内
【讨论】:
刚刚粘贴了 main.dart 代码。你能帮我理解如何在 MaterialApp> 之后立即包装 Scaffold 您可以打开OnBoardingPage
,第一个小部件应该是Scaffold
。对其他页面执行相同操作。单击第一个小部件名称后可以按ALT+ENTER,然后单击Wrap with Widget
并将其设置为Scaffold
谢谢。如果您查看屏幕截图,Scaffold 是 OnboardingPage 中的直接小部件。我错过了什么吗?这是相同的代码sn-p。 class _OnboardingPageState extends State此外,没有使用以下代码的 globalkey 解决方法有效..不知道实际根本原因是什么,但可以修复 :)
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.50,
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(25.0),
topRight: const Radius.circular(25.0),
),
),
child: Center(
child: Text("Modal content goes here"),
),
),
);
【讨论】:
以上是关于showBottomSheet 抛出错误,抱怨没有脚手架的主要内容,如果未能解决你的问题,请参考以下文章