正确的页面导航 [英] Proper page navigation

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

问题描述

我正在尝试导航到名为contactView的页面.我已经列出了联系人,当我单击那里的名称时,我等待导航到该联系人.到目前为止,这就是我所拥有的.我一直在努力使导航正常工作.任何帮助将是巨大的.

I am trying to navigate to a page called contactView. I have made a list of contacts and I wait to navogate to a contact when I click on there name. This is what I have so far. I am stuck trying to get the navigation to work. Any help would be great.

class ContactList extends StatelessWidget {
  final List<Contact> _contacts;

  ContactList(this._contacts);

  @override
  Widget build(BuildContext context) {
    return new ListView.builder(
      padding: new EdgeInsets.symmetric(vertical: 8.0),
      itemBuilder: (context, index) {
        return new _ContactListItem(_contacts[index]);
        Navigator.push(context,  MaterialPageRoute(builder: (context) => viewContact())
        );
      },

      itemCount: _contacts.length,
    );

  }
}

推荐答案

以下是我可以立即指出的几件事(问题):

Here are few things that I can immediately point out (Problems):

  • onPressed is not available on ListView.builder() , you may check here: https://docs.flutter.io/flutter/widgets/ListView/ListView.builder.html
  • Navigator.push(context, MaterialPageRoute(builder: (context) => viewContact()) this won't execute because it is after return

建议:

  • 您可能需要将_ContactListItem()包装在 GestureDetector并实现onTap回调
  • You might need to wrap your _ContactListItem() inside a GestureDetector and implement an onTap callback

示例代码:

class ContactList extends StatelessWidget {
  final List<Contact> _contacts;

  ContactList(this._contacts);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: EdgeInsets.symmetric(vertical: 8.0),
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            //TODO: Insert your navigation logic here
            Navigator.of(context).push(MaterialPageRoute(
                builder: (BuildContext context) =>
                    ContactView(_contacts[index])));
          },
          child: _ContactListItem(_contacts[index]),
        );
      },
      itemCount: _contacts.length,
    );
  }
}

  • 另一个选择可能是更改 _ContactListItem()并且可能使用ListTile并在ListTile中实现onTap,您可以在这里找到它: https://docs.flutter.io/flutter/material/ListTile-class.html
  • 您也可以尝试实现命名路由,这是有关 https://flutter.io/cookbook/networking/named-routes/
    • Another option could be to change the implementation of _ContactListItem() and may be use a ListTile and implement an onTap in ListTile, you can find it here: https://docs.flutter.io/flutter/material/ListTile-class.html
    • You may also try to implement named routes, here is a tutorial for that https://flutter.io/cookbook/networking/named-routes/
    • 我希望这在某种程度上是有帮助的,如果我对这个问题有误解,请告诉我.

      I hope this was helpful in someway, let me know if I misinterpreted the question.

      这篇关于正确的页面导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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