Flutter 如何使联系人列表具有交互性?

Flutter 如何使联系人列表具有交互性?,flutter,flutter-layout,Flutter,Flutter Layout,因此,我遵循本教程,并尝试制作一些类似于联系人列表的内容,在其中单击联系人,它会将您带到另一个包含所有联系人信息的屏幕。但我只是被卡住了,我不知道如何添加互动性,所以当你点击一个联系人时,它会把你带到那里的个人页面。这是到目前为止我的代码。 显示联系人列表和搜索栏的代码: 进口“包装:颤振/材料.省道” class ContactsPage extends StatefulWidget { Widget appBarTitle = new Text("Contacts"); Icon a

因此,我遵循本教程,并尝试制作一些类似于联系人列表的内容,在其中单击联系人,它会将您带到另一个包含所有联系人信息的屏幕。但我只是被卡住了,我不知道如何添加互动性,所以当你点击一个联系人时,它会把你带到那里的个人页面。这是到目前为止我的代码。 显示联系人列表和搜索栏的代码:

进口“包装:颤振/材料.省道”

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 ContactsPage扩展StatefulWidget{
Widget appBarTitle=新文本(“联系人”);
图标操作图标=新图标(Icons.search);
@凌驾
状态createState(){
返回新的联系人页面();
}
}
类_ContactPage扩展状态{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
家:新脚手架(
appBar:新的appBar(
标题:widget.appBarTitle,
行动:[
新图标按钮(
图标:widget.actionIcon,
已按下:(){
设置状态(){
if(widget.actionIcon.icon==Icons.search){
widget.actionIcon=新图标(Icons.close);
widget.appBarTitle=新文本字段(
样式:新文本样式(
颜色:颜色,白色,
),
装饰:新的输入装饰(
前缀:
新图标(Icons.search,颜色:Colors.white),
hintText:“搜索…”,
hintStyle:new TextStyle(颜色:Colors.white)),
一旦更改:(值){
印刷品(价值);
//根据值筛选联系人列表
},
);
}否则{
widget.actionIcon=
新图标(Icons.search);//重置为初始状态
widget.appBarTitle=新文本(“联系人”);
}
});
},
),
],
),
正文:新联系人列表(kContacts)),
);
}
}
类ContactPage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“联系人”),
),
正文:新联系人列表(kContacts));
}
}
类ContactList扩展了无状态小部件{
最终联系人名单;
联系人列表(此.\u联系人);
@凌驾
小部件构建(构建上下文){
返回新的ListView.builder(
填充:新边集。对称(垂直:8.0),
itemBuilder:(上下文,索引){
返回新的_ContactListItem(_contacts[index]);
},
itemCount:_contacts.length,
);
}
}
类_ContactListItem扩展ListTile{
_ContactListItem(联系人)
:超级(
标题:新文本(联系人全名),
前导:新CircleAvatar(子项:新文本(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',

  )
];
class联系人{
最终字符串全名;
const Contact({this.fullName});
}
常数kContacts=常数[
常数触点(
全名:'Joey Trib',
),
常数触点(
全名:“约翰尼男孩”,
)
];

解决方案位于您参考的教程的存储库中

List<_ContactListItem> _buildContactList() {
return _contacts.map((contact) =>
                      new _ContactListItem(
                        contact: contact,
                        onTap: () { _showContactPage(context, contact); }
                      ))
                .toList();
}

void _showContactPage(BuildContext context, Contact contact) {
Navigator.push(context, new MaterialPageRoute<Null>(
  settings: const RouteSettings(name: ContactPage.routeName),
  builder: (BuildContext context) => new ContactPage(contact)
));
}

}
List\u buildContactList(){
return\u contacts.map((contact)=>
新联系人列表项(
联系人:联系人,
onTap:({u showContactPage(上下文,联系人);}
))
.toList();
}
void\u showContactPage(构建上下文上下文,联系人){
推送(上下文,新材料路径)(
设置:常数路由设置(名称:ContactPage.routeName),
生成器:(BuildContext上下文)=>新联系人页面(联系人)
));
}
}
资料来源:


@dazza5000那么,我如何将其转换为我拥有的代码,而不会抛出错误呢?我不熟悉这个。