David Concha
David Concha

Reputation: 133

Context is Undefined

I'm trying go to another page by clicking on a item from a list, but he says the context is Undefined. What i'm doing wrong? Any suggestion? Thank you for the help

I commented in the code where appears the first context that says Undefined.

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

Upvotes: 0

Views: 151

Answers (1)

R&#233;mi Rousselet
R&#233;mi Rousselet

Reputation: 276891

Do not extend widgets. Instead, use composition.

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

This will solve your issue in the process.

Upvotes: 1

Related Questions