颤振导航器不起作用 [英] Flutter Navigator not working

查看:26
本文介绍了颤振导航器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个屏幕的应用程序,我想通过按下按钮从第一个屏幕推送到第二个屏幕.

I have app with two screens, and I want to make push from 1st to second screen by pressing button.

屏幕 1

import 'package:flutter/material.dart';
import './view/second_page.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MainScreen();
  }
}

class MainScreen extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
                title: new Text("Title")
            ),
            body: new Center(
                child: new FlatButton(child: new Text("Second page"),
                    onPressed: () {
                      Navigator.push(context,
                          new MaterialPageRoute(
                              builder: (context) => new SecondPage()))
                    }
                )
            )
        )
    );
  }
}

屏幕 2

import 'package:flutter/material.dart';

class SecondPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return new SecondPageState();
  }
}


class SecondPageState extends State<SecondPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Title"),
      ),
      body: new Center(
        child: new Text("Some text"),
      ),
    );
  }
}

推送没有发生,我得到了这个

Push not happening and I got this

处理手势时抛出以下断言:导航器使用不包含导航器的上下文请求的操作.用于从导航器推送或弹出路由的上下文必须是作为导航器小部件的后代的小部件.

The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

引发了另一个异常:Navigator operation requests with a不包含导航器的上下文.

Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.

怎么了?

推荐答案

将 Flutter 中的小部件想象成一棵树,上下文指向使用 build 函数构建的任何节点.在你的情况下,你有

Think of the widgets in Flutter as a tree, with the context pointing to whichever node is being built with the build function. In your case, you have

MainScreen    <------ context
  --> MaterialApp
   (--> Navigator built within MaterialApp)
      --> Scaffold
        --> App Bar
          --> ...
        --> Center
          --> FlatButton

因此,当您使用上下文查找导航器时,您使用的是不在导航器下方的 MainScreen 上下文.

So when you're using the context to find the Navigator, you're using a context for the MainScreen which isn't under the navigator.

您可以创建一个新的无状态或有状态小部件子类来包含您的 Center + FlatButton,因为它们中的构建函数将指向该级别,或者您可以使用 Builder 并定义 builder 回调(具有指向 Builder 的上下文)以返回居中 + 扁平按钮.

You can either make a new Stateless or Stateful Widget subclass to contain your Center + FlatButton, as the build function within those will point at that level instead, or you can use a Builder and define the builder callback (which has a context pointing at the Builder) to return the Center + FlatButton.

这篇关于颤振导航器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆