如何解决在不包含导航器的上下文中请求的“导航器操作”。扑朔迷离

时间:2019-10-10 14:14:13

标签: flutter dart

我遇到此错误。

  

在不包含导航器的上下文中请求的导航器操作。

我遵循Internet上的准则,但是我仍然对如何解决此错误感到困惑。这是我的代码


     class SecondScreen extends StatelessWidget{
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [Colors.amberAccent, Colors.red]),
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,// add Column
                children: <Widget>[
                  Text('Welcome', style: TextStyle( // your text
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white)
                  ),
                  RaisedButton(onPressed: () {
                    Navigator.pop(context);
                  },
                    child: Text('Button'),
                    shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)
                    ),
                    color: Colors.white,
                    splashColor: Colors.blue,
                    textColor: Color(0xfffe67e22),
                  ), // your button beneath text
                ],
              ),
            ),
          ),
        );
      }
    }
    ```

2 个答案:

答案 0 :(得分:2)

问题不在您的第二页中。实际上问题出在您的main.dart中;您应该为MaterialApp的home属性创建一个新的小部件,而不是直接使用Scaffold小部件;

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: App(),
    ),
  );
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () => Navigator.push(
                context,
                MaterialPageRoute(builder: (_) => SecondPage()),
              ),
              child: Text("SecondPage"),
            ),
          ],
        ),
      ),
    );
  }
}

答案 1 :(得分:0)

截屏:

enter image description here


完整代码:

void main() => runApp(MaterialApp(home: MyApp(), debugShowCheckedModeBanner: false));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.amberAccent, Colors.red]),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center, // add Column
            children: <Widget>[
              Text('Welcome',
                  style: TextStyle(
                      // your text
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white)),
              RaisedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute<Null>(builder: (BuildContext context) {
                    return SecondScreen();
                  }));
                },
                child: Text('Button'),
                shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
                color: Colors.white,
                splashColor: Colors.blue,
                textColor: Color(0xfffe67e22),
              ), // your button beneath text
            ],
          ),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.amberAccent, Colors.red]),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center, // add Column
            children: <Widget>[
              Text('Welcome',
                  style: TextStyle(
                      // your text
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white)),
              RaisedButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text('Button'),
                shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
                color: Colors.white,
                splashColor: Colors.blue,
                textColor: Color(0xfffe67e22),
              ), // your button beneath text
            ],
          ),
        ),
      ),
    );
  }
}