导航时如何在颤动中修复黑屏?

Sha*_*az 17 navigation scaffold dart flutter flutter-navigation

我正在创建一个关于颤振的多页应用程序。当我在其中使用导航时,出现黑屏。

    导入 'package:flutter/material.dart';
    
    
    void main() => runApp(MyHomePage());
    
    class MyHomePage 扩展 StatelessWidget {
      @覆盖
      小部件构建(BuildContext 上下文){
        返回 MaterialApp(
          主页: Page0(),
        );
      }
    }
    
    类 Page0 扩展了 StatefulWidget {
      @覆盖
      _Page0State createState() => _Page0State();
    }
    
    类 _Page0State 扩展状态 {
      @覆盖
      小部件构建(BuildContext 上下文){
        返回脚手架(
          背景颜色:颜色(0xFF493597),
          正文:列表视图(
            孩子们: [
              填充(
                填充:EdgeInsets.only(顶部:15.0,左侧:10.0),
              ),
              大小盒(
                高度:25.0,
              ),
              填充(
                填充:EdgeInsets.only(左:40.0),
                孩子:行(
                  孩子们: [
                    文本(
                      '费用',
                      样式:文本样式(
                          fontFamily: '蒙特塞拉特',
                          颜色:Colors.white,
                          fontWeight: FontWeight.bold,
                          字体大小:25.0),
                    ),
                    大小盒(
                      宽度:10.0,
                    ),
                    文本(
                      '什么',
                      样式:文本样式(
                        fontFamily: '蒙特塞拉特',
                        颜色:Colors.white,
                        字体大小:25.0,
                      ),
                    ),
                  ],
                ),
              ),
              大小框(高度:60.0),
              容器(
                边距:EdgeInsets.only(
                  左:10.0,
                  右:10.0,
                ),
                高度:MediaQuery.of(context).size.height - 150,
                装饰:BoxDecoration(
                  颜色:颜色(0xFFFCFCFC),
                  边界半径:BorderRadius.only(
                    topLeft:半径.circular(75.0),
                    右上:Radius.circular(75.0),
                  ),
                ),
                孩子:列表视图(
                  主要:假,
                  填充:EdgeInsets.only(
                    左:15.0,
                    右:20.0,
                    顶部:25.0,
                  ),
                  孩子们: [
                    填充(
                      填充:const EdgeInsets.only(
                        顶部:30.0,
                      ),
                      孩子:列(
                        孩子们: [
                          //问候语
                          排(
                            孩子们: [
                              展开(
                                孩子:中心(
                                  孩子:文本(
                                    '你好!:)',
                                    样式:文本样式(
                                      fontFamily: '永久标记',
                                      颜色:Colors.black,
                                      字体大小:30.0,
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
    
                          大小盒(
                            高度:30.0,
                          ),
    
                          //添加按钮
                          行(孩子:[
                            展开(
                              弹性:1,
                              孩子:容器(
                                高度:100.0,
                                宽度:100.0,
                                孩子:FittedBox(
                                  孩子:浮动操作按钮(
                                    海拔:10.0,
                                    backgroundColor: Colors.white,
                                    孩子:图标(
                                      Icons.add,
                                      颜色:Colors.black,
                                    ),
                                    按下: () {
                                       Navigator.push(context,MaterialPageRoute(builder: (context) => NewTrip()),);
                                    },
                                  ),``
                                ),
                              ),
                            ),
    
                            //添加文字
                            展开(
                              弹性:1,
                              孩子:文本(
                                '新旅程',
                                样式:文本样式(
                                  fontFamily: 'Nanum',
                                  字体大小:30.0,
                                ),
                              ),
                            ),
                          ]),
    
                          大小盒(
                            高度:30.0,
                          ),
    
                          //上一个行程按钮
                          排(
                            孩子们: [
                              展开(
                                弹性:1,
                                孩子:容器(
                                  高度:100.0,
                                  宽度:100.0,
                                  孩子:FittedBox(
                                    孩子:浮动操作按钮(
                                      海拔:10.0,
                                      backgroundColor: Colors.white,
                                      onPressed: () {},
                                      孩子:图标(
                                        图标评估,
                                        颜色:Colors.black,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
    
                              //之前的行程文字
                              展开(
                                弹性:1,
                                孩子:文本(
                                  '以前的旅行',
                                  样式:文本样式(
                                    fontFamily: 'Nanum',
                                    字体大小:30.0,
                                  ),
                                ),
                              )
                            ],
                          ),
    
                          大小盒(
                            高度:50.0,
                          ),  
                          
    
                         
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      }
    }

而 NewTrip 小部件如下

    类 NewTrip 扩展 StatelessWidget {
      @覆盖
      小部件构建(BuildContext 上下文){
        返回 MaterialApp(
          主页: 脚手架(      
                  正文:文本('新旅行'),
                ),
        );
      }
    }

主页加载正常,但只要我点击新的旅行按钮,它就会显示黑屏。MaterialApp 或 Scaffold 可能存在问题,但我还无法修复它。谁能告诉我是什么问题以及如何解决它?

根据评论中的要求更新了完整代码。

小智 12

所以在NewTrip()remove 中,MaterialApp因为它继承自父级。刚回来Scaffold

class NewTrip extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(      
       body: Text('NEW TRIP'),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Nit*_*esh 9

去除那个 MaterialApp()

class NewTrip extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(      
       body: Text('NEW TRIP'),    
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


lav*_*ava 7

前:

在此输入图像描述

class NewTrip extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Text('NEW TRIP'),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,为了解决这个问题,我们用 widget 包装了新的导航小scaffold部件

之后的解决方案:

在此输入图像描述

class NewTrip extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Material(
        child: Text('NEW TRIP'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*az 6

好吧,经过在互联网上的一些研究,我发现是 FloatingActionButton 造成了麻烦。

我用MaterialButton替换了FloatingActionButton,这解决了我的问题。

  • 如果您仍然想使用浮动按钮,请添加名为“herotag”的浮动按钮属性。这对我有用。 (3认同)
  • 另外,删除您的 MaterialApp,在一个 Flutter 应用程序中使用 2 个 Material App 是没有意义的。 (2认同)

小智 6

请检查您的代码中的完整代码编辑。实际上,您正在使用两个 FloatingActionButton。所以你需要使用两个不同名称的 heroTag。我已经在代码中添加了。NewTrip 类没有问题。

import 'package:flutter/material.dart';

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Page0(),
    );
  }
}

class Page0 extends StatefulWidget {
  @override
  _Page0State createState() => _Page0State();
}

class _Page0State extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF493597),
      body: ListView(
        children: [
          Padding(
            padding: EdgeInsets.only(top: 15.0, left: 10.0),
          ),
          SizedBox(
            height: 25.0,
          ),
          Padding(
            padding: EdgeInsets.only(left: 40.0),
            child: Row(
              children: [
                Text(
                  'Expense',
                  style: TextStyle(
                      fontFamily: 'Montserrat',
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 25.0),
                ),
                SizedBox(
                  width: 10.0,
                ),
                Text(
                  'What',
                  style: TextStyle(
                    fontFamily: 'Montserrat',
                    color: Colors.white,
                    fontSize: 25.0,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 60.0),
          Container(
            margin: EdgeInsets.only(
              left: 10.0,
              right: 10.0,
            ),
            height: MediaQuery.of(context).size.height - 150,
            decoration: BoxDecoration(
              color: Color(0xFFFCFCFC),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(75.0),
                topRight: Radius.circular(75.0),
              ),
            ),
            child: ListView(
              primary: false,
              padding: EdgeInsets.only(
                left: 15.0,
                right: 20.0,
                top: 25.0,
              ),
              children: [
                Padding(
                  padding: const EdgeInsets.only(
                    top: 30.0,
                  ),
                  child: Column(
                    children: [
                      //greeting text
                      Row(
                        children: [
                          Expanded(
                            child: Center(
                              child: Text(
                                'Hello! :)',
                                style: TextStyle(
                                  fontFamily: 'Permanent-Marker',
                                  color: Colors.black,
                                  fontSize: 30.0,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),

                      SizedBox(
                        height: 30.0,
                      ),

                      //add button
                      Row(children: [
                        Expanded(
                          flex: 1,
                          child: Container(
                            height: 100.0,
                            width: 100.0,
                            child: FittedBox(
                              child: FloatingActionButton(
                                heroTag: "btn",
                                elevation: 10.0,
                                backgroundColor: Colors.white,
                                child: Icon(
                                  Icons.add,
                                  color: Colors.black,
                                ),
                                onPressed: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => NewTrip()),
                                  );
                                },
                              ),
                            ),
                          ),
                        ),

                        //add text
                        Expanded(
                          flex: 1,
                          child: Text(
                            'New trip',
                            style: TextStyle(
                              fontFamily: 'Nanum',
                              fontSize: 30.0,
                            ),
                          ),
                        ),
                      ]),

                      SizedBox(
                        height: 30.0,
                      ),

                      //previous trip button
                      Row(
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              height: 100.0,
                              width: 100.0,
                              child: FittedBox(
                                child: FloatingActionButton(
                                  heroTag: "btn1",
                                  elevation: 10.0,
                                  backgroundColor: Colors.white,
                                  onPressed: () {},
                                  child: Icon(
                                    Icons.assessment,
                                    color: Colors.black,
                                  ),
                                ),
                              ),
                            ),
                          ),

                          //previous trip text
                          Expanded(
                            flex: 1,
                            child: Text(
                              'Previous trips',
                              style: TextStyle(
                                fontFamily: 'Nanum',
                                fontSize: 30.0,
                              ),
                            ),
                          )
                        ],
                      ),

                      SizedBox(
                        height: 50.0,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

NewTrip 类

class NewTrip extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Text('NEW TRIP'),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)