flutter 材料页面路径未打开新屏幕

atmip9wb  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(124)

点击PopupMenuItem时,我试图导航到新屏幕。我正在使用showMenu()创建弹出菜单。
调用新屏幕的代码如下所示:

items: [
    PopupMenuItem(
      onTap: () {
        print(movie.title);
        // Navigator.of(context).push(MaterialPageRoute(
        //     builder: (context) => MoreInfo(movie: movie)));
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => MoreInfo(movie: movie)));
      },
      child: InkWell(
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => MoreInfo(movie: movie)));
          },
          child: Text("More info")),

和MoreInfo屏幕代码如下所示:

Scaffold(
  backgroundColor: Colors.indigo,
  appBar: AppBar(
    leading: InkWell(
      onTap: () {
        Navigator.pop(context);
      },
      child: Icon(Icons.arrow_back_outlined),
    ),
    title: Text(widget.movie.title),
  ),

然而,使用PopupMenuItem的onTap字段并不会打开一个新屏幕。它只是打印出movie.title。我还用InkWell Package 了菜单项的文本,当我点击文本时,一个新屏幕确实出现了,但上面没有内容。只是一个空屏幕,背景颜色为Colors.靛蓝,如MoreInfo中所指定的,但是没有其他元素,比如AppBar。这里可能出了什么问题?我需要使用其他类型的导航吗?

8yparm6h

8yparm6h1#

这段代码也可以工作:

class MyHomePage extends StatefulWidget {

const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            InkWell(
              onTap: () {
                showMenu(
                    context: context,
                    position: const RelativeRect.fromLTRB(0, 0, 0, 0),
                    items: [
                      PopupMenuItem(
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => MoreInfo()));
                        },
                        child: InkWell(
                            onTap: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => MoreInfo()));
                            },
                            child: const Text("More info")),
                      )
                    ]);
              },
              child: const Text('push me'),
            )
          ],
        ),
      ),
    );
  }
}

class MoreInfo extends StatefulWidget {
  MoreInfo({Key? key}) : super(key: key);

  @override
  State<MoreInfo> createState() => _MoreInfoState();
}

class _MoreInfoState extends State<MoreInfo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          child: const Icon(Icons.arrow_back_outlined),
        ),
        title: const Text('widget.movie.title'),
      ),
      body: const Center(
        child: Text('hi'),
      ),
    );
  }
}

相关问题