【问题标题】:How do i show the specified data when i press the specified list?按指定列表时如何显示指定数据?
【发布时间】:2020-12-27 13:06:21
【问题描述】:

所以我开始学习 Flutter 并坚持自己的项目,这次我尝试构建一个联系人列表应用程序。

class MyContacts extends StatelessWidget {

  final name = [
    "Mark Zuckerberg",
    "Udin Sasaki",
    "Eagle Man",
    "Mario Speedwagon",
    "Gail Forcewind",
    "Riqfi Dayat",
    "Fityan fitiw",
    "Awwala khalqyn",
    "lathon",
    "Arifin",
    "SMK Yusuf",
    "Harley",
    "Mom",
    "Brother"
  ];
  final number = [
    "+62 0895643242455",
    "+62 0895062457455",
    "+62 0895603211255",
    "+62 0895345357455",
    "+62 0898764247455",
    "+62 0895676247455",
    "+62 0895602247455",
    "+62 0895602656455",
    "+62 0895602247877",
    "+62 5567567567567",
    "+62 3454564687900",
    "+62 2343245345646",
    "+62 4567686798767",
    "+62 6785634232342"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Contact'),
        leading: Icon(Icons.arrow_back),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.white,
            ),
            onPressed: () {
              // do something
            },
          )
        ],
      ),
      body: ListView.builder(
        itemCount: name.length,
        itemBuilder:(context,index){
          return ListTile(
            title: Text(name[index]),
            subtitle: Text(number[index]),
            leading: CircleAvatar(
                backgroundColor: Colors.lightBlue,
                backgroundImage: NetworkImage(
                    'https://cdn.iconscout.com/icon/free/png-512/avatar-372-456324.png')),
            trailing: Icon(Icons.arrow_forward),
            onTap: (){
              Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => Contact()),
              );
            },
          );
        },
      ),
    );
  }
}

class Contact extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Contact'),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.search,
              color: Colors.white,
            ),
            onPressed: () {
              // do something
            },
          )
        ],
      ),
        body: Container(
            margin: EdgeInsets.only(top: 50),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Image.network('https://cdn.iconscout.com/icon/free/png-512/avatar-372-456324.png', height: 150,),
                SizedBox(height: 100),
                Text("data that stored in the array")
              ],
            )
        )
    );
  }
}

我的问题是,我如何显示我按指定联系人的数据?就像我按下“Harley”联系人一样,上面的文字会自行显示 harley。任何帮助将不胜感激,谢谢

联系人列表:

我需要的结果:

【问题讨论】:

标签: android flutter listview


【解决方案1】:

在 MyContacts 中,将名称传递给 Contact 类。

Navigator.push(
      context, MaterialPageRoute(builder: (context) => Contact(name:name[index])),
    );

在 Contact 类中,这样声明

final String name;
Contact({Key key, @required this.name}) : super(key: key);

在正文中,替换

 Text("data that stored in the array")

 Text(name)

您可以找到更多信息here

【讨论】:

  • 当我通过“Contact(name:name[index]))”时,它表明命名参数“name”没有定义。怎么可能?
  • 您是否在联系人中定义了name
  • 是的,我已经在Contact 中定义了name,但它表明它需要在使用前进行初始化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多