0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.