【问题标题】:Context is Undefined上下文未定义
【发布时间】:2019-08-23 20:53:53
【问题描述】:

我试图通过单击列表中的一个项目来转到另一个页面,但他说上下文是未定义的。 我做错了什么?有什么建议吗?谢谢你的帮助

我在代码中注释了第一个显示未定义的上下文。

class ListaServidores extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      backgroundColor: Colors.black,
      centerTitle: true,
      title: Text("Lista de servidores"),
    ),
    body: ContactList(kContacts)
);
}
}

class ContactList extends StatelessWidget {
final List<Contact> _contacts;
ContactList(this._contacts);
@override
Widget build(BuildContext context) {
return ListView(
    padding: EdgeInsets.symmetric(vertical: 8.0),
    children: _buildContactList()
);
}

List<_ContactListItem> _buildContactList() {
return _contacts.map((contact) => _ContactListItem(contact))
    .toList();
}
}

class _ContactListItem extends ListTile {
_ContactListItem(Contact contact) :
    super(
      onTap:(){//here context undefined
        Navigator.push(context, MaterialPageRoute(builder: (context)=> 
NovoSigilo(servidor: contact.oservidor)));  
      },
      title : Text(
        contact.oservidor,
        style: TextStyle(
          fontSize: 18.0, fontWeight: FontWeight.bold),
      ),
      subtitle: Text(contact.texto, overflow: TextOverflow.ellipsis),
      leading: CircleAvatar(
        backgroundColor: Colors.white,
          child: Image.asset(contact.img),
      )
  );
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    不要扩展小部件。相反,请使用组合。

    class _ContactListItem extends StatelessWidget {
      const _ContactListItem({Key key, this.contact}) : super(key: key);
    
      final Contact contact;
    
      @override
      Widget build(BuildContext context) {
        return ListTile(
          onTap: () {
            // ...
          },
          title: Text('...'),
        );
      }
    }
    

    这将解决您在此过程中遇到的问题。

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2019-07-07
      • 2021-02-04
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多