Flutter错误总结1:Scaffold.of() called with a context that does not contain a Scaffold.

本文解决了一个特定的Flutter错误:Scaffold.of()被一个不包含Scaffold的上下文调用。此错误通常发生在使用Navigator从第二个页面返回到第一个页面并尝试显示SnackBar时。通过将按钮组件独立出来并正确传递上下文解决了这个问题。

在这里插入代码片## 1.Scaffold.of() called with a context that does not contain a Scaffold.
错误出现的场景: 使用Navigator 从第二个页面向第一个页面返回值

class HomePage extends StatelessWidget {
   @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('我是第一个页面'),),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text('点我进入下一页面'),
              onPressed: (){
            _navigateToSecondPage(context);
              },
              )
          ],
        ),
      ),
    );
  }

  _navigateToSecondPage(BuildContext context) async {
    final result = await Navigator.push(context, 
            MaterialPageRoute(
            builder: (context)=> SecondPage(),
          ));
    Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result'),));
  }
}

修改后

class HomePage extends StatelessWidget {
   @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('我是第一个页面'),),
      body: Center(
        child: Column(
          children: <Widget>[
            RaiseMyButton()
          ],
        ),
      ),
    );
  }
}

class RaiseMyButton extends StatelessWidget{
    @override 
    Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        RaisedButton(
              child: Text('点我进入下一页面'),
              onPressed: (){
            _navigateToSecondPage(context);
              },
              )
      ], 
    );
    }
    _navigateToSecondPage(BuildContext context) async {
    final result = await Navigator.push(context, 
            MaterialPageRoute(
            builder: (context)=> SecondPage(),
          ));
    Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result'),));
  }
}

原因: of method
需要将RaiseButton剥离出来,因为build方法如果嵌套build后,接收不到外层的context参数

class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: PreferredSize( preferredSize: const Size.fromHeight(0.0), child: AppBar( centerTitle: true, title: const Text(" ", textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 0.0, fontWeight: FontWeight.w800, )), backgroundColor: Colors.transparent, elevation: 0), ), body: LayoutBuilder(builder: (context, constraints) { final mediaQuery = MediaQuery.of(context); final screenHeight = mediaQuery.size.height; final statusBarHeight = mediaQuery.padding.top; double containerHeight = screenHeight - statusBarHeight; // 高度的0.333 double containerWidth = containerHeight * 0.490; // 宽度的1.342倍 double containerhappyHeight = containerHeight * 0.086; double containersizeHeight = containerHeight * 0.023; double containerhumanpictureHeight = containerHeight * 0.5 - containerhappyHeight - containersizeHeight; return Container( width: containerWidth, height: containerHeight, alignment: Alignment.center, decoration: const BoxDecoration( image: DecorationImage( image: AssetImage("assets/images/background.jpg"), fit: BoxFit.fill)), child: Column(children: [ Container( constraints: BoxConstraints( maxWidth: containerWidth, // 最大宽度 maxHeight: containerHeight * 0.5, // 最大高度 ), alignment: Alignment.topCenter, color: Colors.transparent, child: Column(children: [ Container( width: containerWidth, height: containerhappyHeight, alignment: Alignment.bottomCenter, color: Colors.red, child: const Text("Healthy and Happy", textDirection: TextDirection.ltr, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 39.0, fontWeight: FontWeight.w800)), ), Container( alignment: Alignment.bottomCenter, color: Colors.transparent, child: Column(children: [ Container( width: containerWidth, height: containersizeHeight, color: Colors.black, ), Container( width: containerWidth, height: containerhumanpictureHeight, color: Colors.transparent, child: ImageButtonhuman()), ])), ])), Container( width: containerWidth, height: containerHeight * 0.5, alignment: Alignment.topCenter, color: Colors.blue, child: Column(children: [ Container( width: containerWidth, height: 385, alignment: Alignment.topCenter, color: Colors.transparent, child: Row(children: [ Container( child: Column(children: [ SizedBox( width: 200, height: 30, ), Container( child: Row( children: [ SizedBox( width: 15, height: 350, ), Container( width: 185, height: 350, color: Colors.red, child: ImageButtoncbt()) ], )) ])), Container( width: 225, height: 400, alignment: Alignment.center, color: Colors.transparent, child: Column(children: [ SizedBox( width: 225, height: 36, ), Container( width: 225, height: 150, alignment: Alignment.center, color: Colors.transparent, child: Row( children: [ Container( width: 112, height: 150, alignment: Alignment.center, color: Colors.transparent, child: Row( children: [ SizedBox( width: 15, height: 150, ), Container( width: 97, height: 150, color: Colors.red, child: ImageButtonsos()), ], )), Container( width: 112, height: 150, alignment: Alignment.center, color: Colors.red, child: ImageButtonaction()) ], )), Container( width: 112, height: 5, color: Colors.transparent, ), Container( width: 225, height: 140, alignment: Alignment.center, color: Colors.transparent, child: Row( children: [ SizedBox( width: 15, height: 140, ), Container( width: 210, height: 140, alignment: Alignment.center, color: Colors.red, child: ImageButtonsofa(), ) ], )), ]), ), ])), Container( width: 430, height: 53, alignment: Alignment.bottomCenter, decoration: const BoxDecoration( image: DecorationImage( image: AssetImage( "assets/images/darkbackgound.jpg"), fit: BoxFit.fill)), child: Row( children: [ Container( width: 107, height: 52, alignment: Alignment.center, color: Colors.transparent, child: ImageButtoncommunity(), ), Container( width: 106, height: 52, alignment: Alignment.center, color: Colors.transparent, child: ImageButtonknowledge(), ), Container( width: 106, height: 52, alignment: Alignment.center, color: Colors.transparent, child: ImageButtonpsychology(), ), Container( width: 107, height: 52, alignment: Alignment.center, color: Colors.transparent, child: ImageButtonme(), ), ], )) ]), ) ])); })); } } class ImageButtonhuman extends StatelessWidget { const ImageButtonhuman({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: GestureDetector( onTap: () { // 按钮点击逻辑,如导航或显示对话框 Navigator.push( context, MaterialPageRoute( builder: (context) => const SecondPage()), // 目标页面 ); }, child: LayoutBuilder(builder: (context, constraints) { final mediaQuery = MediaQuery.of(context); final screenHeight = mediaQuery.size.height; final statusBarHeight = mediaQuery.padding.top; double containerHeight = screenHeight - statusBarHeight; double containerWidth = containerHeight * 0.490; double humanpictureWidth = containerWidth * 0.790; double humanpictureHeight = containerHeight * 0.365; return Container( width: humanpictureWidth, height: humanpictureHeight, alignment: Alignment.center, child: Container( width: humanpictureWidth, height: humanpictureHeight, alignment: Alignment.center, decoration: const BoxDecoration( image: DecorationImage( image: AssetImage("assets/images/human.jpg"), fit: BoxFit.fill)), )); }), /*Image.asset( 'assets/images/human.jpg', // 替换为您的图片路径 width: humanpictureWidth, height: 320, fit: BoxFit.cover, ),*/ ), ), backgroundColor: Colors.transparent, ); } } class ImageButtoncbt extends StatelessWidget { const ImageButtoncbt({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { // 按钮点击逻辑,如导航或显示对话框 /* ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('按钮被点击!')), );*/ Navigator.push( context, MaterialPageRoute(builder: (context) => const ThirdPage()), // 目标页面 ); }, child: Image.asset( 'assets/images/cbt.jpg', // 替换为您的图片路径 width: 1850, height: 350, alignment: Alignment.topCenter, ), ), backgroundColor: Colors.transparent, ); } } class ImageButtonsos extends StatelessWidget { const ImageButtonsos({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => const SosPage()), // 目标页面 ); }, child: Column( children: [ Image.asset( 'assets/images/sos.jpg', // 替换为您的图片路径 width: 80, height: 133, alignment: Alignment.center, ), const Text('情绪急救按钮', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 11.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } class ImageButtonaction extends StatelessWidget { const ImageButtonaction({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => const ActionPage()), // 目标页面 ); }, child: Column( children: [ Image.asset( 'assets/images/action.jpg', // 替换为您的图片路径 width: 90, height: 133, alignment: Alignment.center, ), const Text('立刻行动', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 11.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } class ImageButtonsofa extends StatelessWidget { const ImageButtonsofa({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => const MeditationPage()), // 目标页面 ); }, child: Column( children: [ Image.asset( 'assets/images/sofa.jpg', // 替换为您的图片路径 width: 190, height: 107, alignment: Alignment.center, ), const Text('冥想', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 11.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } class ImageButtoncommunity extends StatelessWidget { const ImageButtoncommunity({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { // 按钮点击逻辑,如导航或显示对话框 ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('按钮被点击!')), ); }, child: Column( children: [ Image.asset( 'assets/images/community.jpg', // 替换为您的图片路径 width: 106, height: 40, alignment: Alignment.center, ), const Text('AI咨询', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 8.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } class ImageButtonknowledge extends StatelessWidget { const ImageButtonknowledge({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { // 按钮点击逻辑,如导航或显示对话框 ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('按钮被点击!')), ); }, child: Column( children: [ Image.asset( 'assets/images/knowledge.jpg', // 替换为您的图片路径 width: 106, height: 40, alignment: Alignment.center, ), const Text('知识', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 8.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } class ImageButtonpsychology extends StatelessWidget { const ImageButtonpsychology({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { // 按钮点击逻辑,如导航或显示对话框 ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('按钮被点击!')), ); }, child: Column( children: [ Image.asset( 'assets/images/psychology.jpg', // 替换为您的图片路径 width: 106, height: 40, alignment: Alignment.center, ), const Text('心理', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 8.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } class ImageButtonme extends StatelessWidget { const ImageButtonme({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { // 按钮点击逻辑,如导航或显示对话框 ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('按钮被点击!')), ); }, child: Column( children: [ Image.asset( 'assets/images/me.jpg', // 替换为您的图片路径 width: 107, height: 40, alignment: Alignment.center, ), const Text('个人', textDirection: TextDirection.ltr, textAlign: TextAlign.center, style: TextStyle( color: Colors.black, fontFamily: 'Comic Sans MS', fontSize: 8.0, fontWeight: FontWeight.w200)) ], )), backgroundColor: Colors.transparent, ); } } 这个代码为什么会超出屏幕
最新发布
11-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值