gpt4 book ai didi

flutter - 上下文未定义

转载 作者:IT王子 更新时间:2023-10-29 07:15:04 26 4
gpt4 key购买 nike

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

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

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),
)
);
}

最佳答案

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

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('...'),
);
}
}

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

关于flutter - 上下文未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com