MaterialApp中的导航器-Flutter

时间:2020-03-23 16:17:18

标签: flutter navigator

我写了一个简单的应用程序,当我想在onPressed中使用build()时,会抛出此类错误:

Navigator operation requested with a context that does not include a Navigator.

有人可以告诉我为什么我不能在onPressed()中使用导航器吗?

我该如何解决?

   @override
  Widget build(BuildContext context) {
    // double width = MediaQuery.of(context).size.width;
    return MaterialApp(
      home:Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(50.0),
        child: AppBar(
          title: Image(
            alignment: Alignment.center,
            image: AssetImage('assets/images/logo.png'),
            height: 70.0,
          ),
          centerTitle: true,
          iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
            backgroundColor: Colors.white,
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.person,
                  color: Color.fromRGBO(9, 133, 46, 100),
                  size: 30.0,       
                ),
                onPressed: () {

//HERE

                  Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => LoginScreen()));
                },
              ),
              IconButton(
                icon: Icon(
                  Icons.shopping_cart,
                  color: Color.fromRGBO(9, 133, 46, 100),
                  size: 30.0,
                ),
                onPressed: (){
                  //AND HERE :(
                },
              ),
            ],
        ),
      ),
      body: Builder(

              ...

            ),
          ),
        ),
        floatingActionButton: RawMaterialButton(
            ...
              ),
        drawer: MyDrawer(), 

      ), 
    );
  }

3 个答案:

答案 0 :(得分:0)

onPressed: _navigate

void _navigate() {
Navigator.push(
    context, MaterialPageRoute(builder: (context) => HomeScreen()));
}

答案 1 :(得分:0)

我通过创建新类来解决

class UserIcon extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    return IconButton(
      icon: Icon(
        Icons.person,
        color: Color.fromRGBO(9, 133, 46, 100),
        size: 30.0,  
      ),
      onPressed: () {
        Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => UserPage()));
      },
    );
  }
}

答案 2 :(得分:0)

尝试创建一个名为Home的类,并在MaterialApp中将其用作主页,而不是直接创建一个Scaffold。 像这样

return: MaterialApp(
home: Home(),
);

然后

class Home extends StatefulWidget {

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(50.0),
        child: AppBar(
          title: Image(
            alignment: Alignment.center,
            image: AssetImage('assets/images/logo.png'),
            height: 70.0,
          ),
          centerTitle: true,
          iconTheme: IconThemeData(color: Color.fromRGBO(9, 133, 46, 100)),
            backgroundColor: Colors.white,
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.person,
                  color: Color.fromRGBO(9, 133, 46, 100),
                  size: 30.0,       
                ),
                onPressed: () {

//TRY MY ANSWER

                  Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => LoginScreen()));
                },
              ),
              IconButton(
                icon: Icon(
                  Icons.shopping_cart,
                  color: Color.fromRGBO(9, 133, 46, 100),
                  size: 30.0,
                ),
                onPressed: (){
                  //HOPEFULL IT WORKS :)
                },
              ),
            ],
        ),
      ),
      body: Builder(

              ...

            ),
          ),
        ),
        floatingActionButton: RawMaterialButton(
            ...
              ),
        drawer: MyDrawer(), 

      ), 
    );
  }
}

您收到此错误,因为MaterialApp实际上是MyApp的子级。因此,当我们使用“上下文”构建另一个页面时,MyApp的{​​{1}}不能使用BuildContext作为父级。因此,使用新的Class通常可以解决此问题!

请告诉它是否无效!!!