【发布时间】:2018-09-05 01:16:38
【问题描述】:
所以我正在关注本教程https://medium.com/@XensS/flutter-v-material-design-ii-7b0196e7b42d,我正在尝试制作一些类似联系人列表的内容,您可以在其中单击联系人,然后将您带到另一个屏幕,其中包含所有人员信息。但我只是卡住了,我不确定如何添加交互性,所以当你点击一个联系人时,它会带你到那里的个人页面。到目前为止,这是我的代码。 显示联系人列表和搜索栏的代码:
导入'package:flutter/material.dart';
class ContactsPage extends StatefulWidget {
Widget appBarTitle = new Text("Contacts");
Icon actionIcon = new Icon(Icons.search);
@override
State<StatefulWidget> createState() {
return new _ContactPage();
}
}
class _ContactPage extends State<ContactsPage> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: widget.appBarTitle,
actions: <Widget>[
new IconButton(
icon: widget.actionIcon,
onPressed: () {
setState(() {
if (widget.actionIcon.icon == Icons.search) {
widget.actionIcon = new Icon(Icons.close);
widget.appBarTitle = new TextField(
style: new TextStyle(
color: Colors.white,
),
decoration: new InputDecoration(
prefixIcon:
new Icon(Icons.search, color: Colors.white),
hintText: "Search...",
hintStyle: new TextStyle(color: Colors.white)),
onChanged: (value) {
print(value);
//filter your contact list based on value
},
);
} else {
widget.actionIcon =
new Icon(Icons.search); //reset to initial state
widget.appBarTitle = new Text("Contacts");
}
});
},
),
],
),
body: new ContactList(kContacts)),
);
}
}
class ContactPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Contacts"),
),
body: new ContactList(kContacts));
}
}
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]);
},
itemCount: _contacts.length,
);
}
}
class _ContactListItem extends ListTile {
_ContactListItem(Contact contact)
: super(
title: new Text(contact.fullName),
leading: new CircleAvatar(child: new Text(contact.fullName[0])));
}
这是存储所有联系信息的代码:
class Contact {
final String fullName;
const Contact({this.fullName});
}
const kContacts = const <Contact>[
const Contact(
fullName: 'Joey Trib',
),
const Contact(
fullName: 'Johnny Boy',
)
];
【问题讨论】: